parser/gotest: Refactor benchmark grouping

The only reason groupBenchmarksByName needed the reportBuilder receiver
was to access its output collector. By passing the output explicitly to
the groupBenchmarksByName function we can remove the reportBuilder
dependency.
This commit is contained in:
Joël Stemmer 2022-08-15 22:05:38 +01:00
parent b73e4a9ed5
commit 2af321a697
2 changed files with 14 additions and 8 deletions

View File

@ -258,7 +258,7 @@ func (b *reportBuilder) CreatePackage(name, result string, duration time.Duratio
continue continue
} }
} }
tests = b.groupBenchmarksByName(tests) tests = groupBenchmarksByName(tests, b.output)
pkg.Coverage = b.coverage pkg.Coverage = b.coverage
pkg.Output = b.output.Get(globalID) pkg.Output = b.output.Get(globalID)
@ -345,7 +345,7 @@ func parseResult(r string) gtr.Result {
} }
} }
func (b *reportBuilder) groupBenchmarksByName(tests []gtr.Test) []gtr.Test { func groupBenchmarksByName(tests []gtr.Test, output *collector.Output) []gtr.Test {
if len(tests) == 0 { if len(tests) == 0 {
return nil return nil
} }
@ -392,7 +392,7 @@ func (b *reportBuilder) groupBenchmarksByName(tests []gtr.Test) []gtr.Test {
group.Duration = combinedDuration(byName[group.Name]) group.Duration = combinedDuration(byName[group.Name])
group.Result = groupResults(byName[group.Name]) group.Result = groupResults(byName[group.Name])
group.Output = b.output.GetAll(ids...) group.Output = output.GetAll(ids...)
if count > 0 { if count > 0 {
total.Iterations /= int64(count) total.Iterations /= int64(count)
total.NsPerOp /= float64(count) total.NsPerOp /= float64(count)

View File

@ -1,10 +1,12 @@
package gotest package gotest
import ( import (
"fmt"
"testing" "testing"
"time" "time"
"github.com/jstemmer/go-junit-report/v2/gtr" "github.com/jstemmer/go-junit-report/v2/gtr"
"github.com/jstemmer/go-junit-report/v2/parser/gotest/internal/collector"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
) )
@ -236,6 +238,11 @@ func TestSubtestModes(t *testing.T) {
} }
func TestGroupBenchmarksByName(t *testing.T) { func TestGroupBenchmarksByName(t *testing.T) {
output := collector.New()
for i := 1; i <= 4; i++ {
output.AppendToID(i, fmt.Sprintf("output-%d", i))
}
tests := []struct { tests := []struct {
name string name string
in []gtr.Test in []gtr.Test
@ -245,7 +252,7 @@ func TestGroupBenchmarksByName(t *testing.T) {
{ {
"one failing benchmark", "one failing benchmark",
[]gtr.Test{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail, Data: map[string]interface{}{}}}, []gtr.Test{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail, Data: map[string]interface{}{}}},
[]gtr.Test{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail, Data: map[string]interface{}{}}}, []gtr.Test{{ID: 1, Name: "BenchmarkFailed", Result: gtr.Fail, Output: []string{"output-1"}, Data: map[string]interface{}{}}},
}, },
{ {
"four passing benchmarks", "four passing benchmarks",
@ -256,7 +263,7 @@ func TestGroupBenchmarksByName(t *testing.T) {
{ID: 4, Name: "BenchmarkOne", Result: gtr.Pass, Data: map[string]interface{}{key: Benchmark{NsPerOp: 40, MBPerSec: 100, BytesPerOp: 5, AllocsPerOp: 2}}}, {ID: 4, Name: "BenchmarkOne", Result: gtr.Pass, Data: map[string]interface{}{key: Benchmark{NsPerOp: 40, MBPerSec: 100, BytesPerOp: 5, AllocsPerOp: 2}}},
}, },
[]gtr.Test{ []gtr.Test{
{ID: 1, Name: "BenchmarkOne", Result: gtr.Pass, Data: map[string]interface{}{key: Benchmark{NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 4}}}, {ID: 1, Name: "BenchmarkOne", Result: gtr.Pass, Output: []string{"output-1", "output-2", "output-3", "output-4"}, Data: map[string]interface{}{key: Benchmark{NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 4}}},
}, },
}, },
{ {
@ -268,15 +275,14 @@ func TestGroupBenchmarksByName(t *testing.T) {
{ID: 4, Name: "BenchmarkMixed", Result: gtr.Fail}, {ID: 4, Name: "BenchmarkMixed", Result: gtr.Fail},
}, },
[]gtr.Test{ []gtr.Test{
{ID: 1, Name: "BenchmarkMixed", Result: gtr.Fail, Data: map[string]interface{}{key: Benchmark{NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 3}}}, {ID: 1, Name: "BenchmarkMixed", Result: gtr.Fail, Output: []string{"output-1", "output-2", "output-3", "output-4"}, Data: map[string]interface{}{key: Benchmark{NsPerOp: 25, MBPerSec: 250, BytesPerOp: 2, AllocsPerOp: 3}}},
}, },
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
b := newReportBuilder() got := groupBenchmarksByName(test.in, output)
got := b.groupBenchmarksByName(test.in)
if diff := cmp.Diff(test.want, got); diff != "" { if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("groupBenchmarksByName result incorrect, diff (-want, +got):\n%s\n", diff) t.Errorf("groupBenchmarksByName result incorrect, diff (-want, +got):\n%s\n", diff)
} }