| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const benchmarkRepeatsForSummary = 4 |
| 14 | |
| 15 | // Fixed baselines for the Scale column in the summary tables. |
| 16 | // Using constants makes the scale values comparable across runs/branches. |
| 17 | const ( |
| 18 | benchmarkScaleBaselineValidateLicensesNsOp = 5.0 |
| 19 | benchmarkScaleBaselineSatisfiesNsOp = 1500.0 |
| 20 | ) |
| 21 | |
| 22 | func TestMain(m *testing.M) { |
| 23 | // When TestMain is present, it's safest to explicitly parse flags before |
| 24 | // inspecting any -test.* settings. |
| 25 | if !flag.Parsed() { |
| 26 | flag.Parse() |
| 27 | } |
| 28 | |
| 29 | benchPattern := "" |
| 30 | benchFlag := flag.Lookup("test.bench") |
| 31 | if benchFlag != nil { |
| 32 | benchPattern = benchFlag.Value.String() |
| 33 | } |
| 34 | |
| 35 | shouldPrintBenchOutput := benchPattern != "" |
| 36 | if shouldPrintBenchOutput { |
| 37 | // Benchmarks are executed as part of summary table generation (via |
| 38 | // testing.Benchmark). Suppress the default go test benchmark execution so |
| 39 | // we don't run benchmarks twice in a single invocation. |
| 40 | if benchFlag != nil { |
| 41 | _ = benchFlag.Value.Set("$^") |
| 42 | } |
| 43 | |
| 44 | _, _ = fmt.Fprintln(os.Stdout, "Benchmark summary tables:") |
| 45 | _, _ = fmt.Fprintln(os.Stdout, "- ns/op average: average time per operation") |
| 46 | _, _ = fmt.Fprintln(os.Stdout, "- Scale: relative to a fixed baseline per table") |
| 47 | _, _ = fmt.Fprintln(os.Stdout, "") |
| 48 | } |
| 49 | |
| 50 | code := m.Run() |
| 51 | |
| 52 | if shouldPrintBenchOutput { |
| 53 | validateRows := withScaleColumn(computeValidateLicensesBenchmarkTableRows(benchmarkRepeatsForSummary), benchmarkScaleBaselineValidateLicensesNsOp) |
| 54 | printBenchmarkTable(os.Stdout, "Benchmark ValidateLicenses", validateRows, benchmarkScaleBaselineValidateLicensesNsOp) |
| 55 | |
| 56 | satisfiesRows := withScaleColumn(computeSatisfiesBenchmarkTableRows(benchmarkRepeatsForSummary), benchmarkScaleBaselineSatisfiesNsOp) |
| 57 | printBenchmarkTable(os.Stdout, "Benchmark Satisfies", satisfiesRows, benchmarkScaleBaselineSatisfiesNsOp) |
| 58 | } |
| 59 | |
| 60 | os.Exit(code) |
| 61 | } |
| 62 | |
| 63 | type benchmarkTableRow struct { |
| 64 | label string |
| 65 | nsOpAvg float64 |
| 66 | scale string |
| 67 | } |
| 68 | |
| 69 | func withScaleColumn(rows []benchmarkTableRow, benchmarkScaleBaselineNsOp float64) []benchmarkTableRow { |
| 70 | if len(rows) == 0 { |
| 71 | return rows |
| 72 | } |
| 73 | |
| 74 | for i := range rows { |
| 75 | rows[i].scale = formatScale(rows[i].nsOpAvg, benchmarkScaleBaselineNsOp) |
| 76 | } |
| 77 | return rows |
| 78 | } |
| 79 | |
| 80 | func formatScale(ns, baseline float64) string { |
| 81 | if ns <= 0 || baseline <= 0 { |
| 82 | return "n/a" |
| 83 | } |
| 84 | |
| 85 | ratio := ns / baseline |
| 86 | if ratio >= 0.95 && ratio <= 1.05 { |
| 87 | return "1x" |
| 88 | } |
| 89 | |
| 90 | if ratio >= 10 { |
| 91 | return fmt.Sprintf("~%sx", formatWithCommas(int64(math.Round(ratio)))) |
| 92 | } |
| 93 | |
| 94 | return fmt.Sprintf("~%.1fx", math.Round(ratio*10)/10) |
| 95 | } |
| 96 | |
| 97 | func runBenchmarkNsAvg(repeats int, fn func(b *testing.B)) float64 { |
| 98 | if repeats <= 0 { |
| 99 | repeats = 1 |
| 100 | } |
| 101 | |
| 102 | sum := 0.0 |
| 103 | count := 0 |
| 104 | for i := 0; i < repeats; i++ { |
| 105 | res := testing.Benchmark(fn) |
| 106 | if res.N <= 0 { |
| 107 | continue |
| 108 | } |
| 109 | ns := float64(res.T.Nanoseconds()) / float64(res.N) |
| 110 | sum += ns |
| 111 | count++ |
| 112 | } |
| 113 | if count == 0 { |
| 114 | return 0 |
| 115 | } |
| 116 | return sum / float64(count) |
| 117 | } |
| 118 | |
| 119 | func printBenchmarkTable(w *os.File, title string, rows []benchmarkTableRow, benchmarkScaleBaselineNsOp float64) { |
| 120 | header1 := title |
| 121 | header2 := "ns/op average" |
| 122 | header3 := fmt.Sprintf("Scale (%dns/op=1x)", int(benchmarkScaleBaselineNsOp)) |
| 123 | |
| 124 | col1 := len(header1) |
| 125 | for _, r := range rows { |
| 126 | if len(r.label) > col1 { |
| 127 | col1 = len(r.label) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | formatNsAvg := func(r benchmarkTableRow) string { |
| 132 | num := nsNumberString(r.nsOpAvg) |
| 133 | return fmt.Sprintf("~%s ns/op", num) |
| 134 | } |
| 135 | |
| 136 | col2 := len(header2) |
| 137 | for _, r := range rows { |
| 138 | if l := len(formatNsAvg(r)); l > col2 { |
| 139 | col2 = l |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | col3 := len(header3) |
| 144 | for _, r := range rows { |
| 145 | if len(r.scale) > col3 { |
| 146 | col3 = len(r.scale) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | line := func() { |
| 151 | _, _ = fmt.Fprintf(w, "+-%s-+-%s-+-%s-+\n", strings.Repeat("-", col1), strings.Repeat("-", col2), strings.Repeat("-", col3)) |
| 152 | } |
| 153 | row := func(c1, c2, c3 string) { |
| 154 | _, _ = fmt.Fprintf(w, "| %-*s | %-*s | %-*s |\n", col1, c1, col2, c2, col3, c3) |
| 155 | } |
| 156 | |
| 157 | line() |
| 158 | row(header1, header2, header3) |
| 159 | line() |
| 160 | for _, r := range rows { |
| 161 | ns := formatNsAvg(r) |
| 162 | row(r.label, ns, r.scale) |
| 163 | } |
| 164 | line() |
| 165 | _, _ = fmt.Fprintln(w, "") |
| 166 | } |
| 167 | |
| 168 | func nsNumberString(ns float64) string { |
| 169 | if ns <= 0 { |
| 170 | return "0" |
| 171 | } |
| 172 | if ns < float64(10*time.Microsecond.Nanoseconds()) { |
| 173 | return fmt.Sprintf("%.1f", ns) |
| 174 | } |
| 175 | return formatWithCommas(int64(ns + 0.5)) |
| 176 | } |
| 177 | |
| 178 | func formatWithCommas(n int64) string { |
| 179 | s := fmt.Sprintf("%d", n) |
| 180 | if len(s) <= 3 { |
| 181 | return s |
| 182 | } |
| 183 | |
| 184 | var b strings.Builder |
| 185 | pre := len(s) % 3 |
| 186 | if pre == 0 { |
| 187 | pre = 3 |
| 188 | } |
| 189 | b.WriteString(s[:pre]) |
| 190 | for i := pre; i < len(s); i += 3 { |
| 191 | b.WriteByte(',') |
| 192 | b.WriteString(s[i : i+3]) |
| 193 | } |
| 194 | return b.String() |
| 195 | } |