diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-13 05:11:45 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-13 05:11:45 +0000 |
commit | df4aa89a5e7acb315655f193e7f549e8d32367e2 (patch) | |
tree | eb5eccc07097c5fcf940967f33ab84a7d47c96fe /libgo/go/testing/benchmark.go | |
parent | f83fa0bf8f411697ec908cfa86ee6faf4cd9c476 (diff) | |
download | gcc-df4aa89a5e7acb315655f193e7f549e8d32367e2.zip gcc-df4aa89a5e7acb315655f193e7f549e8d32367e2.tar.gz gcc-df4aa89a5e7acb315655f193e7f549e8d32367e2.tar.bz2 |
libgo: Update to weekly.2011-12-22.
From-SVN: r183150
Diffstat (limited to 'libgo/go/testing/benchmark.go')
-rw-r--r-- | libgo/go/testing/benchmark.go | 71 |
1 files changed, 60 insertions, 11 deletions
diff --git a/libgo/go/testing/benchmark.go b/libgo/go/testing/benchmark.go index e81e5c5..4ce6370 100644 --- a/libgo/go/testing/benchmark.go +++ b/libgo/go/testing/benchmark.go @@ -25,12 +25,12 @@ type InternalBenchmark struct { // B is a type passed to Benchmark functions to manage benchmark // timing and to specify the number of iterations to run. type B struct { + common N int benchmark InternalBenchmark - ns time.Duration bytes int64 - start time.Time timerOn bool + result BenchmarkResult } // StartTimer starts timing a test. This function is called automatically @@ -48,7 +48,7 @@ func (b *B) StartTimer() { // want to measure. func (b *B) StopTimer() { if b.timerOn { - b.ns += time.Now().Sub(b.start) + b.duration += time.Now().Sub(b.start) b.timerOn = false } } @@ -59,7 +59,7 @@ func (b *B) ResetTimer() { if b.timerOn { b.start = time.Now() } - b.ns = 0 + b.duration = 0 } // SetBytes records the number of bytes processed in a single operation. @@ -70,7 +70,7 @@ func (b *B) nsPerOp() int64 { if b.N <= 0 { return 0 } - return b.ns.Nanoseconds() / int64(b.N) + return b.duration.Nanoseconds() / int64(b.N) } // runN runs a single benchmark for the specified number of iterations. @@ -127,17 +127,25 @@ func roundUp(n int) int { return 10 * base } -// run times the benchmark function. It gradually increases the number +// run times the benchmark function in a separate goroutine. +func (b *B) run() BenchmarkResult { + go b.launch() + <-b.signal + return b.result +} + +// launch launches the benchmark function. It gradually increases the number // of benchmark iterations until the benchmark runs for a second in order // to get a reasonable measurement. It prints timing information in this form // testing.BenchmarkHello 100000 19 ns/op -func (b *B) run() BenchmarkResult { +// launch is run by the fun function as a separate goroutine. +func (b *B) launch() { // Run the benchmark for a single iteration in case it's expensive. n := 1 b.runN(n) // Run the benchmark for at least the specified amount of time. d := time.Duration(*benchTime * float64(time.Second)) - for b.ns < d && n < 1e9 { + for !b.failed && b.duration < d && n < 1e9 { last := n // Predict iterations/sec. if b.nsPerOp() == 0 { @@ -153,7 +161,8 @@ func (b *B) run() BenchmarkResult { n = roundUp(n) b.runN(n) } - return BenchmarkResult{b.N, b.ns, b.bytes} + b.result = BenchmarkResult{b.N, b.duration, b.bytes} + b.signal <- b } // The results of a benchmark run. @@ -215,14 +224,32 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ } for _, procs := range cpuList { runtime.GOMAXPROCS(procs) - b := &B{benchmark: Benchmark} + b := &B{ + common: common{ + signal: make(chan interface{}), + }, + benchmark: Benchmark, + } benchName := Benchmark.Name if procs != 1 { benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs) } fmt.Printf("%s\t", benchName) r := b.run() + if b.failed { + // The output could be very long here, but probably isn't. + // We print it all, regardless, because we don't want to trim the reason + // the benchmark failed. + fmt.Printf("--- FAIL: %s\n%s", benchName, b.output) + continue + } fmt.Printf("%v\n", r) + // Unlike with tests, we ignore the -chatty flag and always print output for + // benchmarks since the output generation time will skew the results. + if len(b.output) > 0 { + b.trimOutput() + fmt.Printf("--- BENCH: %s\n%s", benchName, b.output) + } if p := runtime.GOMAXPROCS(-1); p != procs { fmt.Fprintf(os.Stderr, "testing: %s left GOMAXPROCS set to %d\n", benchName, p) } @@ -230,9 +257,31 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ } } +// trimOutput shortens the output from a benchmark, which can be very long. +func (b *B) trimOutput() { + // The output is likely to appear multiple times because the benchmark + // is run multiple times, but at least it will be seen. This is not a big deal + // because benchmarks rarely print, but just in case, we trim it if it's too long. + const maxNewlines = 10 + for nlCount, j := 0, 0; j < len(b.output); j++ { + if b.output[j] == '\n' { + nlCount++ + if nlCount >= maxNewlines { + b.output = append(b.output[:j], "\n\t... [output truncated]\n"...) + break + } + } + } +} + // Benchmark benchmarks a single function. Useful for creating // custom benchmarks that do not use gotest. func Benchmark(f func(b *B)) BenchmarkResult { - b := &B{benchmark: InternalBenchmark{"", f}} + b := &B{ + common: common{ + signal: make(chan interface{}), + }, + benchmark: InternalBenchmark{"", f}, + } return b.run() } |