aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/pprof/mprof_test.go
diff options
context:
space:
mode:
authorunknown <daria.kolistratova@intel.com>2016-10-07 18:21:03 +0300
committerMichael Matloob <matloob@golang.org>2016-10-28 18:08:27 +0000
commit7d14401bcbee4a8ff33ac869ef5ebb156a179ab6 (patch)
tree4ad1794aacfee87f3d69d380a824586fd560f57b /src/runtime/pprof/mprof_test.go
parentd70b0fe6c4d1b1369b742ea5b7d4e6f0c50ffdcb (diff)
downloadgo-7d14401bcbee4a8ff33ac869ef5ebb156a179ab6.tar.xz
runtime/pprof: write profiles in protobuf format.
Added functions with suffix proto and stuff from pprof tool to translate to protobuf. Done as the profile proto is more extensible than the legacy pprof format and is pprof's preferred profile format. Large part was taken from https://github.com/google/pprof tool. Tested by hand and compared the result with translated by pprof tool, profiles are identical. Fixes #16093 Change-Id: I5acdb2809cab0d16ed4694fdaa7b8ddfd68df11e Reviewed-on: https://go-review.googlesource.com/30556 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
Diffstat (limited to 'src/runtime/pprof/mprof_test.go')
-rw-r--r--src/runtime/pprof/mprof_test.go62
1 files changed, 42 insertions, 20 deletions
diff --git a/src/runtime/pprof/mprof_test.go b/src/runtime/pprof/mprof_test.go
index 0fff9d46d9..547baada4a 100644
--- a/src/runtime/pprof/mprof_test.go
+++ b/src/runtime/pprof/mprof_test.go
@@ -6,8 +6,8 @@ package pprof_test
import (
"bytes"
- "fmt"
- "regexp"
+ "math"
+ "reflect"
"runtime"
. "runtime/pprof"
"testing"
@@ -71,26 +71,48 @@ func TestMemoryProfiler(t *testing.T) {
memoryProfilerRun++
- tests := []string{
- fmt.Sprintf(`%v: %v \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
-# 0x[0-9,a-f]+ runtime/pprof_test\.allocatePersistent1K\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test\.go:40
-# 0x[0-9,a-f]+ runtime/pprof_test\.TestMemoryProfiler\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test\.go:63
-`, 32*memoryProfilerRun, 1024*memoryProfilerRun, 32*memoryProfilerRun, 1024*memoryProfilerRun),
-
- fmt.Sprintf(`0: 0 \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
-# 0x[0-9,a-f]+ runtime/pprof_test\.allocateTransient1M\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test.go:21
-# 0x[0-9,a-f]+ runtime/pprof_test\.TestMemoryProfiler\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test.go:61
-`, (1<<10)*memoryProfilerRun, (1<<20)*memoryProfilerRun),
+ r := bytes.NewReader(buf.Bytes())
+ p, err := Parse(r)
+ if err != nil {
+ t.Fatalf("can't parse pprof profile: %v", err)
+ }
+ if len(p.Sample) < 3 {
+ t.Fatalf("few samples, got: %d", len(p.Sample))
+ }
+ testSample := make(map[int][]int64)
+ testSample[0] = scaleHeapSample((int64)(32*memoryProfilerRun), (int64)(1024*memoryProfilerRun), p.Period)
+ testSample[0] = append(testSample[0], testSample[0][0], testSample[0][1])
+ testSample[1] = scaleHeapSample((int64)((1<<10)*memoryProfilerRun), (int64)((1<<20)*memoryProfilerRun), p.Period)
+ testSample[1] = append([]int64{0, 0}, testSample[1][0], testSample[1][1])
+ testSample[2] = scaleHeapSample((int64)(memoryProfilerRun), (int64)((2<<20)*memoryProfilerRun), p.Period)
+ testSample[2] = append([]int64{0, 0}, testSample[2][0], testSample[2][1])
+ for _, value := range testSample {
+ found := false
+ for i := range p.Sample {
+ if reflect.DeepEqual(p.Sample[i].Value, value) {
+ found = true
+ break
+ }
+ }
+ if !found {
+ t.Fatalf("the entry did not match any sample:\n%v\n", value)
+ }
+ }
+}
- fmt.Sprintf(`0: 0 \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
-# 0x[0-9,a-f]+ runtime/pprof_test\.allocateTransient2M\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test.go:27
-# 0x[0-9,a-f]+ runtime/pprof_test\.TestMemoryProfiler\+0x[0-9,a-f]+ .*/runtime/pprof/mprof_test.go:62
-`, memoryProfilerRun, (2<<20)*memoryProfilerRun),
+func scaleHeapSample(count, size, rate int64) []int64 {
+ if count == 0 || size == 0 {
+ return []int64{0, 0}
}
- for _, test := range tests {
- if !regexp.MustCompile(test).Match(buf.Bytes()) {
- t.Fatalf("The entry did not match:\n%v\n\nProfile:\n%v\n", test, buf.String())
- }
+ if rate <= 1 {
+ // if rate==1 all samples were collected so no adjustment is needed.
+ // if rate<1 treat as unknown and skip scaling.
+ return []int64{count, size}
}
+
+ avgSize := float64(size) / float64(count)
+ scale := 1 / (1 - math.Exp(-avgSize/float64(rate)))
+
+ return []int64{int64(float64(count) * scale), int64(float64(size) * scale)}
}