File: trace_test.go

package info (click to toggle)
golang-golang-x-net-dev 1:0.0+git20180124.0ed95ab+dfsg-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 5,420 kB
  • sloc: makefile: 19; asm: 18
file content (178 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package trace

import (
	"net/http"
	"reflect"
	"testing"
)

type s struct{}

func (s) String() string { return "lazy string" }

// TestReset checks whether all the fields are zeroed after reset.
func TestReset(t *testing.T) {
	tr := New("foo", "bar")
	tr.LazyLog(s{}, false)
	tr.LazyPrintf("%d", 1)
	tr.SetRecycler(func(_ interface{}) {})
	tr.SetTraceInfo(3, 4)
	tr.SetMaxEvents(100)
	tr.SetError()
	tr.Finish()

	tr.(*trace).reset()

	if !reflect.DeepEqual(tr, new(trace)) {
		t.Errorf("reset didn't clear all fields: %+v", tr)
	}
}

// TestResetLog checks whether all the fields are zeroed after reset.
func TestResetLog(t *testing.T) {
	el := NewEventLog("foo", "bar")
	el.Printf("message")
	el.Errorf("error")
	el.Finish()

	el.(*eventLog).reset()

	if !reflect.DeepEqual(el, new(eventLog)) {
		t.Errorf("reset didn't clear all fields: %+v", el)
	}
}

func TestAuthRequest(t *testing.T) {
	testCases := []struct {
		host string
		want bool
	}{
		{host: "192.168.23.1", want: false},
		{host: "192.168.23.1:8080", want: false},
		{host: "malformed remote addr", want: false},
		{host: "localhost", want: true},
		{host: "localhost:8080", want: true},
		{host: "127.0.0.1", want: true},
		{host: "127.0.0.1:8080", want: true},
		{host: "::1", want: true},
		{host: "[::1]:8080", want: true},
	}
	for _, tt := range testCases {
		req := &http.Request{RemoteAddr: tt.host}
		any, sensitive := AuthRequest(req)
		if any != tt.want || sensitive != tt.want {
			t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
		}
	}
}

// TestParseTemplate checks that all templates used by this package are valid
// as they are parsed on first usage
func TestParseTemplate(t *testing.T) {
	if tmpl := distTmpl(); tmpl == nil {
		t.Error("invalid template returned from distTmpl()")
	}
	if tmpl := pageTmpl(); tmpl == nil {
		t.Error("invalid template returned from pageTmpl()")
	}
	if tmpl := eventsTmpl(); tmpl == nil {
		t.Error("invalid template returned from eventsTmpl()")
	}
}

func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
	numSpans := (b.N + numEvents + 1) / numEvents

	for i := 0; i < numSpans; i++ {
		tr := New("test", "test")
		tr.SetMaxEvents(maxEvents)
		for j := 0; j < numEvents; j++ {
			tr.LazyPrintf("%d", j)
		}
		tr.Finish()
	}
}

func BenchmarkTrace_Default_2(b *testing.B) {
	benchmarkTrace(b, 0, 2)
}

func BenchmarkTrace_Default_10(b *testing.B) {
	benchmarkTrace(b, 0, 10)
}

func BenchmarkTrace_Default_100(b *testing.B) {
	benchmarkTrace(b, 0, 100)
}

func BenchmarkTrace_Default_1000(b *testing.B) {
	benchmarkTrace(b, 0, 1000)
}

func BenchmarkTrace_Default_10000(b *testing.B) {
	benchmarkTrace(b, 0, 10000)
}

func BenchmarkTrace_10_2(b *testing.B) {
	benchmarkTrace(b, 10, 2)
}

func BenchmarkTrace_10_10(b *testing.B) {
	benchmarkTrace(b, 10, 10)
}

func BenchmarkTrace_10_100(b *testing.B) {
	benchmarkTrace(b, 10, 100)
}

func BenchmarkTrace_10_1000(b *testing.B) {
	benchmarkTrace(b, 10, 1000)
}

func BenchmarkTrace_10_10000(b *testing.B) {
	benchmarkTrace(b, 10, 10000)
}

func BenchmarkTrace_100_2(b *testing.B) {
	benchmarkTrace(b, 100, 2)
}

func BenchmarkTrace_100_10(b *testing.B) {
	benchmarkTrace(b, 100, 10)
}

func BenchmarkTrace_100_100(b *testing.B) {
	benchmarkTrace(b, 100, 100)
}

func BenchmarkTrace_100_1000(b *testing.B) {
	benchmarkTrace(b, 100, 1000)
}

func BenchmarkTrace_100_10000(b *testing.B) {
	benchmarkTrace(b, 100, 10000)
}

func BenchmarkTrace_1000_2(b *testing.B) {
	benchmarkTrace(b, 1000, 2)
}

func BenchmarkTrace_1000_10(b *testing.B) {
	benchmarkTrace(b, 1000, 10)
}

func BenchmarkTrace_1000_100(b *testing.B) {
	benchmarkTrace(b, 1000, 100)
}

func BenchmarkTrace_1000_1000(b *testing.B) {
	benchmarkTrace(b, 1000, 1000)
}

func BenchmarkTrace_1000_10000(b *testing.B) {
	benchmarkTrace(b, 1000, 10000)
}