aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vendor/github.com/google/pprof/profile/profile_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/vendor/github.com/google/pprof/profile/profile_test.go')
-rw-r--r--src/cmd/vendor/github.com/google/pprof/profile/profile_test.go147
1 files changed, 144 insertions, 3 deletions
diff --git a/src/cmd/vendor/github.com/google/pprof/profile/profile_test.go b/src/cmd/vendor/github.com/google/pprof/profile/profile_test.go
index bc2ab8bdd1..ab020275cb 100644
--- a/src/cmd/vendor/github.com/google/pprof/profile/profile_test.go
+++ b/src/cmd/vendor/github.com/google/pprof/profile/profile_test.go
@@ -73,11 +73,11 @@ func TestParse(t *testing.T) {
}
// Reencode and decode.
- bw := bytes.NewBuffer(nil)
- if err := p.Write(bw); err != nil {
+ var bw bytes.Buffer
+ if err := p.Write(&bw); err != nil {
t.Fatalf("%s: %v", source, err)
}
- if p, err = Parse(bw); err != nil {
+ if p, err = Parse(&bw); err != nil {
t.Fatalf("%s: %v", source, err)
}
js2 := p.String()
@@ -108,6 +108,21 @@ func TestParseError(t *testing.T) {
}
}
+func TestParseConcatentated(t *testing.T) {
+ prof := testProfile1.Copy()
+ // Write the profile twice to buffer to create concatented profile.
+ var buf bytes.Buffer
+ prof.Write(&buf)
+ prof.Write(&buf)
+ _, err := Parse(&buf)
+ if err == nil {
+ t.Fatalf("got nil, want error")
+ }
+ if got, want := err.Error(), "parsing profile: concatenated profiles detected"; want != got {
+ t.Fatalf("got error %q, want error %q", got, want)
+ }
+}
+
func TestCheckValid(t *testing.T) {
const path = "testdata/java.cpu"
@@ -276,6 +291,7 @@ var cpuL = []*Location{
}
var testProfile1 = &Profile{
+ TimeNanos: 10000,
PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
Period: 1,
DurationNanos: 10e9,
@@ -330,6 +346,60 @@ var testProfile1 = &Profile{
Mapping: cpuM,
}
+var testProfile1NoMapping = &Profile{
+ PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
+ Period: 1,
+ DurationNanos: 10e9,
+ SampleType: []*ValueType{
+ {Type: "samples", Unit: "count"},
+ {Type: "cpu", Unit: "milliseconds"},
+ },
+ Sample: []*Sample{
+ {
+ Location: []*Location{cpuL[0]},
+ Value: []int64{1000, 1000},
+ Label: map[string][]string{
+ "key1": {"tag1"},
+ "key2": {"tag1"},
+ },
+ },
+ {
+ Location: []*Location{cpuL[1], cpuL[0]},
+ Value: []int64{100, 100},
+ Label: map[string][]string{
+ "key1": {"tag2"},
+ "key3": {"tag2"},
+ },
+ },
+ {
+ Location: []*Location{cpuL[2], cpuL[0]},
+ Value: []int64{10, 10},
+ Label: map[string][]string{
+ "key1": {"tag3"},
+ "key2": {"tag2"},
+ },
+ },
+ {
+ Location: []*Location{cpuL[3], cpuL[0]},
+ Value: []int64{10000, 10000},
+ Label: map[string][]string{
+ "key1": {"tag4"},
+ "key2": {"tag1"},
+ },
+ },
+ {
+ Location: []*Location{cpuL[4], cpuL[0]},
+ Value: []int64{1, 1},
+ Label: map[string][]string{
+ "key1": {"tag4"},
+ "key2": {"tag1"},
+ },
+ },
+ },
+ Location: cpuL,
+ Function: cpuF,
+}
+
var testProfile2 = &Profile{
PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
Period: 1,
@@ -577,6 +647,7 @@ func TestMerge(t *testing.T) {
// location should add up to 0).
prof := testProfile1.Copy()
+ prof.Comments = []string{"comment1"}
p1, err := Merge([]*Profile{prof, prof})
if err != nil {
t.Errorf("merge error: %v", err)
@@ -586,6 +657,9 @@ func TestMerge(t *testing.T) {
if err != nil {
t.Errorf("merge error: %v", err)
}
+ if got, want := len(prof.Comments), 1; got != want {
+ t.Errorf("len(prof.Comments) = %d, want %d", got, want)
+ }
// Use aggregation to merge locations at function granularity.
if err := prof.Aggregate(false, true, false, false, false); err != nil {
@@ -627,6 +701,39 @@ func TestMergeAll(t *testing.T) {
}
}
+func TestIsFoldedMerge(t *testing.T) {
+ testProfile1Folded := testProfile1.Copy()
+ testProfile1Folded.Location[0].IsFolded = true
+ testProfile1Folded.Location[1].IsFolded = true
+
+ for _, tc := range []struct {
+ name string
+ profs []*Profile
+ wantLocationLen int
+ }{
+ {
+ name: "folded and non-folded locations not merged",
+ profs: []*Profile{testProfile1.Copy(), testProfile1Folded.Copy()},
+ wantLocationLen: 7,
+ },
+ {
+ name: "identical folded locations are merged",
+ profs: []*Profile{testProfile1Folded.Copy(), testProfile1Folded.Copy()},
+ wantLocationLen: 5,
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ prof, err := Merge(tc.profs)
+ if err != nil {
+ t.Fatalf("merge error: %v", err)
+ }
+ if got, want := len(prof.Location), tc.wantLocationLen; got != want {
+ t.Fatalf("got %d locations, want %d locations", got, want)
+ }
+ })
+ }
+}
+
func TestNumLabelMerge(t *testing.T) {
for _, tc := range []struct {
name string
@@ -684,6 +791,40 @@ func TestNumLabelMerge(t *testing.T) {
}
}
+func TestEmptyMappingMerge(t *testing.T) {
+ // Aggregate a profile with itself and once again with a factor of
+ // -2. Should end up with an empty profile (all samples for a
+ // location should add up to 0).
+
+ prof1 := testProfile1.Copy()
+ prof2 := testProfile1NoMapping.Copy()
+ p1, err := Merge([]*Profile{prof2, prof1})
+ if err != nil {
+ t.Errorf("merge error: %v", err)
+ }
+ prof2.Scale(-2)
+ prof, err := Merge([]*Profile{p1, prof2})
+ if err != nil {
+ t.Errorf("merge error: %v", err)
+ }
+
+ // Use aggregation to merge locations at function granularity.
+ if err := prof.Aggregate(false, true, false, false, false); err != nil {
+ t.Errorf("aggregating after merge: %v", err)
+ }
+
+ samples := make(map[string]int64)
+ for _, s := range prof.Sample {
+ tb := locationHash(s)
+ samples[tb] = samples[tb] + s.Value[0]
+ }
+ for s, v := range samples {
+ if v != 0 {
+ t.Errorf("nonzero value for sample %s: %d", s, v)
+ }
+ }
+}
+
func TestNormalizeBySameProfile(t *testing.T) {
pb := testProfile1.Copy()
p := testProfile1.Copy()