aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/testing
diff options
context:
space:
mode:
authorDan Kegel <[email protected]>2021-08-16 08:45:28 -0700
committerRon Evans <[email protected]>2022-01-07 10:39:30 +0100
commitee4e42ba1fea45a7a1516c7a9d7f93d32a514d15 (patch)
tree812a31f02fdb808c677f8394e264adc1b50c7855 /src/testing
parentc6678525a92d9a566a7cb6066d4d65602ab71bf6 (diff)
downloadtinygo-ee4e42ba1fea45a7a1516c7a9d7f93d32a514d15.tar.gz
tinygo-ee4e42ba1fea45a7a1516c7a9d7f93d32a514d15.zip
src/testing: support -bench option to run benchmarks matching the given pattern.
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/benchmark.go26
-rw-r--r--src/testing/testing.go45
2 files changed, 63 insertions, 8 deletions
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go
index 4647cfe93..88aec366f 100644
--- a/src/testing/benchmark.go
+++ b/src/testing/benchmark.go
@@ -7,6 +7,7 @@
package testing
import (
+ "fmt"
"time"
)
@@ -187,6 +188,30 @@ func (r BenchmarkResult) AllocedBytesPerOp() int64 {
return 0 // Dummy version to allow running e.g. golang.org/test/fibo.go
}
+func runBenchmarks(benchmarks []InternalBenchmark) bool {
+ if len(benchmarks) == 0 {
+ return true
+ }
+ main := &B{
+ common: common{
+ name: "Main",
+ },
+ benchTime: benchTime,
+ benchFunc: func(b *B) {
+ for _, Benchmark := range benchmarks {
+ if flagVerbose {
+ fmt.Printf("=== RUN %s\n", Benchmark.Name)
+ }
+ b.Run(Benchmark.Name, Benchmark.F)
+ fmt.Printf("--- Result: %d ns/op\n", b.result.NsPerOp())
+ }
+ },
+ }
+
+ main.runN(1)
+ return true
+}
+
// Run benchmarks f as a subbenchmark with the given name. It reports
// true if the subbenchmark succeeded.
//
@@ -248,4 +273,3 @@ func Benchmark(f func(b *B)) BenchmarkResult {
}
return b.result
}
-
diff --git a/src/testing/testing.go b/src/testing/testing.go
index fe1fb83d7..ee4056ebd 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -18,9 +18,10 @@ import (
// Testing flags.
var (
- flagVerbose bool
- flagShort bool
- flagRunRegexp string
+ flagVerbose bool
+ flagShort bool
+ flagRunRegexp string
+ flagBenchRegexp string
)
var initRan bool
@@ -35,6 +36,7 @@ func Init() {
flag.BoolVar(&flagVerbose, "test.v", false, "verbose: print additional output")
flag.BoolVar(&flagShort, "test.short", false, "short: run smaller test suite to save time")
flag.StringVar(&flagRunRegexp, "test.run", "", "run: regexp of tests to run")
+ flag.StringVar(&flagBenchRegexp, "test.bench", "", "run: regexp of benchmarks to run")
}
// common holds the elements common between T and B and
@@ -243,7 +245,8 @@ type InternalTest struct {
// M is a test suite.
type M struct {
// tests is a list of the test names to execute
- Tests []InternalTest
+ Tests []InternalTest
+ Benchmarks []InternalBenchmark
deps testDeps
}
@@ -275,8 +278,33 @@ func (m *M) Run() int {
m.Tests = filtered
}
+ if flagBenchRegexp != "" {
+ var filtered []InternalBenchmark
- if len(m.Tests) == 0 {
+ // pre-test the regexp; we don't want to bother logging one failure for every test name if the regexp is broken
+ if _, err := m.deps.MatchString(flagBenchRegexp, "some-test-name"); err != nil {
+ fmt.Println("testing: invalid regexp for -test.bench:", err.Error())
+ failures++
+ }
+
+ // filter the list of tests before we try to run them
+ for _, test := range m.Benchmarks {
+ // ignore the error; we already tested that the regexp compiles fine above
+ if match, _ := m.deps.MatchString(flagBenchRegexp, test.Name); match {
+ filtered = append(filtered, test)
+ }
+ }
+
+ m.Benchmarks = filtered
+ flagVerbose = true
+ if flagRunRegexp == "" {
+ m.Tests = []InternalTest{}
+ }
+ } else {
+ m.Benchmarks = []InternalBenchmark{}
+ }
+
+ if len(m.Tests) == 0 && len(m.Benchmarks) == 0 {
fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
}
@@ -307,6 +335,8 @@ func (m *M) Run() int {
}
}
+ runBenchmarks(m.Benchmarks)
+
if failures > 0 {
fmt.Println("FAIL")
} else {
@@ -358,8 +388,9 @@ type testDeps interface {
func MainStart(deps interface{}, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M {
Init()
return &M{
- Tests: tests,
- deps: deps.(testDeps),
+ Tests: tests,
+ Benchmarks: benchmarks,
+ deps: deps.(testDeps),
}
}