aboutsummaryrefslogtreecommitdiff
path: root/cmd/benchcmp/compare_test.go
blob: 73b3cce7608cc5860a5fb00c95a5029ce3b3cdd2 (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
// 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 main

import (
	"math"
	"reflect"
	"sort"
	"testing"

	"golang.org/x/tools/benchmark/parse"
)

func TestDelta(t *testing.T) {
	cases := []struct {
		before  float64
		after   float64
		mag     float64
		f       float64
		changed bool
		pct     string
		mult    string
	}{
		{before: 1, after: 1, mag: 1, f: 1, changed: false, pct: "+0.00%", mult: "1.00x"},
		{before: 1, after: 2, mag: 0.5, f: 2, changed: true, pct: "+100.00%", mult: "2.00x"},
		{before: 2, after: 1, mag: 0.5, f: 0.5, changed: true, pct: "-50.00%", mult: "0.50x"},
		{before: 0, after: 0, mag: 1, f: 1, changed: false, pct: "+0.00%", mult: "1.00x"},
		{before: 1, after: 0, mag: math.Inf(1), f: 0, changed: true, pct: "-100.00%", mult: "0.00x"},
		{before: 0, after: 1, mag: math.Inf(1), f: math.Inf(1), changed: true, pct: "+Inf%", mult: "+Infx"},
	}
	for _, tt := range cases {
		d := Delta{tt.before, tt.after}
		if want, have := tt.mag, d.mag(); want != have {
			t.Errorf("%s.mag(): want %f have %f", d, want, have)
		}
		if want, have := tt.f, d.Float64(); want != have {
			t.Errorf("%s.Float64(): want %f have %f", d, want, have)
		}
		if want, have := tt.changed, d.Changed(); want != have {
			t.Errorf("%s.Changed(): want %t have %t", d, want, have)
		}
		if want, have := tt.pct, d.Percent(); want != have {
			t.Errorf("%s.Percent(): want %q have %q", d, want, have)
		}
		if want, have := tt.mult, d.Multiple(); want != have {
			t.Errorf("%s.Multiple(): want %q have %q", d, want, have)
		}
	}
}

func TestCorrelate(t *testing.T) {
	// Benches that are going to be successfully correlated get N thus:
	//   0x<counter><num benches><b = before | a = after>
	// Read this: "<counter> of <num benches>, from <before|after>".
	before := parse.BenchSet{
		"BenchmarkOneEach":   []*parse.Bench{{Name: "BenchmarkOneEach", N: 0x11b}},
		"BenchmarkOneToNone": []*parse.Bench{{Name: "BenchmarkOneToNone"}},
		"BenchmarkOneToTwo":  []*parse.Bench{{Name: "BenchmarkOneToTwo"}},
		"BenchmarkTwoToOne": []*parse.Bench{
			{Name: "BenchmarkTwoToOne"},
			{Name: "BenchmarkTwoToOne"},
		},
		"BenchmarkTwoEach": []*parse.Bench{
			{Name: "BenchmarkTwoEach", N: 0x12b},
			{Name: "BenchmarkTwoEach", N: 0x22b},
		},
	}

	after := parse.BenchSet{
		"BenchmarkOneEach":   []*parse.Bench{{Name: "BenchmarkOneEach", N: 0x11a}},
		"BenchmarkNoneToOne": []*parse.Bench{{Name: "BenchmarkNoneToOne"}},
		"BenchmarkTwoToOne":  []*parse.Bench{{Name: "BenchmarkTwoToOne"}},
		"BenchmarkOneToTwo": []*parse.Bench{
			{Name: "BenchmarkOneToTwo"},
			{Name: "BenchmarkOneToTwo"},
		},
		"BenchmarkTwoEach": []*parse.Bench{
			{Name: "BenchmarkTwoEach", N: 0x12a},
			{Name: "BenchmarkTwoEach", N: 0x22a},
		},
	}

	pairs, errs := Correlate(before, after)

	// Fail to match: BenchmarkOneToNone, BenchmarkOneToTwo, BenchmarkTwoToOne.
	// Correlate does not notice BenchmarkNoneToOne.
	if len(errs) != 3 {
		t.Errorf("Correlated expected 4 errors, got %d: %v", len(errs), errs)
	}

	// Want three correlated pairs: one BenchmarkOneEach, two BenchmarkTwoEach.
	if len(pairs) != 3 {
		t.Fatalf("Correlated expected 3 pairs, got %v", pairs)
	}

	for _, pair := range pairs {
		if pair.Before.N&0xF != 0xb {
			t.Errorf("unexpected Before in pair %s", pair)
		}
		if pair.After.N&0xF != 0xa {
			t.Errorf("unexpected After in pair %s", pair)
		}
		if pair.Before.N>>4 != pair.After.N>>4 {
			t.Errorf("mismatched pair %s", pair)
		}
	}
}

func TestBenchCmpSorting(t *testing.T) {
	c := []BenchCmp{
		{&parse.Bench{Name: "BenchmarkMuchFaster", NsOp: 10, Ord: 3}, &parse.Bench{Name: "BenchmarkMuchFaster", NsOp: 1}},
		{&parse.Bench{Name: "BenchmarkSameB", NsOp: 5, Ord: 1}, &parse.Bench{Name: "BenchmarkSameB", NsOp: 5}},
		{&parse.Bench{Name: "BenchmarkSameA", NsOp: 5, Ord: 2}, &parse.Bench{Name: "BenchmarkSameA", NsOp: 5}},
		{&parse.Bench{Name: "BenchmarkSlower", NsOp: 10, Ord: 0}, &parse.Bench{Name: "BenchmarkSlower", NsOp: 11}},
	}

	// Test just one magnitude-based sort order; they are symmetric.
	sort.Sort(ByDeltaNsOp(c))
	want := []string{"BenchmarkMuchFaster", "BenchmarkSlower", "BenchmarkSameA", "BenchmarkSameB"}
	have := []string{c[0].Name(), c[1].Name(), c[2].Name(), c[3].Name()}
	if !reflect.DeepEqual(want, have) {
		t.Errorf("ByDeltaNsOp incorrect sorting: want %v have %v", want, have)
	}

	sort.Sort(ByParseOrder(c))
	want = []string{"BenchmarkSlower", "BenchmarkSameB", "BenchmarkSameA", "BenchmarkMuchFaster"}
	have = []string{c[0].Name(), c[1].Name(), c[2].Name(), c[3].Name()}
	if !reflect.DeepEqual(want, have) {
		t.Errorf("ByParseOrder incorrect sorting: want %v have %v", want, have)
	}
}