Margaret Nolan 7 роки тому
джерело
коміт
539d4380c6
4 змінених файлів з 101 додано та 50 видалено
  1. 1
    1
      internal/driver/driver.go
  2. 71
    17
      internal/driver/driver_test.go
  3. 8
    12
      profile/profile.go
  4. 21
    20
      profile/profile_test.go

+ 1
- 1
internal/driver/driver.go Переглянути файл

@@ -290,7 +290,7 @@ func identifyNumLabelUnits(p *profile.Profile, ui plugin.UI) map[string]string {
290 290
 	// Print errors for tags with multiple units associated with
291 291
 	// a single key.
292 292
 	for k, units := range ignoredUnits {
293
-		ui.PrintErr(fmt.Sprintf("For tag %s used unit %s, also encountered unit(s) %s", k, numLabelUnits[k], strings.Join(units, ",")))
293
+		ui.PrintErr(fmt.Sprintf("For tag %s used unit %s, also encountered unit(s) %s", k, numLabelUnits[k], strings.Join(units, ", ")))
294 294
 	}
295 295
 	return numLabelUnits
296 296
 }

+ 71
- 17
internal/driver/driver_test.go Переглянути файл

@@ -22,6 +22,7 @@ import (
22 22
 	"net"
23 23
 	_ "net/http/pprof"
24 24
 	"os"
25
+	"reflect"
25 26
 	"regexp"
26 27
 	"runtime"
27 28
 	"strconv"
@@ -1077,29 +1078,85 @@ func TestIdentifyNumLabelUnits(t *testing.T) {
1077 1078
 		wantIgnoreErrCount int
1078 1079
 	}{
1079 1080
 		{
1080
-			"Multiple keys, different units",
1081
-			[]map[string][]int64{{"key1": {131072}, "key2": {128}}},
1082
-			[]map[string][]string{{"key1": {"bytes"}, "key2": {"kilobytes"}}},
1083
-			map[string]string{"key1": "bytes", "key2": "kilobytes"},
1081
+			"Multiple keys, no units for all keys",
1082
+			[]map[string][]int64{{"keyA": {131072}, "keyB": {128}}},
1083
+			[]map[string][]string{{"keyA": {}, "keyB": {""}}},
1084
+			map[string]string{"keyA": "keyA", "keyB": "keyB"},
1084 1085
 			"",
1085 1086
 			0,
1086 1087
 		},
1087 1088
 		{
1088
-			"One key with different units in same sample",
1089
-			[]map[string][]int64{{"key1": {8, 8}}},
1090
-			[]map[string][]string{{"key1": {"bytes", "kilobytes"}}},
1091
-			map[string]string{"key1": "bytes"},
1092
-			`(For tag key1 used unit bytes, also encountered unit\(s\) kilobytes)`,
1089
+			"Multiple keys, different units for each key",
1090
+			[]map[string][]int64{{"keyA": {131072}, "keyB": {128}}},
1091
+			[]map[string][]string{{"keyA": {"bytes"}, "keyB": {"kilobytes"}}},
1092
+			map[string]string{"keyA": "bytes", "keyB": "kilobytes"},
1093
+			"",
1094
+			0,
1095
+		},
1096
+		{
1097
+			"Multiple keys with multiple values, different units for each key",
1098
+			[]map[string][]int64{{"keyC": {131072, 1}, "keyD": {128, 252}}},
1099
+			[]map[string][]string{{"keyC": {"bytes", "bytes"}, "keyD": {"kilobytes", "kilobytes"}}},
1100
+			map[string]string{"keyC": "bytes", "keyD": "kilobytes"},
1101
+			"",
1102
+			0,
1103
+		},
1104
+		{
1105
+			"Multiple keys with multiple values, some units missing",
1106
+			[]map[string][]int64{{"key1": {131072, 1}, "A": {128, 252}, "key3": {128}, "key4": {1}}, {"key3": {128}, "key4": {1}}},
1107
+			[]map[string][]string{{"key1": {"", "bytes"}, "A": {"kilobytes", ""}, "key3": {""}, "key4": {"hour"}}, {"key3": {"seconds"}, "key4": {""}}},
1108
+			map[string]string{"key1": "bytes", "A": "kilobytes", "key3": "seconds", "key4": "hour"},
1109
+			"",
1110
+			0,
1111
+		},
1112
+		{
1113
+			"One key with three units in same sample",
1114
+			[]map[string][]int64{{"key": {8, 8, 16}}},
1115
+			[]map[string][]string{{"key": {"bytes", "megabytes", "kilobytes"}}},
1116
+			map[string]string{"key": "bytes"},
1117
+			`(For tag key used unit bytes, also encountered unit\(s\) kilobytes, megabytes)`,
1118
+			1,
1119
+		},
1120
+		{
1121
+			"One key with four units in same sample",
1122
+			[]map[string][]int64{{"key": {8, 8, 16, 32}}},
1123
+			[]map[string][]string{{"key": {"bytes", "kilobytes", "a", "megabytes"}}},
1124
+			map[string]string{"key": "bytes"},
1125
+			`(For tag key used unit bytes, also encountered unit\(s\) a, kilobytes, megabytes)`,
1126
+			1,
1127
+		},
1128
+		{
1129
+			"One key with two units in same sample",
1130
+			[]map[string][]int64{{"key": {8, 8}}},
1131
+			[]map[string][]string{{"key": {"bytes", "seconds"}}},
1132
+			map[string]string{"key": "bytes"},
1133
+			`(For tag key used unit bytes, also encountered unit\(s\) seconds)`,
1093 1134
 			1,
1094 1135
 		},
1095 1136
 		{
1096 1137
 			"One key with different units in different samples",
1097
-			[]map[string][]int64{{"key1": {8}}, {"key1": {8}}},
1098
-			[]map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}},
1138
+			[]map[string][]int64{{"key1": {8}}, {"key1": {8}}, {"key1": {8}}},
1139
+			[]map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}, {"key1": {"megabytes"}}},
1099 1140
 			map[string]string{"key1": "bytes"},
1100
-			`(For tag key1 used unit bytes, also encountered unit\(s\) kilobytes)`,
1141
+			`(For tag key1 used unit bytes, also encountered unit\(s\) kilobytes, megabytes)`,
1101 1142
 			1,
1102 1143
 		},
1144
+		{
1145
+			"Key alignment, unit not specified",
1146
+			[]map[string][]int64{{"alignment": {8}}},
1147
+			[]map[string][]string{nil},
1148
+			map[string]string{"alignment": "bytes"},
1149
+			"",
1150
+			0,
1151
+		},
1152
+		{
1153
+			"Key request, unit not specified",
1154
+			[]map[string][]int64{{"request": {8}}, {"request": {8, 8}}},
1155
+			[]map[string][]string{nil, nil},
1156
+			map[string]string{"request": "bytes"},
1157
+			"",
1158
+			0,
1159
+		},
1103 1160
 		{
1104 1161
 			"Check units not over-written for keys with default units",
1105 1162
 			[]map[string][]int64{{
@@ -1133,11 +1190,8 @@ func TestIdentifyNumLabelUnits(t *testing.T) {
1133 1190
 			}
1134 1191
 			testUI := &proftest.TestUI{T: t, AllowRx: test.allowedRx}
1135 1192
 			units := identifyNumLabelUnits(&p, testUI)
1136
-			for key, wantUnit := range test.wantUnits {
1137
-				unit := units[key]
1138
-				if wantUnit != unit {
1139
-					t.Errorf("for key %s, got unit %s, want unit %s", key, unit, wantUnit)
1140
-				}
1193
+			if !reflect.DeepEqual(test.wantUnits, units) {
1194
+				t.Errorf("got %v units, want %v", units, test.wantUnits)
1141 1195
 			}
1142 1196
 			if got, want := testUI.NumAllowRxMatches, test.wantIgnoreErrCount; want != got {
1143 1197
 				t.Errorf("got %d errors logged, want %d errors logged", got, want)

+ 8
- 12
profile/profile.go Переглянути файл

@@ -450,20 +450,16 @@ func (p *Profile) Aggregate(inlineFrame, function, filename, linenumber, address
450 450
 func (p *Profile) NumLabelUnits() (map[string]string, map[string][]string) {
451 451
 	numLabelUnits := map[string]string{}
452 452
 	ignoredUnits := map[string]map[string]bool{}
453
+	encounteredKeys := map[string]bool{}
453 454
 
454 455
 	// Determine units based on numeric tags for each sample.
455 456
 	for _, s := range p.Sample {
456
-		for k, vs := range s.NumLabel {
457
-			units := s.NumUnit[k]
458
-			if len(units) != len(vs) {
459
-				if _, ok := numLabelUnits[k]; !ok {
460
-					numLabelUnits[k] = ""
461
-				} else {
462
-					ignoredUnits[k] = map[string]bool{"": true}
457
+		for k := range s.NumLabel {
458
+			encounteredKeys[k] = true
459
+			for _, unit := range s.NumUnit[k] {
460
+				if unit == "" {
461
+					continue
463 462
 				}
464
-				continue
465
-			}
466
-			for _, unit := range units {
467 463
 				if wantUnit, ok := numLabelUnits[k]; !ok {
468 464
 					numLabelUnits[k] = unit
469 465
 				} else if wantUnit != unit {
@@ -476,10 +472,10 @@ func (p *Profile) NumLabelUnits() (map[string]string, map[string][]string) {
476 472
 			}
477 473
 		}
478 474
 	}
479
-
480 475
 	// Infer units for keys without any units associated with
481 476
 	// numeric tag values.
482
-	for key, unit := range numLabelUnits {
477
+	for key := range encounteredKeys {
478
+		unit := numLabelUnits[key]
483 479
 		if unit == "" {
484 480
 			switch key {
485 481
 			case "alignment", "request":

+ 21
- 20
profile/profile_test.go Переглянути файл

@@ -811,9 +811,9 @@ func locationHash(s *Sample) string {
811 811
 	return tb
812 812
 }
813 813
 
814
-func TestInferUnits(t *testing.T) {
814
+func TestNumLabelUnits(t *testing.T) {
815 815
 	var tagFilterTests = []struct {
816
-		name             string
816
+		desc             string
817 817
 		tagVals          []map[string][]int64
818 818
 		tagUnits         []map[string][]string
819 819
 		wantUnits        map[string]string
@@ -833,6 +833,13 @@ func TestInferUnits(t *testing.T) {
833 833
 			map[string]string{"key1": "bytes"},
834 834
 			map[string][]string{},
835 835
 		},
836
+		{
837
+			"One sample, one key with one value, empty unit specified",
838
+			[]map[string][]int64{{"key1": {8}}},
839
+			[]map[string][]string{{"key1": {""}}},
840
+			map[string]string{"key1": "key1"},
841
+			map[string][]string{},
842
+		},
836 843
 		{
837 844
 			"Key bytes, unit not specified",
838 845
 			[]map[string][]int64{{"bytes": {8}}},
@@ -862,12 +869,19 @@ func TestInferUnits(t *testing.T) {
862 869
 			map[string][]string{},
863 870
 		},
864 871
 		{
865
-			"One sample, one key with multiple values and different units",
872
+			"One sample, one key with multiple values and two different units",
866 873
 			[]map[string][]int64{{"key1": {8, 8}}},
867 874
 			[]map[string][]string{{"key1": {"bytes", "kilobytes"}}},
868 875
 			map[string]string{"key1": "bytes"},
869 876
 			map[string][]string{"key1": {"kilobytes"}},
870 877
 		},
878
+		{
879
+			"One sample, one key with multiple values and three different units",
880
+			[]map[string][]int64{{"key1": {8, 8}}},
881
+			[]map[string][]string{{"key1": {"bytes", "megabytes", "kilobytes"}}},
882
+			map[string]string{"key1": "bytes"},
883
+			map[string][]string{"key1": {"kilobytes", "megabytes"}},
884
+		},
871 885
 		{
872 886
 			"Two samples, one key, different units specified",
873 887
 			[]map[string][]int64{{"key1": {8}}, {"key1": {8}}},
@@ -905,24 +919,11 @@ func TestInferUnits(t *testing.T) {
905 919
 			p.Sample[i] = &s
906 920
 		}
907 921
 		units, ignoredUnits := p.NumLabelUnits()
908
-		for key, wantUnit := range test.wantUnits {
909
-			unit := units[key]
910
-			if wantUnit != unit {
911
-				t.Errorf("%s: for key %s, got unit %s, want unit %s", test.name, key, unit, wantUnit)
912
-			}
922
+		if !reflect.DeepEqual(test.wantUnits, units) {
923
+			t.Errorf("%s: got %v units, want %v", test.desc, units, test.wantUnits)
913 924
 		}
914
-		for key, ignored := range ignoredUnits {
915
-			wantIgnored := test.wantIgnoredUnits[key]
916
-			if len(wantIgnored) != len(ignored) {
917
-				t.Errorf("%s: for key %s, got ignored units %v, got ignored units %v", test.name, key, ignored, wantIgnored)
918
-				continue
919
-			}
920
-			for i, want := range wantIgnored {
921
-				if got := ignored[i]; want != got {
922
-					t.Errorf("%s: for key %s, got ignored units %v, want ignored units %v", test.name, key, ignored, wantIgnored)
923
-					break
924
-				}
925
-			}
925
+		if !reflect.DeepEqual(test.wantIgnoredUnits, ignoredUnits) {
926
+			t.Errorf("%s: got %v ignored units, want %v", test.desc, ignoredUnits, test.wantIgnoredUnits)
926 927
 		}
927 928
 	}
928 929
 }