mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-05 05:00:15 -05:00
junit: Improve benchmark grouping and add tests
This commit is contained in:
parent
125573d6fc
commit
969fc17b96
@ -265,8 +265,11 @@ func formatOutput(output []string, indent int) string {
|
||||
}
|
||||
|
||||
func groupBenchmarksByName(benchmarks []gtr.Benchmark) []gtr.Benchmark {
|
||||
var grouped []gtr.Benchmark
|
||||
if len(benchmarks) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var grouped []gtr.Benchmark
|
||||
benchmap := make(map[string][]gtr.Benchmark)
|
||||
for _, bm := range benchmarks {
|
||||
if _, ok := benchmap[bm.Name]; !ok {
|
||||
@ -276,19 +279,40 @@ func groupBenchmarksByName(benchmarks []gtr.Benchmark) []gtr.Benchmark {
|
||||
}
|
||||
|
||||
for i, bm := range grouped {
|
||||
n := 0
|
||||
for _, b := range benchmap[bm.Name] {
|
||||
if b.Result != gtr.Pass {
|
||||
continue
|
||||
}
|
||||
bm.Iterations += b.Iterations
|
||||
bm.NsPerOp += b.NsPerOp
|
||||
bm.MBPerSec += b.MBPerSec
|
||||
bm.BytesPerOp += b.BytesPerOp
|
||||
bm.AllocsPerOp += b.AllocsPerOp
|
||||
}
|
||||
n := len(benchmap[bm.Name])
|
||||
grouped[i] = benchmap[bm.Name][0]
|
||||
grouped[i].NsPerOp = bm.NsPerOp / float64(n)
|
||||
grouped[i].MBPerSec = bm.MBPerSec / float64(n)
|
||||
grouped[i].BytesPerOp = bm.BytesPerOp / int64(n)
|
||||
grouped[i].AllocsPerOp = bm.AllocsPerOp / int64(n)
|
||||
n++
|
||||
}
|
||||
|
||||
bm.Result = groupResults(benchmap[bm.Name])
|
||||
if n > 0 {
|
||||
bm.NsPerOp = bm.NsPerOp / float64(n)
|
||||
bm.MBPerSec = bm.MBPerSec / float64(n)
|
||||
bm.BytesPerOp = bm.BytesPerOp / int64(n)
|
||||
bm.AllocsPerOp = bm.AllocsPerOp / int64(n)
|
||||
}
|
||||
grouped[i] = bm
|
||||
}
|
||||
return grouped
|
||||
}
|
||||
|
||||
func groupResults(benchmarks []gtr.Benchmark) gtr.Result {
|
||||
var result gtr.Result
|
||||
for _, bm := range benchmarks {
|
||||
if bm.Result == gtr.Fail {
|
||||
return gtr.Fail
|
||||
}
|
||||
if result != gtr.Pass {
|
||||
result = bm.Result
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
@ -115,3 +115,45 @@ func properties(keyvals ...string) *[]Property {
|
||||
}
|
||||
return &props
|
||||
}
|
||||
|
||||
func TestGroupBenchmarksByName(t *testing.T) {
|
||||
tests := []struct {
|
||||
in []gtr.Benchmark
|
||||
want []gtr.Benchmark
|
||||
}{
|
||||
{nil, nil},
|
||||
{
|
||||
[]gtr.Benchmark{{Name: "BenchmarkFailed", Result: gtr.Fail}},
|
||||
[]gtr.Benchmark{{Name: "BenchmarkFailed", Result: gtr.Fail}},
|
||||
},
|
||||
{
|
||||
[]gtr.Benchmark{
|
||||
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2},
|
||||
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 20, MBPerSec: 300, BytesPerOp: 1, AllocsPerOp: 4},
|
||||
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 30, MBPerSec: 200, BytesPerOp: 1, AllocsPerOp: 8},
|
||||
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 5, AllocsPerOp: 2},
|
||||
},
|
||||
[]gtr.Benchmark{
|
||||
{Name: "BenchmarkOne", Result: gtr.Pass, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 4},
|
||||
},
|
||||
},
|
||||
{
|
||||
[]gtr.Benchmark{
|
||||
{Name: "BenchmarkMixed", Result: gtr.Unknown},
|
||||
{Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 10, MBPerSec: 400, BytesPerOp: 1, AllocsPerOp: 2},
|
||||
{Name: "BenchmarkMixed", Result: gtr.Pass, NsPerOp: 40, MBPerSec: 100, BytesPerOp: 3, AllocsPerOp: 4},
|
||||
{Name: "BenchmarkMixed", Result: gtr.Fail},
|
||||
},
|
||||
[]gtr.Benchmark{
|
||||
{Name: "BenchmarkMixed", Result: gtr.Fail, NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 3},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got := groupBenchmarksByName(test.in)
|
||||
if diff := cmp.Diff(got, test.want); diff != "" {
|
||||
t.Errorf("groupBenchmarksByName result incorrect, diff (-want, +got):\n%s\n", diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user