1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 func (s) String() string { return "lazy string" }
17 // TestReset checks whether all the fields are zeroed after reset.
18 func TestReset(t *testing.T) {
19 tr := New("foo", "bar")
20 tr.LazyLog(s{}, false)
21 tr.LazyPrintf("%d", 1)
22 tr.SetRecycler(func(_ interface{}) {})
30 if !reflect.DeepEqual(tr, new(trace)) {
31 t.Errorf("reset didn't clear all fields: %+v", tr)
35 // TestResetLog checks whether all the fields are zeroed after reset.
36 func TestResetLog(t *testing.T) {
37 el := NewEventLog("foo", "bar")
42 el.(*eventLog).reset()
44 if !reflect.DeepEqual(el, new(eventLog)) {
45 t.Errorf("reset didn't clear all fields: %+v", el)
49 func TestAuthRequest(t *testing.T) {
50 testCases := []struct {
54 {host: "192.168.23.1", want: false},
55 {host: "192.168.23.1:8080", want: false},
56 {host: "malformed remote addr", want: false},
57 {host: "localhost", want: true},
58 {host: "localhost:8080", want: true},
59 {host: "127.0.0.1", want: true},
60 {host: "127.0.0.1:8080", want: true},
61 {host: "::1", want: true},
62 {host: "[::1]:8080", want: true},
64 for _, tt := range testCases {
65 req := &http.Request{RemoteAddr: tt.host}
66 any, sensitive := AuthRequest(req)
67 if any != tt.want || sensitive != tt.want {
68 t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
73 // TestParseTemplate checks that all templates used by this package are valid
74 // as they are parsed on first usage
75 func TestParseTemplate(t *testing.T) {
76 if tmpl := distTmpl(); tmpl == nil {
77 t.Error("invalid template returned from distTmpl()")
79 if tmpl := pageTmpl(); tmpl == nil {
80 t.Error("invalid template returned from pageTmpl()")
82 if tmpl := eventsTmpl(); tmpl == nil {
83 t.Error("invalid template returned from eventsTmpl()")
87 func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
88 numSpans := (b.N + numEvents + 1) / numEvents
90 for i := 0; i < numSpans; i++ {
91 tr := New("test", "test")
92 tr.SetMaxEvents(maxEvents)
93 for j := 0; j < numEvents; j++ {
94 tr.LazyPrintf("%d", j)
100 func BenchmarkTrace_Default_2(b *testing.B) {
101 benchmarkTrace(b, 0, 2)
104 func BenchmarkTrace_Default_10(b *testing.B) {
105 benchmarkTrace(b, 0, 10)
108 func BenchmarkTrace_Default_100(b *testing.B) {
109 benchmarkTrace(b, 0, 100)
112 func BenchmarkTrace_Default_1000(b *testing.B) {
113 benchmarkTrace(b, 0, 1000)
116 func BenchmarkTrace_Default_10000(b *testing.B) {
117 benchmarkTrace(b, 0, 10000)
120 func BenchmarkTrace_10_2(b *testing.B) {
121 benchmarkTrace(b, 10, 2)
124 func BenchmarkTrace_10_10(b *testing.B) {
125 benchmarkTrace(b, 10, 10)
128 func BenchmarkTrace_10_100(b *testing.B) {
129 benchmarkTrace(b, 10, 100)
132 func BenchmarkTrace_10_1000(b *testing.B) {
133 benchmarkTrace(b, 10, 1000)
136 func BenchmarkTrace_10_10000(b *testing.B) {
137 benchmarkTrace(b, 10, 10000)
140 func BenchmarkTrace_100_2(b *testing.B) {
141 benchmarkTrace(b, 100, 2)
144 func BenchmarkTrace_100_10(b *testing.B) {
145 benchmarkTrace(b, 100, 10)
148 func BenchmarkTrace_100_100(b *testing.B) {
149 benchmarkTrace(b, 100, 100)
152 func BenchmarkTrace_100_1000(b *testing.B) {
153 benchmarkTrace(b, 100, 1000)
156 func BenchmarkTrace_100_10000(b *testing.B) {
157 benchmarkTrace(b, 100, 10000)
160 func BenchmarkTrace_1000_2(b *testing.B) {
161 benchmarkTrace(b, 1000, 2)
164 func BenchmarkTrace_1000_10(b *testing.B) {
165 benchmarkTrace(b, 1000, 10)
168 func BenchmarkTrace_1000_100(b *testing.B) {
169 benchmarkTrace(b, 1000, 100)
172 func BenchmarkTrace_1000_1000(b *testing.B) {
173 benchmarkTrace(b, 1000, 1000)
176 func BenchmarkTrace_1000_10000(b *testing.B) {
177 benchmarkTrace(b, 1000, 10000)