Skip to content

Commit 6f6ccdf

Browse files
authored
feat: add -compare option (#10)
* feat: add -compare option * test: fix arguments
1 parent ede8cf4 commit 6f6ccdf

File tree

4 files changed

+110
-12
lines changed

4 files changed

+110
-12
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [Show only benchmarks with worse score](#show-only-benchmarks-with-worse-score)
3030
- [Specify a threshold](#specify-a-threshold)
3131
- [Specify a base commit compared with HEAD](#specify-a-base-commit-compared-with-head)
32+
- [Compare only memory allocation](#compare-only-memory-allocation)
3233
- [Usage](#usage)
3334
- [Q&A](#qa)
3435
- [A result of benchmarks is unstable](#a-result-of-benchmarks-is-unstable)
@@ -172,7 +173,6 @@ Comparison
172173
+-----------------------------+---------+-------------------+
173174
174175
2020/01/12 17:48:39 This commit makes benchmarks worse
175-
176176
```
177177

178178
</details>
@@ -192,6 +192,51 @@ By default, `cob` uses `HEAD~1`. If you compare benchmarks with different commit
192192
$ cob --base origin/master ./...
193193
```
194194

195+
## Compare only memory allocation
196+
You can use `-compare` option.
197+
198+
```
199+
$ cob -compare B/op
200+
```
201+
202+
<details>
203+
<summary>Result</summary>
204+
205+
```
206+
2020/01/15 14:46:31 Run Benchmark: 4363944cbed3da7a8245cbcdc8d8240b8976eb24 HEAD~1
207+
2020/01/15 14:46:33 Run Benchmark: 599a5523729d4d99a331b9d3f71dde9e1e6daef0 HEAD
208+
209+
Result
210+
======
211+
212+
+-----------------------------+----------+---------------+-------------------+
213+
| Name | Commit | NsPerOp | AllocedBytesPerOp |
214+
+-----------------------------+----------+---------------+-------------------+
215+
| BenchmarkAppend_Allocate-16 | HEAD | 179.00 ns/op | 121 B/op |
216+
+ +----------+---------------+-------------------+
217+
| | HEAD@{1} | 104.00 ns/op | 23 B/op |
218+
+-----------------------------+----------+---------------+-------------------+
219+
| BenchmarkCall-16 | HEAD | 0.50 ns/op | 0 B/op |
220+
+ +----------+---------------+ +
221+
| | HEAD@{1} | 0.49 ns/op | |
222+
+-----------------------------+----------+---------------+-------------------+
223+
224+
Comparison
225+
==========
226+
227+
+-----------------------------+---------+-------------------+
228+
| Name | NsPerOp | AllocedBytesPerOp |
229+
+-----------------------------+---------+-------------------+
230+
| BenchmarkAppend_Allocate-16 | - | 426.09% |
231+
+-----------------------------+---------+-------------------+
232+
| BenchmarkCall-16 | - | 0.00% |
233+
+-----------------------------+---------+-------------------+
234+
235+
2020/01/15 14:46:35 This commit makes benchmarks worse
236+
```
237+
238+
</details>
239+
195240
# Usage
196241

197242
```
@@ -208,6 +253,7 @@ GLOBAL OPTIONS:
208253
--only-degression Show only benchmarks with worse score (default: false)
209254
--threshold value The program fails if the benchmark gets worse than the threshold (default: 0.2)
210255
--base value Specify a base commit compared with HEAD (default: "HEAD~1")
256+
--compare value Which score to compare (default: "ns/op,B/op")
211257
--bench-cmd value Specify a command to measure benchmarks (default: "go")
212258
--bench-args value Specify arguments passed to -cmd (default: "test -run '^$' -bench . -benchmem ./...")
213259
--help, -h show help (default: false)

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type config struct {
1010
onlyDegression bool
1111
threshold float64
1212
base string
13+
compare []string
1314
benchCmd string
1415
benchArgs []string
1516
}
@@ -19,6 +20,7 @@ func newConfig(c *cli.Context) config {
1920
onlyDegression: c.Bool("only-degression"),
2021
threshold: c.Float64("threshold"),
2122
base: c.String("base"),
23+
compare: strings.Split(c.String("compare"), ","),
2224
benchCmd: c.String("bench-cmd"),
2325
benchArgs: strings.Fields(c.String("bench-args")),
2426
}

main.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type result struct {
2525
RatioAllocedBytesPerOp float64
2626
}
2727

28+
type comparedScore struct {
29+
nsPerOp bool
30+
allocedBytesPerOp bool
31+
}
32+
2833
func main() {
2934
app := &cli.App{
3035
Name: "cob",
@@ -47,6 +52,11 @@ func main() {
4752
Usage: "Specify a base commit compared with HEAD",
4853
Value: "HEAD~1",
4954
},
55+
&cli.StringFlag{
56+
Name: "compare",
57+
Usage: "Which score to compare",
58+
Value: "ns/op,B/op",
59+
},
5060
&cli.StringFlag{
5161
Name: "bench-cmd",
5262
Usage: "Specify a command to measure benchmarks",
@@ -159,7 +169,7 @@ func run(c config) error {
159169
showResult(os.Stdout, rows)
160170
}
161171

162-
degression := showRatio(os.Stdout, ratios, c.threshold, c.onlyDegression)
172+
degression := showRatio(os.Stdout, ratios, c.threshold, whichScoreToCompare(c.compare), c.onlyDegression)
163173
if degression {
164174
return xerrors.New("This commit makes benchmarks worse")
165175
}
@@ -201,7 +211,7 @@ func showResult(w io.Writer, rows [][]string) {
201211
table.Render()
202212
}
203213

204-
func showRatio(w io.Writer, results []result, threshold float64, onlyDegression bool) bool {
214+
func showRatio(w io.Writer, results []result, threshold float64, comparedScore comparedScore, onlyDegression bool) bool {
205215
table := tablewriter.NewWriter(w)
206216
table.SetAutoFormatHeaders(false)
207217
table.SetAlignment(tablewriter.ALIGN_CENTER)
@@ -211,17 +221,25 @@ func showRatio(w io.Writer, results []result, threshold float64, onlyDegression
211221

212222
var degression bool
213223
for _, result := range results {
214-
if threshold < result.RatioNsPerOp || threshold < result.RatioAllocedBytesPerOp {
224+
if comparedScore.nsPerOp && threshold < result.RatioNsPerOp {
225+
degression = true
226+
} else if comparedScore.allocedBytesPerOp && threshold < result.RatioAllocedBytesPerOp {
215227
degression = true
216228
} else {
217229
if onlyDegression {
218230
continue
219231
}
220232
}
221233
row := []string{result.Name, generateRatioItem(result.RatioNsPerOp), generateRatioItem(result.RatioAllocedBytesPerOp)}
222-
colors := []tablewriter.Colors{{}}
223-
colors = append(colors, generateColor(result.RatioNsPerOp))
224-
colors = append(colors, generateColor(result.RatioAllocedBytesPerOp))
234+
colors := []tablewriter.Colors{{}, generateColor(result.RatioNsPerOp), generateColor(result.RatioAllocedBytesPerOp)}
235+
if !comparedScore.nsPerOp {
236+
row[1] = "-"
237+
colors[1] = tablewriter.Colors{}
238+
}
239+
if !comparedScore.allocedBytesPerOp {
240+
row[2] = "-"
241+
colors[2] = tablewriter.Colors{}
242+
}
225243
table.Rich(row, colors)
226244
}
227245
if table.NumLines() > 0 {
@@ -250,3 +268,18 @@ func generateColor(ratio float64) tablewriter.Colors {
250268
}
251269
return tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlueColor}
252270
}
271+
272+
func whichScoreToCompare(c []string) comparedScore {
273+
var comparedScore comparedScore
274+
for _, cc := range c {
275+
switch cc {
276+
case "ns/op":
277+
fmt.Println("cpu")
278+
comparedScore.nsPerOp = true
279+
case "B/op":
280+
fmt.Println("memory")
281+
comparedScore.allocedBytesPerOp = true
282+
}
283+
}
284+
return comparedScore
285+
}

main_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func Test_showRatio(t *testing.T) {
6161
type args struct {
6262
results []result
6363
threshold float64
64+
compare comparedScore
6465
onlyDegression bool
6566
}
6667
tests := []struct {
@@ -79,7 +80,11 @@ func Test_showRatio(t *testing.T) {
7980
RatioAllocedBytesPerOp: 0.5,
8081
},
8182
},
82-
threshold: 0.2,
83+
threshold: 0.2,
84+
compare: comparedScore{
85+
nsPerOp: true,
86+
allocedBytesPerOp: true,
87+
},
8388
onlyDegression: false,
8489
},
8590
want: true,
@@ -110,7 +115,11 @@ Comparison
110115
RatioAllocedBytesPerOp: 0.5,
111116
},
112117
},
113-
threshold: 0.95,
118+
threshold: 0.95,
119+
compare: comparedScore{
120+
nsPerOp: true,
121+
allocedBytesPerOp: true,
122+
},
114123
onlyDegression: false,
115124
},
116125
want: false,
@@ -143,7 +152,11 @@ Comparison
143152
RatioAllocedBytesPerOp: 0.5,
144153
},
145154
},
146-
threshold: 0.4,
155+
threshold: 0.4,
156+
compare: comparedScore{
157+
nsPerOp: true,
158+
allocedBytesPerOp: true,
159+
},
147160
onlyDegression: true,
148161
},
149162
want: true,
@@ -174,7 +187,11 @@ Comparison
174187
RatioAllocedBytesPerOp: 0.5,
175188
},
176189
},
177-
threshold: 0.6,
190+
threshold: 0.6,
191+
compare: comparedScore{
192+
nsPerOp: true,
193+
allocedBytesPerOp: true,
194+
},
178195
onlyDegression: true,
179196
},
180197
want: false,
@@ -184,7 +201,7 @@ Comparison
184201
for _, tt := range tests {
185202
t.Run(tt.name, func(t *testing.T) {
186203
w := &bytes.Buffer{}
187-
got := showRatio(w, tt.args.results, tt.args.threshold, tt.args.onlyDegression)
204+
got := showRatio(w, tt.args.results, tt.args.threshold, tt.args.compare, tt.args.onlyDegression)
188205
gotTable := w.String()
189206
assert.Equal(t, tt.wantTable, gotTable, tt.name)
190207
assert.Equal(t, tt.want, got, tt.name)

0 commit comments

Comments
 (0)