aboutsummaryrefslogtreecommitdiff
path: root/benchmark/parse/parse_test.go
blob: d96be63171337ed9d75632098f404654ed46bf7f (plain)
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
// 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 *Bench
		err  bool // expect an error
	}{
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op",
			want: &Bench{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6,
				Measured: NsOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s",
			want: &Bench{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6, MbS: 817.77,
				Measured: NsOp | MbS,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77",
			want: &Bench{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6,
				Measured: NsOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       5 allocs/op",
			want: &Bench{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6, MbS: 817.77, AllocsOp: 5,
				Measured: NsOp | MbS | AllocsOp,
			},
		},
		{
			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       3 B/op	       5 allocs/op",
			want: &Bench{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6, MbS: 817.77, BOp: 3, AllocsOp: 5,
				Measured: NsOp | MbS | BOp | AllocsOp,
			},
		},
		// 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: &Bench{
				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 TestParseBenchSet(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 := BenchSet{
		"BenchmarkReadRequestApachebench": []*Bench{
			{
				Name: "BenchmarkReadRequestApachebench",
				N:    1000000, NsOp: 2960, MbS: 27.70, BOp: 839, AllocsOp: 9,
				Measured: NsOp | MbS | BOp | AllocsOp,
				Ord:      2,
			},
		},
		"BenchmarkClientServerParallel64": []*Bench{
			{
				Name: "BenchmarkClientServerParallel64",
				N:    50000, NsOp: 59192, BOp: 7028, AllocsOp: 60,
				Measured: NsOp | BOp | AllocsOp,
				Ord:      3,
			},
		},
		"BenchmarkEncrypt": []*Bench{
			{
				Name: "BenchmarkEncrypt",
				N:    100000000, NsOp: 19.6,
				Measured: NsOp,
				Ord:      0,
			},
			{
				Name: "BenchmarkEncrypt",
				N:    5000000, NsOp: 517,
				Measured: NsOp,
				Ord:      1,
			},
		},
	}

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