golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/profile/proto_test.go (about)

     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  }
    56  
    57  var testL = []*Location{
    58  	{
    59  		ID:      1,
    60  		Address: 1,
    61  		Mapping: testM[0],
    62  		Line: []Line{
    63  			{
    64  				Function: testF[0],
    65  				Line:     2,
    66  			},
    67  			{
    68  				Function: testF[1],
    69  				Line:     2222222,
    70  			},
    71  		},
    72  	},
    73  	{
    74  		ID:      2,
    75  		Mapping: testM[1],
    76  		Address: 11,
    77  		Line: []Line{
    78  			{
    79  				Function: testF[2],
    80  				Line:     2,
    81  			},
    82  		},
    83  	},
    84  	{
    85  		ID:      3,
    86  		Mapping: testM[1],
    87  		Address: 12,
    88  	},
    89  }
    90  
    91  var all = &Profile{
    92  	PeriodType:    &ValueType{Type: "cpu", Unit: "milliseconds"},
    93  	Period:        10,
    94  	DurationNanos: 10e9,
    95  	SampleType: []*ValueType{
    96  		{Type: "cpu", Unit: "cycles"},
    97  		{Type: "object", Unit: "count"},
    98  	},
    99  	Sample: []*Sample{
   100  		{
   101  			Location: []*Location{testL[0], testL[1], testL[2], testL[1], testL[1]},
   102  			Label: map[string][]string{
   103  				"key1": []string{"value1"},
   104  				"key2": []string{"value2"},
   105  			},
   106  			Value: []int64{10, 20},
   107  		},
   108  		{
   109  			Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
   110  			Value:    []int64{30, 40},
   111  			Label: map[string][]string{
   112  				"key1": []string{"value1"},
   113  				"key2": []string{"value2"},
   114  			},
   115  			NumLabel: map[string][]int64{
   116  				"key1": []int64{1, 2},
   117  				"key2": []int64{3, 4},
   118  			},
   119  		},
   120  	},
   121  	Function: testF,
   122  	Mapping:  testM,
   123  	Location: testL,
   124  	Comments: []string{"Comment 1", "Comment 2"},
   125  }
   126  
   127  func TestMarshalUnmarshal(t *testing.T) {
   128  	// Write the profile, parse it, and ensure they're equal.
   129  	buf := bytes.NewBuffer(nil)
   130  	all.Write(buf)
   131  	all2, err := Parse(buf)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  
   136  	js1 := proftest.EncodeJSON(&all)
   137  	js2 := proftest.EncodeJSON(&all2)
   138  	if string(js1) != string(js2) {
   139  		t.Errorf("profiles differ")
   140  		d, err := proftest.Diff(js1, js2)
   141  		if err != nil {
   142  			t.Fatal(err)
   143  		}
   144  		t.Error("\n" + string(d))
   145  	}
   146  }