OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / trace / trace_test.go
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.
4
5 package trace
6
7 import (
8         "net/http"
9         "reflect"
10         "testing"
11 )
12
13 type s struct{}
14
15 func (s) String() string { return "lazy string" }
16
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{}) {})
23         tr.SetTraceInfo(3, 4)
24         tr.SetMaxEvents(100)
25         tr.SetError()
26         tr.Finish()
27
28         tr.(*trace).reset()
29
30         if !reflect.DeepEqual(tr, new(trace)) {
31                 t.Errorf("reset didn't clear all fields: %+v", tr)
32         }
33 }
34
35 // TestResetLog checks whether all the fields are zeroed after reset.
36 func TestResetLog(t *testing.T) {
37         el := NewEventLog("foo", "bar")
38         el.Printf("message")
39         el.Errorf("error")
40         el.Finish()
41
42         el.(*eventLog).reset()
43
44         if !reflect.DeepEqual(el, new(eventLog)) {
45                 t.Errorf("reset didn't clear all fields: %+v", el)
46         }
47 }
48
49 func TestAuthRequest(t *testing.T) {
50         testCases := []struct {
51                 host string
52                 want bool
53         }{
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},
63         }
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)
69                 }
70         }
71 }
72
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()")
78         }
79         if tmpl := pageTmpl(); tmpl == nil {
80                 t.Error("invalid template returned from pageTmpl()")
81         }
82         if tmpl := eventsTmpl(); tmpl == nil {
83                 t.Error("invalid template returned from eventsTmpl()")
84         }
85 }
86
87 func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
88         numSpans := (b.N + numEvents + 1) / numEvents
89
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)
95                 }
96                 tr.Finish()
97         }
98 }
99
100 func BenchmarkTrace_Default_2(b *testing.B) {
101         benchmarkTrace(b, 0, 2)
102 }
103
104 func BenchmarkTrace_Default_10(b *testing.B) {
105         benchmarkTrace(b, 0, 10)
106 }
107
108 func BenchmarkTrace_Default_100(b *testing.B) {
109         benchmarkTrace(b, 0, 100)
110 }
111
112 func BenchmarkTrace_Default_1000(b *testing.B) {
113         benchmarkTrace(b, 0, 1000)
114 }
115
116 func BenchmarkTrace_Default_10000(b *testing.B) {
117         benchmarkTrace(b, 0, 10000)
118 }
119
120 func BenchmarkTrace_10_2(b *testing.B) {
121         benchmarkTrace(b, 10, 2)
122 }
123
124 func BenchmarkTrace_10_10(b *testing.B) {
125         benchmarkTrace(b, 10, 10)
126 }
127
128 func BenchmarkTrace_10_100(b *testing.B) {
129         benchmarkTrace(b, 10, 100)
130 }
131
132 func BenchmarkTrace_10_1000(b *testing.B) {
133         benchmarkTrace(b, 10, 1000)
134 }
135
136 func BenchmarkTrace_10_10000(b *testing.B) {
137         benchmarkTrace(b, 10, 10000)
138 }
139
140 func BenchmarkTrace_100_2(b *testing.B) {
141         benchmarkTrace(b, 100, 2)
142 }
143
144 func BenchmarkTrace_100_10(b *testing.B) {
145         benchmarkTrace(b, 100, 10)
146 }
147
148 func BenchmarkTrace_100_100(b *testing.B) {
149         benchmarkTrace(b, 100, 100)
150 }
151
152 func BenchmarkTrace_100_1000(b *testing.B) {
153         benchmarkTrace(b, 100, 1000)
154 }
155
156 func BenchmarkTrace_100_10000(b *testing.B) {
157         benchmarkTrace(b, 100, 10000)
158 }
159
160 func BenchmarkTrace_1000_2(b *testing.B) {
161         benchmarkTrace(b, 1000, 2)
162 }
163
164 func BenchmarkTrace_1000_10(b *testing.B) {
165         benchmarkTrace(b, 1000, 10)
166 }
167
168 func BenchmarkTrace_1000_100(b *testing.B) {
169         benchmarkTrace(b, 1000, 100)
170 }
171
172 func BenchmarkTrace_1000_1000(b *testing.B) {
173         benchmarkTrace(b, 1000, 1000)
174 }
175
176 func BenchmarkTrace_1000_10000(b *testing.B) {
177         benchmarkTrace(b, 1000, 10000)
178 }