ソースを参照

record diff base profile label key/value constant (#384)

Margaret Nolan 6 年 前
コミット
8b03ce837f
共有3 個のファイルを変更した62 個の追加3 個の削除を含む
  1. 3
    3
      internal/report/report.go
  2. 6
    0
      profile/profile.go
  3. 53
    0
      profile/profile_test.go

+ 3
- 3
internal/report/report.go ファイルの表示

@@ -1217,8 +1217,8 @@ func NewDefault(prof *profile.Profile, options Options) *Report {
1217 1217
 }
1218 1218
 
1219 1219
 // computeTotal computes the sum of the absolute value of all sample values.
1220
-// If any samples have the label "pprof::base" with value "true", then the total
1221
-// will only include samples with that label.
1220
+// If any samples have label indicating they belong to the diff base, then the
1221
+// total will only include samples with that label.
1222 1222
 func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64) int64 {
1223 1223
 	var div, total, diffDiv, diffTotal int64
1224 1224
 	for _, sample := range prof.Sample {
@@ -1232,7 +1232,7 @@ func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64) i
1232 1232
 		}
1233 1233
 		total += v
1234 1234
 		div += d
1235
-		if sample.HasLabel("pprof::base", "true") {
1235
+		if sample.DiffBaseSample() {
1236 1236
 			diffTotal += v
1237 1237
 			diffDiv += d
1238 1238
 		}

+ 6
- 0
profile/profile.go ファイルの表示

@@ -704,6 +704,12 @@ func (s *Sample) HasLabel(key, value string) bool {
704 704
 	return false
705 705
 }
706 706
 
707
+// DiffBaseSample returns true if a sample belongs to the diff base and false
708
+// otherwise.
709
+func (s *Sample) DiffBaseSample() bool {
710
+	return s.HasLabel("pprof::base", "true")
711
+}
712
+
707 713
 // Scale multiplies all sample values in a profile by a constant.
708 714
 func (p *Profile) Scale(ratio float64) {
709 715
 	if ratio == 1 {

+ 53
- 0
profile/profile_test.go ファイルの表示

@@ -977,6 +977,59 @@ func TestHasLabel(t *testing.T) {
977 977
 	}
978 978
 }
979 979
 
980
+func TestDiffBaseSample(t *testing.T) {
981
+	var testcases = []struct {
982
+		desc               string
983
+		labels             map[string][]string
984
+		wantDiffBaseSample bool
985
+	}{
986
+		{
987
+			desc:               "empty label does not have label",
988
+			labels:             map[string][]string{},
989
+			wantDiffBaseSample: false,
990
+		},
991
+		{
992
+			desc:               "label with one key and value, including diff base label",
993
+			labels:             map[string][]string{"pprof::base": {"true"}},
994
+			wantDiffBaseSample: true,
995
+		},
996
+		{
997
+			desc:               "label with one key and value, not including diff base label",
998
+			labels:             map[string][]string{"key": {"value"}},
999
+			wantDiffBaseSample: false,
1000
+		},
1001
+		{
1002
+			desc: "label with many keys and values, including diff base label",
1003
+			labels: map[string][]string{
1004
+				"pprof::base": {"value2", "true"},
1005
+				"key2":        {"true", "value2", "value2"},
1006
+				"key3":        {"true", "value2", "value2"},
1007
+			},
1008
+			wantDiffBaseSample: true,
1009
+		},
1010
+		{
1011
+			desc: "label with many keys and values, not including diff base label",
1012
+			labels: map[string][]string{
1013
+				"key1": {"value2", "value1"},
1014
+				"key2": {"value1", "value2", "value2"},
1015
+				"key3": {"value1", "value2", "value2"},
1016
+			},
1017
+			wantDiffBaseSample: false,
1018
+		},
1019
+	}
1020
+
1021
+	for _, tc := range testcases {
1022
+		t.Run(tc.desc, func(t *testing.T) {
1023
+			sample := &Sample{
1024
+				Label: tc.labels,
1025
+			}
1026
+			if gotHasLabel := sample.DiffBaseSample(); gotHasLabel != tc.wantDiffBaseSample {
1027
+				t.Errorf("sample.DiffBaseSample() got %v, want %v", gotHasLabel, tc.wantDiffBaseSample)
1028
+			}
1029
+		})
1030
+	}
1031
+}
1032
+
980 1033
 func TestRemove(t *testing.T) {
981 1034
 	var testcases = []struct {
982 1035
 		desc       string