...

Source file src/github.com/google/pprof/profile/proto_test.go

Documentation: github.com/google/pprof/profile

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package profile
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/google/pprof/internal/proftest"
    22  )
    23  
    24  var testM = []*Mapping{
    25  	{
    26  		ID:              1,
    27  		Start:           1,
    28  		Limit:           10,
    29  		Offset:          0,
    30  		File:            "file1",
    31  		BuildID:         "buildid1",
    32  		HasFunctions:    true,
    33  		HasFilenames:    true,
    34  		HasLineNumbers:  true,
    35  		HasInlineFrames: true,
    36  	},
    37  	{
    38  		ID:              2,
    39  		Start:           10,
    40  		Limit:           30,
    41  		Offset:          9,
    42  		File:            "file1",
    43  		BuildID:         "buildid2",
    44  		HasFunctions:    true,
    45  		HasFilenames:    true,
    46  		HasLineNumbers:  true,
    47  		HasInlineFrames: true,
    48  	},
    49  }
    50  
    51  var testF = []*Function{
    52  	{ID: 1, Name: "func1", SystemName: "func1", Filename: "file1"},
    53  	{ID: 2, Name: "func2", SystemName: "func2", Filename: "file1"},
    54  	{ID: 3, Name: "func3", SystemName: "func3", Filename: "file2"},
    55  	{ID: 4, Name: "func4", SystemName: "func4", Filename: "file3"},
    56  	{ID: 5, Name: "func5", SystemName: "func5", Filename: "file4"},
    57  }
    58  
    59  var testL = []*Location{
    60  	{
    61  		ID:      1,
    62  		Address: 1,
    63  		Mapping: testM[0],
    64  		Line: []Line{
    65  			{
    66  				Function: testF[0],
    67  				Line:     2,
    68  			},
    69  			{
    70  				Function: testF[1],
    71  				Line:     2222222,
    72  			},
    73  		},
    74  	},
    75  	{
    76  		ID:      2,
    77  		Mapping: testM[1],
    78  		Address: 11,
    79  		Line: []Line{
    80  			{
    81  				Function: testF[2],
    82  				Line:     2,
    83  			},
    84  		},
    85  	},
    86  	{
    87  		ID:      3,
    88  		Mapping: testM[1],
    89  		Address: 12,
    90  	},
    91  	{
    92  		ID:      4,
    93  		Mapping: testM[1],
    94  		Address: 12,
    95  		Line: []Line{
    96  			{
    97  				Function: testF[4],
    98  				Line:     6,
    99  			},
   100  			{
   101  				Function: testF[4],
   102  				Line:     6,
   103  			},
   104  		},
   105  		IsFolded: true,
   106  	},
   107  }
   108  
   109  var all = &Profile{
   110  	PeriodType:    &ValueType{Type: "cpu", Unit: "milliseconds"},
   111  	Period:        10,
   112  	DurationNanos: 10e9,
   113  	SampleType: []*ValueType{
   114  		{Type: "cpu", Unit: "cycles"},
   115  		{Type: "object", Unit: "count"},
   116  	},
   117  	Sample: []*Sample{
   118  		{
   119  			Location: []*Location{testL[0], testL[1], testL[2], testL[1], testL[1]},
   120  			Label: map[string][]string{
   121  				"key1": {"value1"},
   122  				"key2": {"value2"},
   123  			},
   124  			Value: []int64{10, 20},
   125  		},
   126  		{
   127  			Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
   128  			Value:    []int64{30, 40},
   129  			Label: map[string][]string{
   130  				"key1": {"value1"},
   131  				"key2": {"value2"},
   132  			},
   133  			NumLabel: map[string][]int64{
   134  				"key1":      {1, 2},
   135  				"key2":      {3, 4},
   136  				"bytes":     {3, 4},
   137  				"requests":  {1, 1, 3, 4, 5},
   138  				"alignment": {3, 4},
   139  			},
   140  			NumUnit: map[string][]string{
   141  				"requests":  {"", "", "seconds", "", "s"},
   142  				"alignment": {"kilobytes", "kilobytes"},
   143  			},
   144  		},
   145  		{
   146  			Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
   147  			Value:    []int64{30, 40},
   148  			NumLabel: map[string][]int64{
   149  				"size": {0},
   150  			},
   151  			NumUnit: map[string][]string{
   152  				"size": {"bytes"},
   153  			},
   154  		},
   155  	},
   156  	Function: testF,
   157  	Mapping:  testM,
   158  	Location: testL,
   159  	Comments: []string{"Comment 1", "Comment 2"},
   160  }
   161  
   162  func TestMarshalUnmarshal(t *testing.T) {
   163  	// Write the profile, parse it, and ensure they're equal.
   164  	var buf bytes.Buffer
   165  	all.Write(&buf)
   166  	all2, err := Parse(&buf)
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	js1 := proftest.EncodeJSON(&all)
   172  	js2 := proftest.EncodeJSON(&all2)
   173  	if string(js1) != string(js2) {
   174  		t.Errorf("profiles differ")
   175  		d, err := proftest.Diff(js1, js2)
   176  		if err != nil {
   177  			t.Fatal(err)
   178  		}
   179  		t.Error("\n" + string(d))
   180  	}
   181  }
   182  

View as plain text