]> Cypherpunks repositories - gostls13.git/commitdiff
testing: introduce (*B).ReportAllocs()
authorShenghou Ma <minux.ma@gmail.com>
Thu, 17 Jan 2013 10:45:49 +0000 (18:45 +0800)
committerShenghou Ma <minux.ma@gmail.com>
Thu, 17 Jan 2013 10:45:49 +0000 (18:45 +0800)
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

src/pkg/exp/html/parse_test.go
src/pkg/exp/html/token_test.go
src/pkg/testing/benchmark.go

index 7cf2ff4163e43f01b916ef37082247402fcb6d88..4896dfb7a0f558695d6954087997978bb77ceeba 100644 (file)
@@ -382,15 +382,9 @@ func BenchmarkParser(b *testing.B) {
        }
        b.SetBytes(int64(len(buf)))
        runtime.GC()
-       var ms runtime.MemStats
-       runtime.ReadMemStats(&ms)
-       mallocs := ms.Mallocs
+       b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                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)
 }
index 63a8bfc483486cfea42d204c4a75b3f83c7428af..14e23467f49e9b152fe74bc6941cb1caedd0c478 100644 (file)
@@ -634,9 +634,7 @@ func benchmarkTokenizer(b *testing.B, level int) {
        }
        b.SetBytes(int64(len(buf)))
        runtime.GC()
-       var ms runtime.MemStats
-       runtime.ReadMemStats(&ms)
-       mallocs := ms.Mallocs
+       b.ReportAllocs()
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                z := NewTokenizer(bytes.NewBuffer(buf))
@@ -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) }
index cb92fab50adbf1bf115b16109abcd72d324eb41f..25fb2d61918dbfc37d7d9290438ccafb756fc70f 100644 (file)
@@ -34,11 +34,12 @@ type InternalBenchmark struct {
 // timing and to specify the number of iterations to run.
 type B struct {
        common
-       N         int
-       benchmark InternalBenchmark
-       bytes     int64
-       timerOn   bool
-       result    BenchmarkResult
+       N               int
+       benchmark       InternalBenchmark
+       bytes           int64
+       timerOn         bool
+       showAllocResult bool
+       result          BenchmarkResult
        // The initial states of memStats.Mallocs and memStats.TotalAlloc.
        startAllocs uint64
        startBytes  uint64
@@ -91,6 +92,13 @@ func (b *B) ResetTimer() {
 // If this is called, the benchmark will report ns/op and MB/s.
 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 {
        if b.N <= 0 {
                return 0
@@ -298,7 +306,7 @@ func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [
                                continue
                        }
                        results := r.String()
-                       if *benchmarkMemory {
+                       if *benchmarkMemory || b.showAllocResult {
                                results += "\t" + r.MemString()
                        }
                        fmt.Println(results)