File size: 5,358 Bytes
de452ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pprof

import (
	"context"
	"fmt"
	"internal/runtime/pprof/label"
	"reflect"
	"slices"
	"strings"
	"testing"
)

func labelsSorted(ctx context.Context) []label.Label {
	ls := []label.Label{}
	ForLabels(ctx, func(key, value string) bool {
		ls = append(ls, label.Label{Key: key, Value: value})
		return true
	})
	slices.SortFunc(ls, func(a, b label.Label) int { return strings.Compare(a.Key, b.Key) })
	return ls
}

func TestContextLabels(t *testing.T) {
	// Background context starts with no labels.
	ctx := context.Background()
	labels := labelsSorted(ctx)
	if len(labels) != 0 {
		t.Errorf("labels on background context: want [], got %v ", labels)
	}

	// Add a single label.
	ctx = WithLabels(ctx, Labels("key", "value"))
	// Retrieve it with Label.
	v, ok := Label(ctx, "key")
	if !ok || v != "value" {
		t.Errorf(`Label(ctx, "key"): got %v, %v; want "value", ok`, v, ok)
	}
	gotLabels := labelsSorted(ctx)
	wantLabels := []label.Label{{Key: "key", Value: "value"}}
	if !reflect.DeepEqual(gotLabels, wantLabels) {
		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
	}

	// Add a label with a different key.
	ctx = WithLabels(ctx, Labels("key2", "value2"))
	v, ok = Label(ctx, "key2")
	if !ok || v != "value2" {
		t.Errorf(`Label(ctx, "key2"): got %v, %v; want "value2", ok`, v, ok)
	}
	gotLabels = labelsSorted(ctx)
	wantLabels = []label.Label{{Key: "key", Value: "value"}, {Key: "key2", Value: "value2"}}
	if !reflect.DeepEqual(gotLabels, wantLabels) {
		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
	}

	// Add label with first key to test label replacement.
	ctx = WithLabels(ctx, Labels("key", "value3"))
	v, ok = Label(ctx, "key")
	if !ok || v != "value3" {
		t.Errorf(`Label(ctx, "key3"): got %v, %v; want "value3", ok`, v, ok)
	}
	gotLabels = labelsSorted(ctx)
	wantLabels = []label.Label{{Key: "key", Value: "value3"}, {Key: "key2", Value: "value2"}}
	if !reflect.DeepEqual(gotLabels, wantLabels) {
		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
	}

	// Labels called with two labels with the same key should pick the second.
	ctx = WithLabels(ctx, Labels("key4", "value4a", "key4", "value4b"))
	v, ok = Label(ctx, "key4")
	if !ok || v != "value4b" {
		t.Errorf(`Label(ctx, "key4"): got %v, %v; want "value4b", ok`, v, ok)
	}
	gotLabels = labelsSorted(ctx)
	wantLabels = []label.Label{{Key: "key", Value: "value3"}, {Key: "key2", Value: "value2"}, {Key: "key4", Value: "value4b"}}
	if !reflect.DeepEqual(gotLabels, wantLabels) {
		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
	}
}

func TestLabelMapStringer(t *testing.T) {
	for _, tbl := range []struct {
		m        labelMap
		expected string
	}{
		{
			m: labelMap{
				// empty map
			},
			expected: "{}",
		}, {
			m: labelMap{
				label.NewSet(Labels("foo", "bar").list),
			},
			expected: `{"foo":"bar"}`,
		}, {
			m: labelMap{
				label.NewSet(Labels(
					"foo", "bar",
					"key1", "value1",
					"key2", "value2",
					"key3", "value3",
					"key4WithNewline", "\nvalue4",
				).list),
			},
			expected: `{"foo":"bar", "key1":"value1", "key2":"value2", "key3":"value3", "key4WithNewline":"\nvalue4"}`,
		},
	} {
		if got := tbl.m.String(); tbl.expected != got {
			t.Errorf("%#v.String() = %q; want %q", tbl.m, got, tbl.expected)
		}
	}
}

func BenchmarkLabels(b *testing.B) {
	b.Run("set-one", func(b *testing.B) {
		b.ReportAllocs()
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			Do(context.Background(), Labels("key", "value"), func(context.Context) {})
		}
	})

	b.Run("merge-one", func(b *testing.B) {
		ctx := WithLabels(context.Background(), Labels("key1", "val1"))

		b.ReportAllocs()
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			Do(ctx, Labels("key2", "value2"), func(context.Context) {})
		}
	})

	b.Run("overwrite-one", func(b *testing.B) {
		ctx := WithLabels(context.Background(), Labels("key", "val"))

		b.ReportAllocs()
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			Do(ctx, Labels("key", "value"), func(context.Context) {})
		}
	})

	for _, scenario := range []string{"ordered", "unordered"} {
		var labels []string
		for i := 0; i < 10; i++ {
			labels = append(labels, fmt.Sprintf("key%03d", i), fmt.Sprintf("value%03d", i))
		}
		if scenario == "unordered" {
			labels[0], labels[len(labels)-1] = labels[len(labels)-1], labels[0]
		}

		b.Run(scenario, func(b *testing.B) {
			b.Run("set-many", func(b *testing.B) {
				b.ReportAllocs()
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					Do(context.Background(), Labels(labels...), func(context.Context) {})
				}
			})

			b.Run("merge-many", func(b *testing.B) {
				ctx := WithLabels(context.Background(), Labels(labels[:len(labels)/2]...))

				b.ResetTimer()
				b.ReportAllocs()
				for i := 0; i < b.N; i++ {
					Do(ctx, Labels(labels[len(labels)/2:]...), func(context.Context) {})
				}
			})

			b.Run("overwrite-many", func(b *testing.B) {
				ctx := WithLabels(context.Background(), Labels(labels...))

				b.ReportAllocs()
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					Do(ctx, Labels(labels...), func(context.Context) {})
				}
			})
		})
	}

	// TODO: hit slow path in Labels
}