| 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 mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/ |
| 6 | |
| 7 | import ( |
| 8 | "mathscip-go gomod github.com/golang/go/src go1.25.0 math/" |
| 9 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 10 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 12 | |
| 13 | "golang.org/x/tools/benchmark/parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/" |
| 14 | ) |
| 15 | |
| 16 | func TestDeltascip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/TestDelta().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 17 | cases := []struct { |
| 18 | before float64 |
| 19 | after float64 |
| 20 | mag float64 |
| 21 | f float64 |
| 22 | changed bool |
| 23 | pct string |
| 24 | mult string |
| 25 | }{ |
| 26 | {before: 1, after: 1, mag: 1, f: 1, changed: false, pct: "+0.00%", mult: "1.00x"}, |
| 27 | {before: 1, after: 2, mag: 0.5, f: 2, changed: true, pct: "+100.00%", mult: "2.00x"}, |
| 28 | {before: 2, after: 1, mag: 0.5, f: 0.5, changed: true, pct: "-50.00%", mult: "0.50x"}, |
| 29 | {before: 0, after: 0, mag: 1, f: 1, changed: false, pct: "+0.00%", mult: "1.00x"}, |
| 30 | {before: 1, after: 0, mag: mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Infscip-go gomod github.com/golang/go/src go1.25.0 math/Inf().(1), f: 0, changed: true, pct: "-100.00%", mult: "0.00x"}, |
| 31 | {before: 0, after: 1, mag: mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Infscip-go gomod github.com/golang/go/src go1.25.0 math/Inf().(1), f: mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Infscip-go gomod github.com/golang/go/src go1.25.0 math/Inf().(1), changed: true, pct: "+Inf%", mult: "+Infx"}, |
| 32 | } |
| 33 | for _, tt := range cases { |
| 34 | d := Deltascip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#{tt.before, tt.after} |
| 35 | if want, have := tt.mag, d.magscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#mag().(); want != have { |
| 36 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s.mag(): want %f have %f", d, want, have) |
| 37 | } |
| 38 | if want, have := tt.f, d.Float64scip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#Float64().(); want != have { |
| 39 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s.Float64(): want %f have %f", d, want, have) |
| 40 | } |
| 41 | if want, have := tt.changed, d.Changedscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#Changed().(); want != have { |
| 42 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s.Changed(): want %t have %t", d, want, have) |
| 43 | } |
| 44 | if want, have := tt.pct, d.Percentscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#Percent().(); want != have { |
| 45 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s.Percent(): want %q have %q", d, want, have) |
| 46 | } |
| 47 | if want, have := tt.mult, d.Multiplescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Delta#Multiple().(); want != have { |
| 48 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s.Multiple(): want %q have %q", d, want, have) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestCorrelatescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/TestCorrelate().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 54 | // Benches that are going to be successfully correlated get N thus: |
| 55 | // 0x<counter><num benches><b = before | a = after> |
| 56 | // Read this: "<counter> of <num benches>, from <before|after>". |
| 57 | before := parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Set#{ |
| 58 | "BenchmarkOneEach": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x11b}}, |
| 59 | "BenchmarkOneToNone": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneToNone"}}, |
| 60 | "BenchmarkOneToTwo": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneToTwo"}}, |
| 61 | "BenchmarkTwoToOne": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{ |
| 62 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoToOne"}, |
| 63 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoToOne"}, |
| 64 | }, |
| 65 | "BenchmarkTwoEach": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{ |
| 66 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x12b}, |
| 67 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x22b}, |
| 68 | }, |
| 69 | } |
| 70 | |
| 71 | after := parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Set#{ |
| 72 | "BenchmarkOneEach": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x11a}}, |
| 73 | "BenchmarkNoneToOne": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkNoneToOne"}}, |
| 74 | "BenchmarkTwoToOne": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoToOne"}}, |
| 75 | "BenchmarkOneToTwo": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{ |
| 76 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneToTwo"}, |
| 77 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkOneToTwo"}, |
| 78 | }, |
| 79 | "BenchmarkTwoEach": []*parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{ |
| 80 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x12a}, |
| 81 | {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkTwoEach", Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.: 0x22a}, |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | pairs, errs := Correlatescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/Correlate().(before, after) |
| 86 | |
| 87 | // Fail to match: BenchmarkOneToNone, BenchmarkOneToTwo, BenchmarkTwoToOne. |
| 88 | // Correlate does not notice BenchmarkNoneToOne. |
| 89 | if len(errs) != 3 { |
| 90 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Correlated expected 4 errors, got %d: %v", len(errs), errs) |
| 91 | } |
| 92 | |
| 93 | // Want three correlated pairs: one BenchmarkOneEach, two BenchmarkTwoEach. |
| 94 | if len(pairs) != 3 { |
| 95 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Correlated expected 3 pairs, got %v", pairs) |
| 96 | } |
| 97 | |
| 98 | for _, pair := range pairs { |
| 99 | if pair.Beforescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Before..Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.&0xF != 0xb { |
| 100 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected Before in pair %s", pair) |
| 101 | } |
| 102 | if pair.Afterscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#After..Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.&0xF != 0xa { |
| 103 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected After in pair %s", pair) |
| 104 | } |
| 105 | if pair.Beforescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Before..Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.>>4 != pair.Afterscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#After..Nscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#N.>>4 { |
| 106 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("mismatched pair %s", pair) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestBenchCmpSortingscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/TestBenchCmpSorting().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 112 | c := []BenchCmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#{ |
| 113 | {&parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkMuchFaster", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 10, Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord.: 3}, &parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkMuchFaster", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 1}}, |
| 114 | {&parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSameB", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 5, Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord.: 1}, &parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSameB", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 5}}, |
| 115 | {&parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSameA", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 5, Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord.: 2}, &parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSameA", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 5}}, |
| 116 | {&parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSlower", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 10, Ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Ord.: 0}, &parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/.Benchmarkscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#Name.: "BenchmarkSlower", NsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/benchmark/parse`/Benchmark#NsPerOp.: 11}}, |
| 117 | } |
| 118 | |
| 119 | // Test just one magnitude-based sort order; they are symmetric. |
| 120 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(ByDeltaNsPerOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/ByDeltaNsPerOp#(c)) |
| 121 | want := []string{"BenchmarkMuchFaster", "BenchmarkSlower", "BenchmarkSameA", "BenchmarkSameB"} |
| 122 | have := []string{c[0].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[1].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[2].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[3].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().()} |
| 123 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(want, have) { |
| 124 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ByDeltaNsOp incorrect sorting: want %v have %v", want, have) |
| 125 | } |
| 126 | |
| 127 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(ByParseOrderscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/ByParseOrder#(c)) |
| 128 | want = []string{"BenchmarkSlower", "BenchmarkSameB", "BenchmarkSameA", "BenchmarkMuchFaster"} |
| 129 | have = []string{c[0].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[1].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[2].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().(), c[3].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/benchcmp`/BenchCmp#Name().()} |
| 130 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(want, have) { |
| 131 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ByParseOrder incorrect sorting: want %v have %v", want, have) |
| 132 | } |
| 133 | } |