File: parse_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,588 kB
  • sloc: javascript: 2,011; asm: 1,458; sh: 174; yacc: 155; makefile: 21; ansic: 17
file content (208 lines) | stat: -rw-r--r-- 5,226 bytes parent folder | download | duplicates (2)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2014 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 parse

import (
	"reflect"
	"strings"
	"testing"
)

func TestParseLine(t *testing.T) {
	cases := []struct {
		line string
		want *Benchmark
		err  bool // expect an error
	}{
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op",
			want: &Benchmark{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6,
				Measured: NsPerOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s",
			want: &Benchmark{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6, MBPerS: 817.77,
				Measured: NsPerOp | MBPerS,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77",
			want: &Benchmark{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6,
				Measured: NsPerOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       5 allocs/op",
			want: &Benchmark{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocsPerOp: 5,
				Measured: NsPerOp | MBPerS | AllocsPerOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       3 B/op	       5 allocs/op",
			want: &Benchmark{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6, MBPerS: 817.77, AllocedBytesPerOp: 3, AllocsPerOp: 5,
				Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
			},
		},
		// error handling cases
		{
			line: "BenchPress	100	        19.6 ns/op", // non-benchmark
			err: true,
		},
		{
			line: "BenchmarkEncrypt	lots	        19.6 ns/op", // non-int iterations
			err: true,
		},
		{
			line: "BenchmarkBridge	100000000	        19.6 smoots", // unknown unit
			want: &Benchmark{
				Name: "BenchmarkBridge",
				N:    100000000,
			},
		},
		{
			line: "PASS",
			err:  true,
		},
	}

	for _, tt := range cases {
		have, err := ParseLine(tt.line)
		if tt.err && err == nil {
			t.Errorf("parsing line %q should have failed", tt.line)
			continue
		}
		if !reflect.DeepEqual(have, tt.want) {
			t.Errorf("parsed line %q incorrectly, want %v have %v", tt.line, tt.want, have)
		}
	}
}

func TestParseSet(t *testing.T) {
	// Test two things:
	// 1. The noise that can accompany testing.B output gets ignored.
	// 2. Benchmarks with the same name have their order preserved.
	in := `
		?   	crypto	[no test files]
		PASS
				pem_decrypt_test.go:17: test 4. %!s(x509.PEMCipher=5)
			... [output truncated]

		BenchmarkEncrypt	100000000	        19.6 ns/op
		BenchmarkEncrypt	 5000000	       517 ns/op
		=== RUN TestChunk
		--- PASS: TestChunk (0.00 seconds)
		--- SKIP: TestLinuxSendfile (0.00 seconds)
			fs_test.go:716: skipping; linux-only test
		BenchmarkReadRequestApachebench	 1000000	      2960 ns/op	  27.70 MB/s	     839 B/op	       9 allocs/op
		BenchmarkClientServerParallel64	   50000	     59192 ns/op	    7028 B/op	      60 allocs/op
		ok  	net/http	95.783s
	`

	want := Set{
		"BenchmarkReadRequestApachebench": []*Benchmark{
			{
				Name: "BenchmarkReadRequestApachebench",
				N:    1000000, NsPerOp: 2960, MBPerS: 27.70, AllocedBytesPerOp: 839, AllocsPerOp: 9,
				Measured: NsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
				Ord:      2,
			},
		},
		"BenchmarkClientServerParallel64": []*Benchmark{
			{
				Name: "BenchmarkClientServerParallel64",
				N:    50000, NsPerOp: 59192, AllocedBytesPerOp: 7028, AllocsPerOp: 60,
				Measured: NsPerOp | AllocedBytesPerOp | AllocsPerOp,
				Ord:      3,
			},
		},
		"BenchmarkEncrypt": []*Benchmark{
			{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsPerOp: 19.6,
				Measured: NsPerOp,
				Ord:      0,
			},
			{
				Name: "BenchmarkEncrypt",
				N:    5000000, NsPerOp: 517,
				Measured: NsPerOp,
				Ord:      1,
			},
		},
	}

	have, err := ParseSet(strings.NewReader(in))
	if err != nil {
		t.Fatalf("unexpected err during ParseSet: %v", err)
	}
	if !reflect.DeepEqual(want, have) {
		t.Errorf("parsed bench set incorrectly, want %v have %v", want, have)
	}
}

func TestString(t *testing.T) {
	tests := []struct {
		name   string
		input  *Benchmark
		wanted string
	}{
		{
			name: "nsTest",
			input: &Benchmark{
				Name: "BenchmarkTest",
				N:    100000000, NsPerOp: 19.6,
				Measured: NsPerOp,
			},
			wanted: "BenchmarkTest 100000000 19.60 ns/op",
		},
		{
			name: "mbTest",
			input: &Benchmark{
				Name: "BenchmarkTest",
				N:    100000000, MBPerS: 19.6,
				Measured: MBPerS,
			},
			wanted: "BenchmarkTest 100000000 19.60 MB/s",
		},
		{
			name: "allocatedBytesTest",
			input: &Benchmark{
				Name: "BenchmarkTest",
				N:    100000000, AllocedBytesPerOp: 5,
				Measured: AllocedBytesPerOp,
			},
			wanted: "BenchmarkTest 100000000 5 B/op",
		},
		{
			name: "allocsTest",
			input: &Benchmark{
				Name: "BenchmarkTest",
				N:    100000000, AllocsPerOp: 5,
				Measured: AllocsPerOp,
			},
			wanted: "BenchmarkTest 100000000 5 allocs/op",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := tt.input.String()
			if result != tt.wanted {
				t.Errorf("String() is called, want %q, have %q", tt.wanted, result)
			}
		})
	}
}