diff options
Diffstat (limited to 'libgo/go/testing')
-rw-r--r-- | libgo/go/testing/benchmark.go | 66 | ||||
-rw-r--r-- | libgo/go/testing/testing.go | 58 |
2 files changed, 99 insertions, 25 deletions
diff --git a/libgo/go/testing/benchmark.go b/libgo/go/testing/benchmark.go index 4129059..cb92fab 100644 --- a/libgo/go/testing/benchmark.go +++ b/libgo/go/testing/benchmark.go @@ -9,11 +9,19 @@ import ( "fmt" "os" "runtime" + "sync" "time" ) var matchBenchmarks = flag.String("test.bench", "", "regular expression to select benchmarks to run") -var benchTime = flag.Float64("test.benchtime", 1, "approximate run time for each benchmark, in seconds") +var benchTime = flag.Duration("test.benchtime", 1*time.Second, "approximate run time for each benchmark") +var benchmarkMemory = flag.Bool("test.benchmem", false, "print memory allocations for benchmarks") + +// Global lock to ensure only one benchmark runs at a time. +var benchmarkLock sync.Mutex + +// Used for every benchmark for measuring memory. +var memStats runtime.MemStats // An internal type but exported because it is cross-package; part of the implementation // of the "go test" command. @@ -31,6 +39,12 @@ type B struct { bytes int64 timerOn bool result BenchmarkResult + // The initial states of memStats.Mallocs and memStats.TotalAlloc. + startAllocs uint64 + startBytes uint64 + // The net total of this test after being run. + netAllocs uint64 + netBytes uint64 } // StartTimer starts timing a test. This function is called automatically @@ -38,6 +52,9 @@ type B struct { // a call to StopTimer. func (b *B) StartTimer() { if !b.timerOn { + runtime.ReadMemStats(&memStats) + b.startAllocs = memStats.Mallocs + b.startBytes = memStats.TotalAlloc b.start = time.Now() b.timerOn = true } @@ -49,6 +66,9 @@ func (b *B) StartTimer() { func (b *B) StopTimer() { if b.timerOn { b.duration += time.Now().Sub(b.start) + runtime.ReadMemStats(&memStats) + b.netAllocs += memStats.Mallocs - b.startAllocs + b.netBytes += memStats.TotalAlloc - b.startBytes b.timerOn = false } } @@ -57,9 +77,14 @@ func (b *B) StopTimer() { // It does not affect whether the timer is running. func (b *B) ResetTimer() { if b.timerOn { + runtime.ReadMemStats(&memStats) + b.startAllocs = memStats.Mallocs + b.startBytes = memStats.TotalAlloc b.start = time.Now() } b.duration = 0 + b.netAllocs = 0 + b.netBytes = 0 } // SetBytes records the number of bytes processed in a single operation. @@ -75,6 +100,8 @@ func (b *B) nsPerOp() int64 { // runN runs a single benchmark for the specified number of iterations. func (b *B) runN(n int) { + benchmarkLock.Lock() + defer benchmarkLock.Unlock() // Try to get a comparable environment for each run // by clearing garbage from previous runs. runtime.GC() @@ -151,7 +178,7 @@ func (b *B) launch() { b.runN(n) // Run the benchmark for at least the specified amount of time. - d := time.Duration(*benchTime * float64(time.Second)) + d := *benchTime for !b.failed && b.duration < d && n < 1e9 { last := n // Predict iterations/sec. @@ -168,14 +195,16 @@ func (b *B) launch() { n = roundUp(n) b.runN(n) } - b.result = BenchmarkResult{b.N, b.duration, b.bytes} + b.result = BenchmarkResult{b.N, b.duration, b.bytes, b.netAllocs, b.netBytes} } // The results of a benchmark run. type BenchmarkResult struct { - N int // The number of iterations. - T time.Duration // The total time taken. - Bytes int64 // Bytes processed in one iteration. + N int // The number of iterations. + T time.Duration // The total time taken. + Bytes int64 // Bytes processed in one iteration. + MemAllocs uint64 // The total number of memory allocations. + MemBytes uint64 // The total number of bytes allocated. } func (r BenchmarkResult) NsPerOp() int64 { @@ -192,6 +221,20 @@ func (r BenchmarkResult) mbPerSec() float64 { return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds() } +func (r BenchmarkResult) AllocsPerOp() int64 { + if r.N <= 0 { + return 0 + } + return int64(r.MemAllocs) / int64(r.N) +} + +func (r BenchmarkResult) AllocedBytesPerOp() int64 { + if r.N <= 0 { + return 0 + } + return int64(r.MemBytes) / int64(r.N) +} + func (r BenchmarkResult) String() string { mbs := r.mbPerSec() mb := "" @@ -212,6 +255,11 @@ func (r BenchmarkResult) String() string { return fmt.Sprintf("%8d\t%s%s", r.N, ns, mb) } +func (r BenchmarkResult) MemString() string { + return fmt.Sprintf("%8d B/op\t%8d allocs/op", + r.AllocedBytesPerOp(), r.AllocsPerOp()) +} + // An internal function but exported because it is cross-package; part of the implementation // of the "go test" command. func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) { @@ -249,7 +297,11 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ fmt.Printf("--- FAIL: %s\n%s", benchName, b.output) continue } - fmt.Printf("%v\n", r) + results := r.String() + if *benchmarkMemory { + results += "\t" + r.MemString() + } + fmt.Println(results) // 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 { diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index e56b77c..60edbd5 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -13,7 +13,10 @@ // Functions of the form // func BenchmarkXxx(*testing.B) // are considered benchmarks, and are executed by the "go test" command when -// the -test.bench flag is provided. +// the -test.bench flag is provided. Benchmarks are run sequentially. +// +// For a description of the testing flags, see +// http://golang.org/cmd/go/#Description_of_testing_flags. // // A sample benchmark function looks like this: // func BenchmarkHello(b *testing.B) { @@ -24,15 +27,14 @@ // // The benchmark package will vary b.N until the benchmark function lasts // long enough to be timed reliably. The output -// testing.BenchmarkHello 10000000 282 ns/op +// BenchmarkHello 10000000 282 ns/op // means that the loop ran 10000000 times at a speed of 282 ns per loop. // // If a benchmark needs some expensive setup before running, the timer -// may be stopped: +// may be reset: // func BenchmarkBigLen(b *testing.B) { -// b.StopTimer() // big := NewBig() -// b.StartTimer() +// b.ResetTimer() // for i := 0; i < b.N; i++ { // big.Len() // } @@ -100,14 +102,16 @@ var ( short = flag.Bool("test.short", false, "run smaller test suite to save time") // Report as tests are run; default is silent for success. - chatty = flag.Bool("test.v", false, "verbose: print additional output") - match = flag.String("test.run", "", "regular expression to select tests and examples to run") - memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution") - memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate") - cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") - timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests") - cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test") - parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") + chatty = flag.Bool("test.v", false, "verbose: print additional output") + match = flag.String("test.run", "", "regular expression to select tests and examples to run") + memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution") + memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate") + cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") + blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to the named file after execution") + blockProfileRate = flag.Int("test.blockprofilerate", 1, "if >= 0, calls runtime.SetBlockProfileRate()") + timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests") + cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test") + parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") haveExamples bool // are there examples? @@ -132,6 +136,11 @@ func Short() bool { return *short } +// Verbose reports whether the -test.v flag is set. +func Verbose() bool { + return *chatty +} + // decorate prefixes the string with the file and line of the call site // and inserts the final newline if needed and indentation tabs for formatting. func decorate(s string) string { @@ -151,6 +160,9 @@ func decorate(s string) string { fmt.Fprintf(buf, "%s:%d: ", file, line) lines := strings.Split(s, "\n") + if l := len(lines); l > 1 && lines[l-1] == "" { + lines = lines[:l-1] + } for i, line := range lines { if i > 0 { buf.WriteByte('\n') @@ -163,10 +175,7 @@ func decorate(s string) string { } buf.WriteString(line) } - if l := len(s); l > 0 && s[len(s)-1] != '\n' { - // Add final new line if needed. - buf.WriteByte('\n') - } + buf.WriteByte('\n') return buf.String() } @@ -413,7 +422,9 @@ func before() { } // Could save f so after can call f.Close; not worth the effort. } - + if *blockProfile != "" && *blockProfileRate >= 0 { + runtime.SetBlockProfileRate(*blockProfileRate) + } } // after runs after all testing. @@ -432,6 +443,17 @@ func after() { } f.Close() } + if *blockProfile != "" && *blockProfileRate >= 0 { + f, err := os.Create(*blockProfile) + if err != nil { + fmt.Fprintf(os.Stderr, "testing: %s", err) + return + } + if err = pprof.Lookup("block").WriteTo(f, 0); err != nil { + fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *blockProfile, err) + } + f.Close() + } } var timer *time.Timer |