github.com/jgbaldwinbrown/perf@v0.1.1/benchstat/sort_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package benchstat 6 7 import ( 8 "log" 9 "os" 10 "runtime" 11 "sort" 12 "testing" 13 ) 14 15 var file1 = "../cmd/benchstat/testdata/old.txt" 16 var file2 = "../cmd/benchstat/testdata/new.txt" 17 18 func extractRowBenchmark(row *Row) string { 19 return row.Benchmark 20 } 21 func extractRowDelta(row *Row) float64 { 22 return row.PctDelta 23 } 24 func extractRowChange(row *Row) int { 25 return row.Change 26 } 27 28 func benchmarkSortTest(t *testing.T, sampleTable *Table) { 29 numRows := len(sampleTable.Rows) 30 benchmarks := make([]string, numRows) 31 Sort(sampleTable, ByName) 32 for idx, row := range sampleTable.Rows { 33 benchmarks[idx] = extractRowBenchmark(row) 34 } 35 t.Run("BenchSorted", func(t *testing.T) { 36 if !sort.StringsAreSorted(benchmarks) { 37 t.Error("Table not sorted by names") 38 } 39 }) 40 Sort(sampleTable, Reverse(ByName)) 41 for idx, row := range sampleTable.Rows { 42 benchmarks[numRows-idx-1] = extractRowBenchmark(row) 43 } 44 t.Run("BenchReversed", func(t *testing.T) { 45 if !sort.StringsAreSorted(benchmarks) { 46 t.Error("Table not reverse sorted by benchmarks") 47 } 48 }) 49 } 50 51 func deltaSortTest(t *testing.T, sampleTable *Table) { 52 numRows := len(sampleTable.Rows) 53 deltas := make([]float64, numRows) 54 Sort(sampleTable, ByDelta) 55 for idx, row := range sampleTable.Rows { 56 deltas[idx] = -extractRowDelta(row) 57 } 58 t.Run("DeltaSorted", func(t *testing.T) { 59 if !sort.Float64sAreSorted(deltas) { 60 t.Errorf("Table not sorted by deltas: %v", deltas) 61 } 62 }) 63 Sort(sampleTable, Reverse(ByDelta)) 64 for idx, row := range sampleTable.Rows { 65 deltas[idx] = extractRowDelta(row) 66 } 67 t.Run("DeltaReversed", func(t *testing.T) { 68 if !sort.Float64sAreSorted(deltas) { 69 t.Error("Table not reverse sorted by deltas") 70 } 71 }) 72 } 73 74 func TestCompareCollection(t *testing.T) { 75 if runtime.GOOS == "android" { 76 t.Skipf("files not available on GOOS=%s", runtime.GOOS) 77 } 78 sampleCompareCollection := Collection{Alpha: 0.05, AddGeoMean: false, DeltaTest: UTest} 79 file1Data, err := os.ReadFile(file1) 80 if err != nil { 81 log.Fatal(err) 82 } 83 file2Data, err := os.ReadFile(file2) 84 if err != nil { 85 log.Fatal(err) 86 } 87 sampleCompareCollection.AddConfig(file1, file1Data) 88 sampleCompareCollection.AddConfig(file2, file2Data) 89 // data has both time and speed tables, test only the speed table 90 sampleTable := sampleCompareCollection.Tables()[0] 91 t.Run("BenchmarkSort", func(t *testing.T) { 92 benchmarkSortTest(t, sampleTable) 93 }) 94 t.Run("DeltaSort", func(t *testing.T) { 95 deltaSortTest(t, sampleTable) 96 }) 97 } 98 99 func TestSingleCollection(t *testing.T) { 100 if runtime.GOOS == "android" { 101 t.Skipf("files not available on GOOS=%s", runtime.GOOS) 102 } 103 sampleCollection := Collection{Alpha: 0.05, AddGeoMean: false, DeltaTest: UTest} 104 file1Data, err1 := os.ReadFile(file1) 105 if err1 != nil { 106 log.Fatal(err1) 107 } 108 sampleCollection.AddConfig(file1, file1Data) 109 sampleTable := sampleCollection.Tables()[0] 110 t.Run("BenchmarkSort", func(t *testing.T) { 111 benchmarkSortTest(t, sampleTable) 112 }) 113 }