Commit 1e095b76 authored by Shenghou Ma's avatar Shenghou Ma

testing: introduce (*B).ReportAllocs()

Calling it will show memory allocation statistics for that
single benchmark (if -test.benchmem is not provided)

R=golang-dev, rsc, kevlar, bradfitz
CC=golang-dev
https://golang.org/cl/7027046
parent f5958c61
...@@ -382,15 +382,9 @@ func BenchmarkParser(b *testing.B) { ...@@ -382,15 +382,9 @@ func BenchmarkParser(b *testing.B) {
} }
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
runtime.GC() runtime.GC()
var ms runtime.MemStats b.ReportAllocs()
runtime.ReadMemStats(&ms)
mallocs := ms.Mallocs
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Parse(bytes.NewBuffer(buf)) Parse(bytes.NewBuffer(buf))
} }
b.StopTimer()
runtime.ReadMemStats(&ms)
mallocs = ms.Mallocs - mallocs
b.Logf("%d iterations, %d mallocs per iteration\n", b.N, int(mallocs)/b.N)
} }
...@@ -634,9 +634,7 @@ func benchmarkTokenizer(b *testing.B, level int) { ...@@ -634,9 +634,7 @@ func benchmarkTokenizer(b *testing.B, level int) {
} }
b.SetBytes(int64(len(buf))) b.SetBytes(int64(len(buf)))
runtime.GC() runtime.GC()
var ms runtime.MemStats b.ReportAllocs()
runtime.ReadMemStats(&ms)
mallocs := ms.Mallocs
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
z := NewTokenizer(bytes.NewBuffer(buf)) z := NewTokenizer(bytes.NewBuffer(buf))
...@@ -674,10 +672,6 @@ func benchmarkTokenizer(b *testing.B, level int) { ...@@ -674,10 +672,6 @@ func benchmarkTokenizer(b *testing.B, level int) {
} }
} }
} }
b.StopTimer()
runtime.ReadMemStats(&ms)
mallocs = ms.Mallocs - mallocs
b.Logf("%d iterations, %d mallocs per iteration\n", b.N, int(mallocs)/b.N)
} }
func BenchmarkRawLevelTokenizer(b *testing.B) { benchmarkTokenizer(b, rawLevel) } func BenchmarkRawLevelTokenizer(b *testing.B) { benchmarkTokenizer(b, rawLevel) }
......
...@@ -34,11 +34,12 @@ type InternalBenchmark struct { ...@@ -34,11 +34,12 @@ type InternalBenchmark struct {
// timing and to specify the number of iterations to run. // timing and to specify the number of iterations to run.
type B struct { type B struct {
common common
N int N int
benchmark InternalBenchmark benchmark InternalBenchmark
bytes int64 bytes int64
timerOn bool timerOn bool
result BenchmarkResult showAllocResult bool
result BenchmarkResult
// The initial states of memStats.Mallocs and memStats.TotalAlloc. // The initial states of memStats.Mallocs and memStats.TotalAlloc.
startAllocs uint64 startAllocs uint64
startBytes uint64 startBytes uint64
...@@ -91,6 +92,13 @@ func (b *B) ResetTimer() { ...@@ -91,6 +92,13 @@ func (b *B) ResetTimer() {
// If this is called, the benchmark will report ns/op and MB/s. // If this is called, the benchmark will report ns/op and MB/s.
func (b *B) SetBytes(n int64) { b.bytes = n } func (b *B) SetBytes(n int64) { b.bytes = n }
// ReportAllocs enables malloc statistics for this benchmark.
// It is equivalent to setting -test.benchmem, but it only affects the
// benchmark function that calls ReportAllocs.
func (b *B) ReportAllocs() {
b.showAllocResult = true
}
func (b *B) nsPerOp() int64 { func (b *B) nsPerOp() int64 {
if b.N <= 0 { if b.N <= 0 {
return 0 return 0
...@@ -298,7 +306,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ ...@@ -298,7 +306,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [
continue continue
} }
results := r.String() results := r.String()
if *benchmarkMemory { if *benchmarkMemory || b.showAllocResult {
results += "\t" + r.MemString() results += "\t" + r.MemString()
} }
fmt.Println(results) fmt.Println(results)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment