浏览代码

Fix unit warnings (#240)

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
 	// Print errors for tags with multiple units associated with
290
 	// Print errors for tags with multiple units associated with
291
 	// a single key.
291
 	// a single key.
292
 	for k, units := range ignoredUnits {
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
 	return numLabelUnits
295
 	return numLabelUnits
296
 }
296
 }

+ 71
- 17
internal/driver/driver_test.go 查看文件

22
 	"net"
22
 	"net"
23
 	_ "net/http/pprof"
23
 	_ "net/http/pprof"
24
 	"os"
24
 	"os"
25
+	"reflect"
25
 	"regexp"
26
 	"regexp"
26
 	"runtime"
27
 	"runtime"
27
 	"strconv"
28
 	"strconv"
1077
 		wantIgnoreErrCount int
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
 			0,
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
 			1,
1134
 			1,
1094
 		},
1135
 		},
1095
 		{
1136
 		{
1096
 			"One key with different units in different samples",
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
 			map[string]string{"key1": "bytes"},
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
 			1,
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
 			"Check units not over-written for keys with default units",
1161
 			"Check units not over-written for keys with default units",
1105
 			[]map[string][]int64{{
1162
 			[]map[string][]int64{{
1133
 			}
1190
 			}
1134
 			testUI := &proftest.TestUI{T: t, AllowRx: test.allowedRx}
1191
 			testUI := &proftest.TestUI{T: t, AllowRx: test.allowedRx}
1135
 			units := identifyNumLabelUnits(&p, testUI)
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
 			if got, want := testUI.NumAllowRxMatches, test.wantIgnoreErrCount; want != got {
1196
 			if got, want := testUI.NumAllowRxMatches, test.wantIgnoreErrCount; want != got {
1143
 				t.Errorf("got %d errors logged, want %d errors logged", got, want)
1197
 				t.Errorf("got %d errors logged, want %d errors logged", got, want)

+ 8
- 12
profile/profile.go 查看文件

450
 func (p *Profile) NumLabelUnits() (map[string]string, map[string][]string) {
450
 func (p *Profile) NumLabelUnits() (map[string]string, map[string][]string) {
451
 	numLabelUnits := map[string]string{}
451
 	numLabelUnits := map[string]string{}
452
 	ignoredUnits := map[string]map[string]bool{}
452
 	ignoredUnits := map[string]map[string]bool{}
453
+	encounteredKeys := map[string]bool{}
453
 
454
 
454
 	// Determine units based on numeric tags for each sample.
455
 	// Determine units based on numeric tags for each sample.
455
 	for _, s := range p.Sample {
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
 				if wantUnit, ok := numLabelUnits[k]; !ok {
463
 				if wantUnit, ok := numLabelUnits[k]; !ok {
468
 					numLabelUnits[k] = unit
464
 					numLabelUnits[k] = unit
469
 				} else if wantUnit != unit {
465
 				} else if wantUnit != unit {
476
 			}
472
 			}
477
 		}
473
 		}
478
 	}
474
 	}
479
-
480
 	// Infer units for keys without any units associated with
475
 	// Infer units for keys without any units associated with
481
 	// numeric tag values.
476
 	// numeric tag values.
482
-	for key, unit := range numLabelUnits {
477
+	for key := range encounteredKeys {
478
+		unit := numLabelUnits[key]
483
 		if unit == "" {
479
 		if unit == "" {
484
 			switch key {
480
 			switch key {
485
 			case "alignment", "request":
481
 			case "alignment", "request":

+ 21
- 20
profile/profile_test.go 查看文件

811
 	return tb
811
 	return tb
812
 }
812
 }
813
 
813
 
814
-func TestInferUnits(t *testing.T) {
814
+func TestNumLabelUnits(t *testing.T) {
815
 	var tagFilterTests = []struct {
815
 	var tagFilterTests = []struct {
816
-		name             string
816
+		desc             string
817
 		tagVals          []map[string][]int64
817
 		tagVals          []map[string][]int64
818
 		tagUnits         []map[string][]string
818
 		tagUnits         []map[string][]string
819
 		wantUnits        map[string]string
819
 		wantUnits        map[string]string
833
 			map[string]string{"key1": "bytes"},
833
 			map[string]string{"key1": "bytes"},
834
 			map[string][]string{},
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
 			"Key bytes, unit not specified",
844
 			"Key bytes, unit not specified",
838
 			[]map[string][]int64{{"bytes": {8}}},
845
 			[]map[string][]int64{{"bytes": {8}}},
862
 			map[string][]string{},
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
 			[]map[string][]int64{{"key1": {8, 8}}},
873
 			[]map[string][]int64{{"key1": {8, 8}}},
867
 			[]map[string][]string{{"key1": {"bytes", "kilobytes"}}},
874
 			[]map[string][]string{{"key1": {"bytes", "kilobytes"}}},
868
 			map[string]string{"key1": "bytes"},
875
 			map[string]string{"key1": "bytes"},
869
 			map[string][]string{"key1": {"kilobytes"}},
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
 			"Two samples, one key, different units specified",
886
 			"Two samples, one key, different units specified",
873
 			[]map[string][]int64{{"key1": {8}}, {"key1": {8}}},
887
 			[]map[string][]int64{{"key1": {8}}, {"key1": {8}}},
905
 			p.Sample[i] = &s
919
 			p.Sample[i] = &s
906
 		}
920
 		}
907
 		units, ignoredUnits := p.NumLabelUnits()
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
 }