1 | // Copyright 2014 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 parse |
6 | |
7 | import ( |
8 | "reflect" |
9 | "strings" |
10 | "testing" |
11 | ) |
12 | |
13 | func TestParseLine(t *testing.T) { |
14 | cases := []struct { |
15 | line string |
16 | want *Benchmark |
17 | err bool // expect an error |
18 | }{ |
19 | { |
20 | line: "BenchmarkEncrypt 100000000 19.6 ns/op", |
21 | want: &Benchmark{ |
22 | Name: "BenchmarkEncrypt", |
23 | N: 100000000, NsPerOp: 19.6, |
24 | Measured: NsPerOp, |
25 | }, |
26 | }, |
27 | { |
28 | line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s", |
29 | want: &Benchmark{ |
30 | Name: "BenchmarkEncrypt", |
31 | N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, |
32 | Measured: NsPerOp | MBPerS, |
33 | }, |
34 | }, |
35 | { |
36 | line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77", |
37 | want: &Benchmark{ |
38 | Name: "BenchmarkEncrypt", |
39 | N: 100000000, NsPerOp: 19.6, |
40 | Measured: NsPerOp, |
41 | }, |
42 | }, |
43 | { |
44 | line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 5 allocs/op", |
45 | want: &Benchmark{ |
46 | Name: "BenchmarkEncrypt", |
47 | N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocsPerOp: 5, |
48 | Measured: NsPerOp | MBPerS | AllocsPerOp, |
49 | }, |
50 | }, |
51 | { |
52 | line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 3 B/op 5 allocs/op", |
53 | want: &Benchmark{ |
54 | Name: "BenchmarkEncrypt", |
55 | N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocedBytesPerOp: 3, AllocsPerOp: 5, |
56 | Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp, |
57 | }, |
58 | }, |
59 | // error handling cases |
60 | { |
61 | line: "BenchPress 100 19.6 ns/op", // non-benchmark |
62 | err: true, |
63 | }, |
64 | { |
65 | line: "BenchmarkEncrypt lots 19.6 ns/op", // non-int iterations |
66 | err: true, |
67 | }, |
68 | { |
69 | line: "BenchmarkBridge 100000000 19.6 smoots", // unknown unit |
70 | want: &Benchmark{ |
71 | Name: "BenchmarkBridge", |
72 | N: 100000000, |
73 | }, |
74 | }, |
75 | { |
76 | line: "PASS", |
77 | err: true, |
78 | }, |
79 | } |
80 | |
81 | for _, tt := range cases { |
82 | have, err := ParseLine(tt.line) |
83 | if tt.err && err == nil { |
84 | t.Errorf("parsing line %q should have failed", tt.line) |
85 | continue |
86 | } |
87 | if !reflect.DeepEqual(have, tt.want) { |
88 | t.Errorf("parsed line %q incorrectly, want %v have %v", tt.line, tt.want, have) |
89 | } |
90 | } |
91 | } |
92 | |
93 | func TestParseSet(t *testing.T) { |
94 | // Test two things: |
95 | // 1. The noise that can accompany testing.B output gets ignored. |
96 | // 2. Benchmarks with the same name have their order preserved. |
97 | in := ` |
98 | ? crypto [no test files] |
99 | PASS |
100 | pem_decrypt_test.go:17: test 4. %!s(x509.PEMCipher=5) |
101 | ... [output truncated] |
102 | |
103 | BenchmarkEncrypt 100000000 19.6 ns/op |
104 | BenchmarkEncrypt 5000000 517 ns/op |
105 | === RUN TestChunk |
106 | --- PASS: TestChunk (0.00 seconds) |
107 | --- SKIP: TestLinuxSendfile (0.00 seconds) |
108 | fs_test.go:716: skipping; linux-only test |
109 | BenchmarkReadRequestApachebench 1000000 2960 ns/op 27.70 MB/s 839 B/op 9 allocs/op |
110 | BenchmarkClientServerParallel64 50000 59192 ns/op 7028 B/op 60 allocs/op |
111 | ok net/http 95.783s |
112 | ` |
113 | |
114 | want := Set{ |
115 | "BenchmarkReadRequestApachebench": []*Benchmark{ |
116 | { |
117 | Name: "BenchmarkReadRequestApachebench", |
118 | N: 1000000, NsPerOp: 2960, MBPerS: 27.70, AllocedBytesPerOp: 839, AllocsPerOp: 9, |
119 | Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp, |
120 | Ord: 2, |
121 | }, |
122 | }, |
123 | "BenchmarkClientServerParallel64": []*Benchmark{ |
124 | { |
125 | Name: "BenchmarkClientServerParallel64", |
126 | N: 50000, NsPerOp: 59192, AllocedBytesPerOp: 7028, AllocsPerOp: 60, |
127 | Measured: NsPerOp | AllocedBytesPerOp | AllocsPerOp, |
128 | Ord: 3, |
129 | }, |
130 | }, |
131 | "BenchmarkEncrypt": []*Benchmark{ |
132 | { |
133 | Name: "BenchmarkEncrypt", |
134 | N: 100000000, NsPerOp: 19.6, |
135 | Measured: NsPerOp, |
136 | Ord: 0, |
137 | }, |
138 | { |
139 | Name: "BenchmarkEncrypt", |
140 | N: 5000000, NsPerOp: 517, |
141 | Measured: NsPerOp, |
142 | Ord: 1, |
143 | }, |
144 | }, |
145 | } |
146 | |
147 | have, err := ParseSet(strings.NewReader(in)) |
148 | if err != nil { |
149 | t.Fatalf("unexpected err during ParseSet: %v", err) |
150 | } |
151 | if !reflect.DeepEqual(want, have) { |
152 | t.Errorf("parsed bench set incorrectly, want %v have %v", want, have) |
153 | } |
154 | } |
155 | |
156 | func TestString(t *testing.T) { |
157 | tests := []struct { |
158 | name string |
159 | input *Benchmark |
160 | wanted string |
161 | }{ |
162 | { |
163 | name: "nsTest", |
164 | input: &Benchmark{ |
165 | Name: "BenchmarkTest", |
166 | N: 100000000, NsPerOp: 19.6, |
167 | Measured: NsPerOp, |
168 | }, |
169 | wanted: "BenchmarkTest 100000000 19.60 ns/op", |
170 | }, |
171 | { |
172 | name: "mbTest", |
173 | input: &Benchmark{ |
174 | Name: "BenchmarkTest", |
175 | N: 100000000, MBPerS: 19.6, |
176 | Measured: MBPerS, |
177 | }, |
178 | wanted: "BenchmarkTest 100000000 19.60 MB/s", |
179 | }, |
180 | { |
181 | name: "allocatedBytesTest", |
182 | input: &Benchmark{ |
183 | Name: "BenchmarkTest", |
184 | N: 100000000, AllocedBytesPerOp: 5, |
185 | Measured: AllocedBytesPerOp, |
186 | }, |
187 | wanted: "BenchmarkTest 100000000 5 B/op", |
188 | }, |
189 | { |
190 | name: "allocsTest", |
191 | input: &Benchmark{ |
192 | Name: "BenchmarkTest", |
193 | N: 100000000, AllocsPerOp: 5, |
194 | Measured: AllocsPerOp, |
195 | }, |
196 | wanted: "BenchmarkTest 100000000 5 allocs/op", |
197 | }, |
198 | } |
199 | |
200 | for _, tt := range tests { |
201 | t.Run(tt.name, func(t *testing.T) { |
202 | result := tt.input.String() |
203 | if result != tt.wanted { |
204 | t.Errorf("String() is called, want %q, have %q", tt.wanted, result) |
205 | } |
206 | }) |
207 | } |
208 | } |
209 |
Members