-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aggregate elapsed time when there are multiple runs
A test case may have multiple runs if -count=x is used, or when the input contains the test2json output from multiple test runs. Using the median time should exclude some outliers.
- Loading branch information
Showing
2 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package slowest | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"gotest.tools/gotestsum/testjson" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestAggregateTestCases(t *testing.T) { | ||
cases := []testjson.TestCase{ | ||
{Test: "TestOne", Package: "pkg", Elapsed: time.Second}, | ||
{Test: "TestTwo", Package: "pkg", Elapsed: 2 * time.Second}, | ||
{Test: "TestOne", Package: "pkg", Elapsed: 3 * time.Second}, | ||
{Test: "TestTwo", Package: "pkg", Elapsed: 4 * time.Second}, | ||
{Test: "TestOne", Package: "pkg", Elapsed: 5 * time.Second}, | ||
{Test: "TestTwo", Package: "pkg", Elapsed: 6 * time.Second}, | ||
} | ||
actual := aggregateTestCases(cases) | ||
expected := []testjson.TestCase{ | ||
{Test: "TestOne", Package: "pkg", Elapsed: 3 * time.Second}, | ||
{Test: "TestTwo", Package: "pkg", Elapsed: 4 * time.Second}, | ||
} | ||
assert.DeepEqual(t, actual, expected, | ||
cmpopts.SortSlices(func(x, y testjson.TestCase) bool { | ||
return strings.Compare(x.Test, y.Test) == -1 | ||
}), | ||
cmpopts.IgnoreUnexported(testjson.TestCase{})) | ||
} | ||
|
||
func TestMedian(t *testing.T) { | ||
var testcases = []struct { | ||
name string | ||
times []time.Duration | ||
expected time.Duration | ||
}{ | ||
{ | ||
name: "one item slice", | ||
times: []time.Duration{time.Minute}, | ||
expected: time.Minute, | ||
}, | ||
{ | ||
name: "odd number of items", | ||
times: []time.Duration{time.Millisecond, time.Hour, time.Second}, | ||
expected: time.Second, | ||
}, | ||
{ | ||
name: "even number of items", | ||
times: []time.Duration{time.Second, time.Millisecond, time.Microsecond, time.Hour}, | ||
expected: time.Second, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := median(tc.times) | ||
assert.Equal(t, actual, tc.expected) | ||
}) | ||
} | ||
} |