diff options
Diffstat (limited to 'src/runtime/pprof/internal/protopprof/protopprof_test.go')
-rw-r--r-- | src/runtime/pprof/internal/protopprof/protopprof_test.go | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/src/runtime/pprof/internal/protopprof/protopprof_test.go b/src/runtime/pprof/internal/protopprof/protopprof_test.go new file mode 100644 index 0000000000..2884b1005a --- /dev/null +++ b/src/runtime/pprof/internal/protopprof/protopprof_test.go @@ -0,0 +1,207 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package protopprof + +import ( + "bytes" + "fmt" + "internal/pprof/profile" + "io" + "io/ioutil" + "reflect" + "runtime" + "runtime/pprof" + "testing" + "time" + "unsafe" +) + +// Profile collects a CPU utilization profile and +// writes it to w as a compressed profile.proto. It's used by +// TestProfileParse. +func Profile(w io.Writer, seconds int) error { + var buf bytes.Buffer + // Collect the CPU profile in legacy format in buf. + startTime := time.Now() + if err := pprof.StartCPUProfile(&buf); err != nil { + return fmt.Errorf("Could not enable CPU profiling: %s\n", err) + } + time.Sleep(time.Duration(seconds) * time.Second) + pprof.StopCPUProfile() + + const untagged = false + p, err := TranslateCPUProfile(buf.Bytes(), startTime) + if err != nil { + return err + } + return p.Write(w) +} + +// Helper function to initialize empty cpu profile with sampling period provided. +func createEmptyProfileWithPeriod(t *testing.T, periodMs uint64) bytes.Buffer { + // Mock the sample header produced by cpu profiler. Write a sample + // period of 2000 microseconds, followed by no samples. + buf := new(bytes.Buffer) + // Profile header is as follows: + // The first, third and fifth words are 0. The second word is 3. + // The fourth word is the period. + // EOD marker: + // The sixth word -- count is initialized to 0 above. + // The code below sets the seventh word -- nstk to 1 + // The eighth word -- addr is initialized to 0 above. + words := []int{0, 3, 0, int(periodMs), 0, 0, 1, 0} + n := int(unsafe.Sizeof(0)) * len(words) + data := ((*[1 << 29]byte)(unsafe.Pointer(&words[0])))[:n:n] + if _, err := buf.Write(data); err != nil { + t.Fatalf("createEmptyProfileWithPeriod failed: %v", err) + } + return *buf +} + +// Helper function to initialize cpu profile with two sample values. +func createProfileWithTwoSamples(t *testing.T, periodMs uintptr, count1 uintptr, count2 uintptr, + address1 uintptr, address2 uintptr) bytes.Buffer { + // Mock the sample header produced by cpu profiler. Write a sample + // period of 2000 microseconds, followed by no samples. + buf := new(bytes.Buffer) + words := []uint64{0, 3, 0, uint64(periodMs), 0, uint64(count1), 2, + uint64(address1), uint64(address1 + 2), + uint64(count2), 2, uint64(address2), uint64(address2 + 2), + 0, uint64(1), 0} + for _, n := range words { + var err error + switch unsafe.Sizeof(int(0)) { + case 8: + _, err = buf.Write((*[8]byte)(unsafe.Pointer(&n))[:8:8]) + case 4: + _, err = buf.Write((*[4]byte)(unsafe.Pointer(&n))[:4:4]) + } + if err != nil { + t.Fatalf("createProfileWithTwoSamples failed: %v", err) + } + } + return *buf +} + +// Tests that server creates a cpu profile handler that outputs a parsable Profile profile. +func TestCPUProfileParse(t *testing.T) { + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + var buf bytes.Buffer + if err := Profile(&buf, 30); err != nil { + t.Fatalf("Profile failed: %v", err) + } + runtime.ReadMemStats(&after) + _, err := profile.Parse(&buf) + if err != nil { + t.Fatalf("Could not parse Profile profile: %v", err) + } +} + +// Tests TranslateCPUProfile parses correct sampling period in an otherwise empty cpu profile. +func TestTranlateCPUProfileSamplingPeriod(t *testing.T) { + // A test server with mock cpu profile data. + var buf bytes.Buffer + + startTime := time.Now() + b := createEmptyProfileWithPeriod(t, 2000) + p, err := TranslateCPUProfile(b.Bytes(), startTime) + if err != nil { + t.Fatalf("translate failed: %v", err) + } + if err := p.Write(&buf); err != nil { + t.Fatalf("write failed: %v", err) + } + + p, err = profile.Parse(&buf) + if err != nil { + t.Fatalf("Could not parse Profile profile: %v", err) + } + + // Expected PeriodType and SampleType. + expectedPeriodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"} + expectedSampleType := []*profile.ValueType{ + {Type: "samples", Unit: "count"}, + {Type: "cpu", Unit: "nanoseconds"}, + } + if p.Period != 2000*1000 || !reflect.DeepEqual(p.PeriodType, expectedPeriodType) || + !reflect.DeepEqual(p.SampleType, expectedSampleType) || p.Sample != nil { + t.Fatalf("Unexpected Profile fields") + } +} + +func getSampleAsString(sample []*profile.Sample) string { + var str string + for _, x := range sample { + for _, y := range x.Location { + if y.Mapping != nil { + str += fmt.Sprintf("Mapping:%v\n", *y.Mapping) + } + str += fmt.Sprintf("Location:%v\n", y) + } + str += fmt.Sprintf("Sample:%v\n", *x) + } + return str +} + +// Tests TranslateCPUProfile parses a cpu profile with sample values present. +func TestTranslateCPUProfileWithSamples(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("test requires a system with /proc/self/maps") + } + // Figure out two addresses from /proc/self/maps. + mmap, err := ioutil.ReadFile("/proc/self/maps") + if err != nil { + t.Fatal("Cannot read /proc/self/maps") + } + rd := bytes.NewReader(mmap) + mprof := &profile.Profile{} + if err = mprof.ParseMemoryMap(rd); err != nil { + t.Fatalf("Cannot parse /proc/self/maps") + } + if len(mprof.Mapping) < 2 { + t.Fatalf("Less than two mappings") + } + address1 := mprof.Mapping[0].Start + address2 := mprof.Mapping[1].Start + // A test server with mock cpu profile data. + + startTime := time.Now() + b := createProfileWithTwoSamples(t, 2000, 20, 40, uintptr(address1), uintptr(address2)) + p, err := TranslateCPUProfile(b.Bytes(), startTime) + + if err != nil { + t.Fatalf("Could not parse Profile profile: %v", err) + } + // Expected PeriodType, SampleType and Sample. + expectedPeriodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"} + expectedSampleType := []*profile.ValueType{ + {Type: "samples", Unit: "count"}, + {Type: "cpu", Unit: "nanoseconds"}, + } + expectedSample := []*profile.Sample{ + {Value: []int64{20, 20 * 2000 * 1000}, Location: []*profile.Location{ + {ID: 1, Mapping: mprof.Mapping[0], Address: address1}, + {ID: 2, Mapping: mprof.Mapping[0], Address: address1 + 1}, + }}, + {Value: []int64{40, 40 * 2000 * 1000}, Location: []*profile.Location{ + {ID: 3, Mapping: mprof.Mapping[1], Address: address2}, + {ID: 4, Mapping: mprof.Mapping[1], Address: address2 + 1}, + }}, + } + if p.Period != 2000*1000 { + t.Fatalf("Sampling periods do not match") + } + if !reflect.DeepEqual(p.PeriodType, expectedPeriodType) { + t.Fatalf("Period types do not match") + } + if !reflect.DeepEqual(p.SampleType, expectedSampleType) { + t.Fatalf("Sample types do not match") + } + if !reflect.DeepEqual(p.Sample, expectedSample) { + t.Fatalf("Samples do not match: Expected: %v, Got:%v", getSampleAsString(expectedSample), + getSampleAsString(p.Sample)) + } +} |