diff options
author | Dan Kegel <[email protected]> | 2021-04-16 11:04:22 -0700 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-08-06 08:19:15 +0200 |
commit | 55789fd2c2e02b58c40f714347a1c93d61a02b5c (patch) | |
tree | 81428186eeed5dbd7ed7c6a0615b63966d7d264f /tests | |
parent | 478c592b131b21506e50ed1793b09689f0da456f (diff) | |
download | tinygo-55789fd2c2e02b58c40f714347a1c93d61a02b5c.tar.gz tinygo-55789fd2c2e02b58c40f714347a1c93d61a02b5c.zip |
src/testing/benchmark.go: add subset implementation of Benchmark
Partially fixes #1808
Allows the following to succeed:
curl "https://golang.org/test/fibo.go?m=text" > fibo.go
tinygo build -o fibo fibo.go
./fibo -bench
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tinygotest/benchmark_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/tinygotest/benchmark_test.go b/tests/tinygotest/benchmark_test.go new file mode 100644 index 000000000..c045c975b --- /dev/null +++ b/tests/tinygotest/benchmark_test.go @@ -0,0 +1,50 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package testbench + +import ( + "testing" +) + +var buf = make([]byte, 13579) + +func NonASCII(b []byte, i int, offset int) int { + for i = offset; i < len(b) + offset; i++ { + if b[i % len(b)] >= 0x80 { + break + } + } + return i +} + +func BenchmarkFastNonASCII(b *testing.B) { + var val int + for i := 0; i < b.N; i++ { + val += NonASCII(buf, 0, 0) + } +} + +func BenchmarkSlowNonASCII(b *testing.B) { + var val int + for i := 0; i < b.N; i++ { + val += NonASCII(buf, 0, 0) + val += NonASCII(buf, 0, 1) + } +} + +// TestBenchmark simply uses Benchmark twice and makes sure it does not crash. +func TestBenchmark(t *testing.T) { + // FIXME: reduce runtime from the current 3 seconds. + rslow := testing.Benchmark(BenchmarkSlowNonASCII) + rfast := testing.Benchmark(BenchmarkFastNonASCII) + tslow := rslow.NsPerOp() + tfast := rfast.NsPerOp() + + // Be exceedingly forgiving; do not fail even if system gets busy. + speedup := float64(tslow) / float64(tfast) + if speedup < 0.3 { + t.Errorf("Expected speedup >= 0.3, got %f", speedup) + } +} |