github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/runtime/pprof/protomem_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pprof
     6  
     7  import (
     8  	"bytes"
     9  	"runtime"
    10  	"runtime/pprof/internal/profile"
    11  	"testing"
    12  )
    13  
    14  func TestConvertMemProfile(t *testing.T) {
    15  	addr1, addr2, map1, map2 := testPCs(t)
    16  
    17  	var buf bytes.Buffer
    18  	a1, a2 := uintptr(addr1), uintptr(addr2)
    19  	rate := int64(512 * 1024)
    20  	rec := []runtime.MemProfileRecord{
    21  		{AllocBytes: 4096, FreeBytes: 1024, AllocObjects: 4, FreeObjects: 1, Stack0: [32]uintptr{a1, a2}},
    22  		{AllocBytes: 512 * 1024, FreeBytes: 0, AllocObjects: 1, FreeObjects: 0, Stack0: [32]uintptr{a2 + 1, a2 + 2}},
    23  		{AllocBytes: 512 * 1024, FreeBytes: 512 * 1024, AllocObjects: 1, FreeObjects: 1, Stack0: [32]uintptr{a1 + 1, a1 + 2, a2 + 3}},
    24  	}
    25  
    26  	if err := writeHeapProto(&buf, rec, rate); err != nil {
    27  		t.Fatalf("writing profile: %v", err)
    28  	}
    29  
    30  	p, err := profile.Parse(&buf)
    31  	if err != nil {
    32  		t.Fatalf("profile.Parse: %v", err)
    33  	}
    34  
    35  	periodType := &profile.ValueType{Type: "space", Unit: "bytes"}
    36  	sampleType := []*profile.ValueType{
    37  		{Type: "alloc_objects", Unit: "count"},
    38  		{Type: "alloc_space", Unit: "bytes"},
    39  		{Type: "inuse_objects", Unit: "count"},
    40  		{Type: "inuse_space", Unit: "bytes"},
    41  	}
    42  	samples := []*profile.Sample{
    43  		{
    44  			Value: []int64{2050, 2099200, 1537, 1574400},
    45  			Location: []*profile.Location{
    46  				{ID: 1, Mapping: map1, Address: addr1},
    47  				{ID: 2, Mapping: map2, Address: addr2},
    48  			},
    49  			NumLabel: map[string][]int64{"bytes": {1024}},
    50  		},
    51  		{
    52  			Value: []int64{1, 829411, 1, 829411},
    53  			Location: []*profile.Location{
    54  				{ID: 3, Mapping: map2, Address: addr2 + 1},
    55  				{ID: 4, Mapping: map2, Address: addr2 + 2},
    56  			},
    57  			NumLabel: map[string][]int64{"bytes": {829411}},
    58  		},
    59  		{
    60  			Value: []int64{1, 829411, 0, 0},
    61  			Location: []*profile.Location{
    62  				{ID: 5, Mapping: map1, Address: addr1 + 1},
    63  				{ID: 6, Mapping: map1, Address: addr1 + 2},
    64  				{ID: 7, Mapping: map2, Address: addr2 + 3},
    65  			},
    66  			NumLabel: map[string][]int64{"bytes": {829411}},
    67  		},
    68  	}
    69  	checkProfile(t, p, rate, periodType, sampleType, samples)
    70  }