summaryrefslogtreecommitdiff
path: root/src/pkg/testing/benchmark.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/testing/benchmark.go')
-rw-r--r--src/pkg/testing/benchmark.go26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/pkg/testing/benchmark.go b/src/pkg/testing/benchmark.go
index cf73e2b48..f8b53e63a 100644
--- a/src/pkg/testing/benchmark.go
+++ b/src/pkg/testing/benchmark.go
@@ -143,14 +143,13 @@ func (b *B) run() BenchmarkResult {
b.runN(n)
}
return BenchmarkResult{b.N, b.ns, b.bytes}
-
}
// The results of a benchmark run.
type BenchmarkResult struct {
N int // The number of iterations.
Ns int64 // The total time taken.
- Bytes int64 // The total number of bytes processed.
+ Bytes int64 // Bytes processed in one iteration.
}
func (r BenchmarkResult) NsPerOp() int64 {
@@ -160,13 +159,20 @@ func (r BenchmarkResult) NsPerOp() int64 {
return r.Ns / int64(r.N)
}
+func (r BenchmarkResult) mbPerSec() float64 {
+ if r.Bytes <= 0 || r.Ns <= 0 || r.N <= 0 {
+ return 0
+ }
+ return float64(r.Bytes) * float64(r.N) / float64(r.Ns) * 1e3
+}
+
func (r BenchmarkResult) String() string {
- ns := r.NsPerOp()
+ mbs := r.mbPerSec()
mb := ""
- if ns > 0 && r.Bytes > 0 {
- mb = fmt.Sprintf("\t%7.2f MB/s", (float64(r.Bytes)/1e6)/(float64(ns)/1e9))
+ if mbs != 0 {
+ mb = fmt.Sprintf("\t%7.2f MB/s", mbs)
}
- return fmt.Sprintf("%8d\t%10d ns/op%s", r.N, ns, mb)
+ return fmt.Sprintf("%8d\t%10d ns/op%s", r.N, r.NsPerOp(), mb)
}
// An internal function but exported because it is cross-package; part of the implementation
@@ -176,6 +182,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmark
if len(*matchBenchmarks) == 0 {
return
}
+ procs := runtime.GOMAXPROCS(-1)
for _, Benchmark := range benchmarks {
matched, err := matchString(*matchBenchmarks, Benchmark.Name)
if err != nil {
@@ -187,7 +194,12 @@ func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmark
}
b := &B{benchmark: Benchmark}
r := b.run()
- fmt.Printf("%s\t%v\n", Benchmark.Name, r)
+ print(fmt.Sprintf("%s\t%v\n", Benchmark.Name, r))
+ if p := runtime.GOMAXPROCS(-1); p != procs {
+ print(fmt.Sprintf("%s left GOMAXPROCS set to %d\n", Benchmark.Name, p))
+ procs = p
+ }
+
}
}