]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/pprof: add block profile test
authorDmitriy Vyukov <dvyukov@google.com>
Sun, 11 Aug 2013 09:05:51 +0000 (13:05 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Sun, 11 Aug 2013 09:05:51 +0000 (13:05 +0400)
Fixes #5993.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12665046

src/pkg/runtime/pprof/pprof_test.go

index 040d77a4346c8d803dbfae073100ed43a2117b00..630d3643be5699b70219af538e651ceb35b4543b 100644 (file)
@@ -11,10 +11,13 @@ import (
        "bytes"
        "hash/crc32"
        "os/exec"
+       "regexp"
        "runtime"
        . "runtime/pprof"
        "strings"
+       "sync"
        "testing"
+       "time"
        "unsafe"
 )
 
@@ -148,3 +151,55 @@ var badOS = map[string]bool{
        "netbsd":  true,
        "openbsd": true,
 }
+
+func TestBlockProfile(t *testing.T) {
+       runtime.SetBlockProfileRate(1)
+       defer runtime.SetBlockProfileRate(0)
+       produceChanContention()
+       produceMutexContention()
+       var w bytes.Buffer
+       Lookup("block").WriteTo(&w, 1)
+       prof := w.String()
+
+       if !strings.HasPrefix(prof, "--- contention:\ncycles/second=") {
+               t.Fatalf("Bad profile header:\n%v", prof)
+       }
+
+       reChan := regexp.MustCompile(`
+[0-9]+ [0-9]+ @ 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]+
+#      0x[0-9,a-f]+    runtime/pprof_test\.produceChanContention\+0x[0-9,a-f]+ .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+
+#      0x[0-9,a-f]+    runtime/pprof_test\.TestBlockProfile\+0x[0-9,a-f]+      .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+
+`)
+       if !reChan.MatchString(prof) {
+               t.Fatalf("Bad chan entry, expect:\n%v\ngot:\n%v", reChan, prof)
+       }
+
+       reMutex := regexp.MustCompile(`
+[0-9]+ [0-9]+ @ 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]+
+#      0x[0-9,a-f]+    sync\.\(\*Mutex\)\.Lock\+0x[0-9,a-f]+   .*/src/pkg/sync/mutex\.go:[0-9]+
+#      0x[0-9,a-f]+    runtime/pprof_test\.produceMutexContention\+0x[0-9,a-f]+        .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+
+#      0x[0-9,a-f]+    runtime/pprof_test\.TestBlockProfile\+0x[0-9,a-f]+      .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+
+`)
+       if !reMutex.MatchString(prof) {
+               t.Fatalf("Bad mutex entry, expect:\n%v\ngot:\n%v", reMutex, prof)
+       }
+}
+
+func produceChanContention() {
+       c := make(chan bool)
+       go func() {
+               time.Sleep(10 * time.Millisecond)
+               c <- true
+       }()
+       <-c
+}
+
+func produceMutexContention() {
+       var mu sync.Mutex
+       mu.Lock()
+       go func() {
+               time.Sleep(10 * time.Millisecond)
+               mu.Unlock()
+       }()
+       mu.Lock()
+}