-
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.
Merge pull request #161 from dnephin/time-and-aggregate
Add TestCase.Time
- Loading branch information
Showing
7 changed files
with
158 additions
and
124 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
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,71 @@ | ||
package aggregate | ||
|
||
import ( | ||
"sort" | ||
"time" | ||
|
||
"gotest.tools/gotestsum/testjson" | ||
) | ||
|
||
// Slowest returns a slice of all tests with an elapsed time greater than | ||
// threshold. The slice is sorted by Elapsed time in descending order (slowest | ||
// test first). | ||
// | ||
// If there are multiple runs of a TestCase, all of them will be represented | ||
// by a single TestCase with the median elapsed time in the returned slice. | ||
func Slowest(exec *testjson.Execution, threshold time.Duration) []testjson.TestCase { | ||
if threshold == 0 { | ||
return nil | ||
} | ||
pkgs := exec.Packages() | ||
tests := make([]testjson.TestCase, 0, len(pkgs)) | ||
for _, pkg := range pkgs { | ||
pkgTests := ByElapsed(exec.Package(pkg).TestCases(), median) | ||
tests = append(tests, pkgTests...) | ||
} | ||
sort.Slice(tests, func(i, j int) bool { | ||
return tests[i].Elapsed > tests[j].Elapsed | ||
}) | ||
end := sort.Search(len(tests), func(i int) bool { | ||
return tests[i].Elapsed < threshold | ||
}) | ||
return tests[:end] | ||
} | ||
|
||
// ByElapsed maps all test cases by name, and if there is more than one | ||
// instance of a TestCase, uses fn to select the elapsed time for the group. | ||
// | ||
// All cases are assumed to be part of the same package. | ||
func ByElapsed(cases []testjson.TestCase, fn func(times []time.Duration) time.Duration) []testjson.TestCase { | ||
if len(cases) <= 1 { | ||
return cases | ||
} | ||
pkg := cases[0].Package | ||
// nolint: prealloc // size is not predictable | ||
m := make(map[testjson.TestName][]time.Duration) | ||
for _, tc := range cases { | ||
m[tc.Test] = append(m[tc.Test], tc.Elapsed) | ||
} | ||
result := make([]testjson.TestCase, 0, len(m)) | ||
for name, timing := range m { | ||
result = append(result, testjson.TestCase{ | ||
Package: pkg, | ||
Test: name, | ||
Elapsed: fn(timing), | ||
}) | ||
} | ||
return result | ||
} | ||
|
||
func median(times []time.Duration) time.Duration { | ||
switch len(times) { | ||
case 0: | ||
return 0 | ||
case 1: | ||
return times[0] | ||
} | ||
sort.Slice(times, func(i, j int) bool { | ||
return times[i] < times[j] | ||
}) | ||
return times[len(times)/2] | ||
} |
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 aggregate | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"gotest.tools/gotestsum/testjson" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestByElapsed_WithMedian(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 := ByElapsed(cases, median) | ||
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.Name(), y.Test.Name()) == -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) | ||
}) | ||
} | ||
} |
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