GoPLS Viewer

Home|gopls/benchmark/parse/parse_test.go
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
5package parse
6
7import (
8    "reflect"
9    "strings"
10    "testing"
11)
12
13func 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:    100000000NsPerOp19.6,
24                MeasuredNsPerOp,
25            },
26        },
27        {
28            line"BenchmarkEncrypt    100000000            19.6 ns/op     817.77 MB/s",
29            want: &Benchmark{
30                Name"BenchmarkEncrypt",
31                N:    100000000NsPerOp19.6MBPerS817.77,
32                MeasuredNsPerOp | MBPerS,
33            },
34        },
35        {
36            line"BenchmarkEncrypt    100000000            19.6 ns/op     817.77",
37            want: &Benchmark{
38                Name"BenchmarkEncrypt",
39                N:    100000000NsPerOp19.6,
40                MeasuredNsPerOp,
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:    100000000NsPerOp19.6MBPerS817.77AllocsPerOp5,
48                MeasuredNsPerOp | 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:    100000000NsPerOp19.6MBPerS817.77AllocedBytesPerOp3AllocsPerOp5,
56                MeasuredNsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
57            },
58        },
59        // error handling cases
60        {
61            line"BenchPress    100            19.6 ns/op"// non-benchmark
62            errtrue,
63        },
64        {
65            line"BenchmarkEncrypt    lots            19.6 ns/op"// non-int iterations
66            errtrue,
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        haveerr := 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(havett.want) {
88            t.Errorf("parsed line %q incorrectly, want %v have %v"tt.linett.wanthave)
89        }
90    }
91}
92
93func 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:    1000000NsPerOp2960MBPerS27.70AllocedBytesPerOp839AllocsPerOp9,
119                MeasuredNsPerOp | MBPerS | AllocedBytesPerOp | AllocsPerOp,
120                Ord:      2,
121            },
122        },
123        "BenchmarkClientServerParallel64": []*Benchmark{
124            {
125                Name"BenchmarkClientServerParallel64",
126                N:    50000NsPerOp59192AllocedBytesPerOp7028AllocsPerOp60,
127                MeasuredNsPerOp | AllocedBytesPerOp | AllocsPerOp,
128                Ord:      3,
129            },
130        },
131        "BenchmarkEncrypt": []*Benchmark{
132            {
133                Name"BenchmarkEncrypt",
134                N:    100000000NsPerOp19.6,
135                MeasuredNsPerOp,
136                Ord:      0,
137            },
138            {
139                Name"BenchmarkEncrypt",
140                N:    5000000NsPerOp517,
141                MeasuredNsPerOp,
142                Ord:      1,
143            },
144        },
145    }
146
147    haveerr := ParseSet(strings.NewReader(in))
148    if err != nil {
149        t.Fatalf("unexpected err during ParseSet: %v"err)
150    }
151    if !reflect.DeepEqual(wanthave) {
152        t.Errorf("parsed bench set incorrectly, want %v have %v"wanthave)
153    }
154}
155
156func 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:    100000000NsPerOp19.6,
167                MeasuredNsPerOp,
168            },
169            wanted"BenchmarkTest 100000000 19.60 ns/op",
170        },
171        {
172            name"mbTest",
173            input: &Benchmark{
174                Name"BenchmarkTest",
175                N:    100000000MBPerS19.6,
176                MeasuredMBPerS,
177            },
178            wanted"BenchmarkTest 100000000 19.60 MB/s",
179        },
180        {
181            name"allocatedBytesTest",
182            input: &Benchmark{
183                Name"BenchmarkTest",
184                N:    100000000AllocedBytesPerOp5,
185                MeasuredAllocedBytesPerOp,
186            },
187            wanted"BenchmarkTest 100000000 5 B/op",
188        },
189        {
190            name"allocsTest",
191            input: &Benchmark{
192                Name"BenchmarkTest",
193                N:    100000000AllocsPerOp5,
194                MeasuredAllocsPerOp,
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.wantedresult)
205            }
206        })
207    }
208}
209
MembersX
reflect
TestParseLine
TestParseLine.t
TestParseSet.want
TestString
TestString.RangeStmt_5016.tt
testing
TestParseLine.cases
TestParseLine.RangeStmt_1951.tt
TestParseSet
TestString.tests
TestParseLine.RangeStmt_1951.BlockStmt.err
TestParseSet.in
TestParseLine.RangeStmt_1951.BlockStmt.have
TestParseSet.t
TestParseSet.have
TestParseSet.err
TestString.t
TestString.RangeStmt_5016.BlockStmt.BlockStmt.result
Members
X