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.
13 func TestParseLine(t *testing.T) {
17 err bool // expect an error
20 line: "BenchmarkEncrypt 100000000 19.6 ns/op",
22 Name: "BenchmarkEncrypt",
23 N: 100000000, NsPerOp: 19.6,
28 line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s",
30 Name: "BenchmarkEncrypt",
31 N: 100000000, NsPerOp: 19.6, MBPerS: 817.77,
32 Measured: NsPerOp | MBPerS,
36 line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77",
38 Name: "BenchmarkEncrypt",
39 N: 100000000, NsPerOp: 19.6,
44 line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 5 allocs/op",
46 Name: "BenchmarkEncrypt",
47 N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocsPerOp: 5,
48 Measured: NsPerOp | MBPerS | AllocsPerOp,
52 line: "BenchmarkEncrypt 100000000 19.6 ns/op 817.77 MB/s 3 B/op 5 allocs/op",
54 Name: "BenchmarkEncrypt",
55 N: 100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocedBytesPerOp: 3, AllocsPerOp: 5,
56 Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
59 // error handling cases
61 line: "BenchPress 100 19.6 ns/op", // non-benchmark
65 line: "BenchmarkEncrypt lots 19.6 ns/op", // non-int iterations
69 line: "BenchmarkBridge 100000000 19.6 smoots", // unknown unit
71 Name: "BenchmarkBridge",
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)
87 if !reflect.DeepEqual(have, tt.want) {
88 t.Errorf("parsed line %q incorrectly, want %v have %v", tt.line, tt.want, have)
93 func TestParseSet(t *testing.T) {
95 // 1. The noise that can accompany testing.B output gets ignored.
96 // 2. Benchmarks with the same name have their order preserved.
98 ? crypto [no test files]
100 pem_decrypt_test.go:17: test 4. %!s(x509.PEMCipher=5)
101 ... [output truncated]
103 BenchmarkEncrypt 100000000 19.6 ns/op
104 BenchmarkEncrypt 5000000 517 ns/op
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
115 "BenchmarkReadRequestApachebench": []*Benchmark{
117 Name: "BenchmarkReadRequestApachebench",
118 N: 1000000, NsPerOp: 2960, MBPerS: 27.70, AllocedBytesPerOp: 839, AllocsPerOp: 9,
119 Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
123 "BenchmarkClientServerParallel64": []*Benchmark{
125 Name: "BenchmarkClientServerParallel64",
126 N: 50000, NsPerOp: 59192, AllocedBytesPerOp: 7028, AllocsPerOp: 60,
127 Measured: NsPerOp | AllocedBytesPerOp | AllocsPerOp,
131 "BenchmarkEncrypt": []*Benchmark{
133 Name: "BenchmarkEncrypt",
134 N: 100000000, NsPerOp: 19.6,
139 Name: "BenchmarkEncrypt",
140 N: 5000000, NsPerOp: 517,
147 have, err := ParseSet(strings.NewReader(in))
149 t.Fatalf("unexpected err during ParseSet: %v", err)
151 if !reflect.DeepEqual(want, have) {
152 t.Errorf("parsed bench set incorrectly, want %v have %v", want, have)
156 func TestString(t *testing.T) {
165 Name: "BenchmarkTest",
166 N: 100000000, NsPerOp: 19.6,
169 wanted: "BenchmarkTest 100000000 19.60 ns/op",
174 Name: "BenchmarkTest",
175 N: 100000000, MBPerS: 19.6,
178 wanted: "BenchmarkTest 100000000 19.60 MB/s",
181 name: "allocatedBytesTest",
183 Name: "BenchmarkTest",
184 N: 100000000, AllocedBytesPerOp: 5,
185 Measured: AllocedBytesPerOp,
187 wanted: "BenchmarkTest 100000000 5 B/op",
192 Name: "BenchmarkTest",
193 N: 100000000, AllocsPerOp: 5,
194 Measured: AllocsPerOp,
196 wanted: "BenchmarkTest 100000000 5 allocs/op",
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)