| 1 | package spdxexpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/ |
| 2 | |
| 3 | import ( |
| 4 | "flagscip-go gomod github.com/golang/go/src go1.24 flag/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/" |
| 6 | "mathscip-go gomod github.com/golang/go/src go1.24 math/" |
| 7 | "osscip-go gomod github.com/golang/go/src go1.24 os/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 9 | "testingscip-go gomod github.com/golang/go/src go1.24 testing/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.24 time/" |
| 11 | ) |
| 12 | |
| 13 | const benchmarkRepeatsForSummaryscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/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 | benchmarkScaleBaselineValidateLicensesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineValidateLicensesNsOp. = 5.0 |
| 19 | benchmarkScaleBaselineSatisfiesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineSatisfiesNsOp. = 1500.0 |
| 20 | ) |
| 21 | |
| 22 | func TestMainscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/TestMain().(m *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Mscip-go gomod github.com/golang/go/src go1.24 testing/M#) { |
| 23 | // When TestMain is present, it's safest to explicitly parse flags before |
| 24 | // inspecting any -test.* settings. |
| 25 | if !flagscip-go gomod github.com/golang/go/src go1.24 flag/.Parsedscip-go gomod github.com/golang/go/src go1.24 flag/Parsed().() { |
| 26 | flagscip-go gomod github.com/golang/go/src go1.24 flag/.Parsescip-go gomod github.com/golang/go/src go1.24 flag/Parse().() |
| 27 | } |
| 28 | |
| 29 | benchPattern := "" |
| 30 | benchFlag := flagscip-go gomod github.com/golang/go/src go1.24 flag/.Lookupscip-go gomod github.com/golang/go/src go1.24 flag/Lookup().("test.bench") |
| 31 | if benchFlag != nil { |
| 32 | benchPattern = benchFlag.Valuescip-go gomod github.com/golang/go/src go1.24 flag/Flag#Value..Stringscip-go gomod github.com/golang/go/src go1.24 flag/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.Valuescip-go gomod github.com/golang/go/src go1.24 flag/Flag#Value..Setscip-go gomod github.com/golang/go/src go1.24 flag/Value#Set().("$^") |
| 42 | } |
| 43 | |
| 44 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "Benchmark summary tables:") |
| 45 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "- ns/op average: average time per operation") |
| 46 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "- Scale: relative to a fixed baseline per table") |
| 47 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "") |
| 48 | } |
| 49 | |
| 50 | code := m.Runscip-go gomod github.com/golang/go/src go1.24 testing/M#Run().() |
| 51 | |
| 52 | if shouldPrintBenchOutput { |
| 53 | validateRows := withScaleColumnscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/withScaleColumn().(computeValidateLicensesBenchmarkTableRowsscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/computeValidateLicensesBenchmarkTableRows().(benchmarkRepeatsForSummaryscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkRepeatsForSummary.), benchmarkScaleBaselineValidateLicensesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineValidateLicensesNsOp.) |
| 54 | printBenchmarkTablescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/printBenchmarkTable().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "Benchmark ValidateLicenses", validateRows, benchmarkScaleBaselineValidateLicensesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineValidateLicensesNsOp.) |
| 55 | |
| 56 | satisfiesRows := withScaleColumnscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/withScaleColumn().(computeSatisfiesBenchmarkTableRowsscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/computeSatisfiesBenchmarkTableRows().(benchmarkRepeatsForSummaryscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkRepeatsForSummary.), benchmarkScaleBaselineSatisfiesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineSatisfiesNsOp.) |
| 57 | printBenchmarkTablescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/printBenchmarkTable().(osscip-go gomod github.com/golang/go/src go1.24 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24 os/Stdout., "Benchmark Satisfies", satisfiesRows, benchmarkScaleBaselineSatisfiesNsOpscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkScaleBaselineSatisfiesNsOp.) |
| 58 | } |
| 59 | |
| 60 | osscip-go gomod github.com/golang/go/src go1.24 os/.Exitscip-go gomod github.com/golang/go/src go1.24 os/Exit().(code) |
| 61 | } |
| 62 | |
| 63 | type benchmarkTableRowscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow# struct { |
| 64 | labelscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#label. string |
| 65 | nsOpAvgscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#nsOpAvg. float64 |
| 66 | scalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#scale. string |
| 67 | } |
| 68 | |
| 69 | func withScaleColumnscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/withScaleColumn().(rows []benchmarkTableRowscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#, benchmarkScaleBaselineNsOp float64) []benchmarkTableRowscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow# { |
| 70 | if len(rows) == 0 { |
| 71 | return rows |
| 72 | } |
| 73 | |
| 74 | for i := range rows { |
| 75 | rows[i].scalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#scale. = formatScalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/formatScale().(rows[i].nsOpAvgscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#nsOpAvg., benchmarkScaleBaselineNsOp) |
| 76 | } |
| 77 | return rows |
| 78 | } |
| 79 | |
| 80 | func formatScalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/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 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("~%sx", formatWithCommasscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/formatWithCommas().(int64(mathscip-go gomod github.com/golang/go/src go1.24 math/.Roundscip-go gomod github.com/golang/go/src go1.24 math/Round().(ratio)))) |
| 92 | } |
| 93 | |
| 94 | return fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("~%.1fx", mathscip-go gomod github.com/golang/go/src go1.24 math/.Roundscip-go gomod github.com/golang/go/src go1.24 math/Round().(ratio*10)/10) |
| 95 | } |
| 96 | |
| 97 | func runBenchmarkNsAvgscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/runBenchmarkNsAvg().(repeats int, fn func(b *testingscip-go gomod github.com/golang/go/src go1.24 testing/.Bscip-go gomod github.com/golang/go/src go1.24 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 := testingscip-go gomod github.com/golang/go/src go1.24 testing/.Benchmarkscip-go gomod github.com/golang/go/src go1.24 testing/Benchmark().(fn) |
| 106 | if res.Nscip-go gomod github.com/golang/go/src go1.24 testing/BenchmarkResult#N. <= 0 { |
| 107 | continue |
| 108 | } |
| 109 | ns := float64(res.Tscip-go gomod github.com/golang/go/src go1.24 testing/BenchmarkResult#T..Nanosecondsscip-go gomod github.com/golang/go/src go1.24 time/Duration#Nanoseconds().()) / float64(res.Nscip-go gomod github.com/golang/go/src go1.24 testing/BenchmarkResult#N.) |
| 110 | sum += ns |
| 111 | count++ |
| 112 | } |
| 113 | if count == 0 { |
| 114 | return 0 |
| 115 | } |
| 116 | return sum / float64(count) |
| 117 | } |
| 118 | |
| 119 | func printBenchmarkTablescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/printBenchmarkTable().(w *osscip-go gomod github.com/golang/go/src go1.24 os/.Filescip-go gomod github.com/golang/go/src go1.24 os/File#, title string, rows []benchmarkTableRowscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#, benchmarkScaleBaselineNsOp float64) { |
| 120 | header1 := title |
| 121 | header2 := "ns/op average" |
| 122 | header3 := fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("Scale (%dns/op=1x)", int(benchmarkScaleBaselineNsOp)) |
| 123 | |
| 124 | col1 := len(header1) |
| 125 | for _, r := range rows { |
| 126 | if len(r.labelscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#label.) > col1 { |
| 127 | col1 = len(r.labelscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#label.) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | formatNsAvg := func(r benchmarkTableRowscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#) string { |
| 132 | num := nsNumberStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/nsNumberString().(r.nsOpAvgscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#nsOpAvg.) |
| 133 | return fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 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.scalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#scale.) > col3 { |
| 146 | col3 = len(r.scalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#scale.) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | line := func() { |
| 151 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(w, "+-%s-+-%s-+-%s-+\n", stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Repeatscip-go gomod github.com/golang/go/src go1.24 strings/Repeat().("-", col1), stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Repeatscip-go gomod github.com/golang/go/src go1.24 strings/Repeat().("-", col2), stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Repeatscip-go gomod github.com/golang/go/src go1.24 strings/Repeat().("-", col3)) |
| 152 | } |
| 153 | row := func(c1, c2, c3 string) { |
| 154 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 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.labelscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#label., ns, r.scalescip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/benchmarkTableRow#scale.) |
| 163 | } |
| 164 | line() |
| 165 | _, _ = fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintln().(w, "") |
| 166 | } |
| 167 | |
| 168 | func nsNumberStringscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/nsNumberString().(ns float64) string { |
| 169 | if ns <= 0 { |
| 170 | return "0" |
| 171 | } |
| 172 | if ns < float64(10*timescip-go gomod github.com/golang/go/src go1.24 time/.Microsecondscip-go gomod github.com/golang/go/src go1.24 time/Microsecond..Nanosecondsscip-go gomod github.com/golang/go/src go1.24 time/Duration#Nanoseconds().()) { |
| 173 | return fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("%.1f", ns) |
| 174 | } |
| 175 | return formatWithCommasscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/formatWithCommas().(int64(ns + 0.5)) |
| 176 | } |
| 177 | |
| 178 | func formatWithCommasscip-go gomod github.com/github/go-spdx/v2 . `github.com/github/go-spdx/v2/spdxexp`/formatWithCommas().(n int64) string { |
| 179 | s := fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("%d", n) |
| 180 | if len(s) <= 3 { |
| 181 | return s |
| 182 | } |
| 183 | |
| 184 | var b stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Builderscip-go gomod github.com/golang/go/src go1.24 strings/Builder# |
| 185 | pre := len(s) % 3 |
| 186 | if pre == 0 { |
| 187 | pre = 3 |
| 188 | } |
| 189 | b.WriteStringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#WriteString().(s[:pre]) |
| 190 | for i := pre; i < len(s); i += 3 { |
| 191 | b.WriteBytescip-go gomod github.com/golang/go/src go1.24 strings/Builder#WriteByte().(',') |
| 192 | b.WriteStringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#WriteString().(s[i : i+3]) |
| 193 | } |
| 194 | return b.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 195 | } |