1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:14:40 -07:00

testing: fix MB/s computation, documentation

R=rsc
CC=golang-dev
https://golang.org/cl/4529100
This commit is contained in:
Dmitriy Vyukov 2011-06-02 10:52:46 -04:00 committed by Russ Cox
parent d1bdff5448
commit 2653b4fbcc

View File

@ -150,7 +150,7 @@ func (b *B) run() BenchmarkResult {
type BenchmarkResult struct { type BenchmarkResult struct {
N int // The number of iterations. N int // The number of iterations.
Ns int64 // The total time taken. 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 { func (r BenchmarkResult) NsPerOp() int64 {
@ -160,13 +160,20 @@ func (r BenchmarkResult) NsPerOp() int64 {
return r.Ns / int64(r.N) return r.Ns / int64(r.N)
} }
func (r BenchmarkResult) String() string { func (r BenchmarkResult) mbPerSec() float64 {
ns := r.NsPerOp() if r.Bytes <= 0 || r.Ns <= 0 || r.N <= 0 {
mb := "" return 0
if ns > 0 && r.Bytes > 0 {
mb = fmt.Sprintf("\t%7.2f MB/s", (float64(r.Bytes)/1e6)/(float64(ns)/1e9))
} }
return fmt.Sprintf("%8d\t%10d ns/op%s", r.N, ns, mb) return float64(r.Bytes) * float64(r.N) / float64(r.Ns) * 1e3
}
func (r BenchmarkResult) String() string {
mbs := r.mbPerSec()
mb := ""
if mbs != 0 {
mb = fmt.Sprintf("\t%7.2f MB/s", mbs)
}
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 // An internal function but exported because it is cross-package; part of the implementation