golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/symbolz/symbolz_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 symbolz
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/google/pprof/internal/plugin"
    23  	"github.com/google/pprof/internal/proftest"
    24  	"github.com/google/pprof/profile"
    25  )
    26  
    27  func TestSymbolzURL(t *testing.T) {
    28  	for try, want := range map[string]string{
    29  		"http://host:8000/profilez":                        "http://host:8000/symbolz",
    30  		"http://host:8000/profilez?seconds=5":              "http://host:8000/symbolz",
    31  		"http://host:8000/profilez?seconds=5&format=proto": "http://host:8000/symbolz",
    32  		"http://host:8000/heapz?format=legacy":             "http://host:8000/symbolz",
    33  		"http://host:8000/debug/pprof/profile":             "http://host:8000/debug/pprof/symbol",
    34  		"http://host:8000/debug/pprof/profile?seconds=10":  "http://host:8000/debug/pprof/symbol",
    35  		"http://host:8000/debug/pprof/heap":                "http://host:8000/debug/pprof/symbol",
    36  	} {
    37  		if got := symbolz(try); got != want {
    38  			t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
    39  		}
    40  	}
    41  }
    42  
    43  func TestSymbolize(t *testing.T) {
    44  	m := []*profile.Mapping{
    45  		{
    46  			ID:      1,
    47  			Start:   0x1000,
    48  			Limit:   0x5000,
    49  			BuildID: "buildid",
    50  		},
    51  	}
    52  	p := &profile.Profile{
    53  		Location: []*profile.Location{
    54  			{ID: 1, Mapping: m[0], Address: 0x1000},
    55  			{ID: 2, Mapping: m[0], Address: 0x2000},
    56  			{ID: 3, Mapping: m[0], Address: 0x3000},
    57  			{ID: 4, Mapping: m[0], Address: 0x4000},
    58  		},
    59  		Mapping: m,
    60  	}
    61  
    62  	s := plugin.MappingSources{
    63  		"buildid": []struct {
    64  			Source string
    65  			Start  uint64
    66  		}{
    67  			{Source: "http://localhost:80/profilez"},
    68  		},
    69  	}
    70  
    71  	if err := Symbolize(s, fetchSymbols, p, &proftest.TestUI{T: t}); err != nil {
    72  		t.Errorf("symbolz: %v", err)
    73  	}
    74  
    75  	if l := p.Location[0]; len(l.Line) != 0 {
    76  		t.Errorf("unexpected symbolization for %#x: %v", l.Address, l.Line)
    77  	}
    78  
    79  	for _, l := range p.Location[1:] {
    80  		if len(l.Line) != 1 {
    81  			t.Errorf("failed to symbolize %#x", l.Address)
    82  			continue
    83  		}
    84  		address := l.Address - l.Mapping.Start
    85  		if got, want := l.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
    86  			t.Errorf("symbolz %#x, got %s, want %s", address, got, want)
    87  		}
    88  	}
    89  }
    90  
    91  func fetchSymbols(source, post string) ([]byte, error) {
    92  	var symbolz string
    93  
    94  	addresses := strings.Split(post, "+")
    95  	// Do not symbolize the first symbol.
    96  	for _, address := range addresses[1:] {
    97  		symbolz += fmt.Sprintf("%s\t%s\n", address, address)
    98  	}
    99  	return []byte(symbolz), nil
   100  }