-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_benchmark_test.go
163 lines (145 loc) · 2.88 KB
/
manager_benchmark_test.go
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
package pectx_test
import (
"context"
"fmt"
"testing"
"github.com/pragmaticengineering/pectx"
)
func helperFields(amount int) map[string]string {
store := map[string]string{}
for i := 0; i < amount; i++ {
key := fmt.Sprintf("k%d", i)
store[key] = fmt.Sprintf("v%d", i)
}
return store
}
func BenchmarkManager_Set(b *testing.B) {
var contextKey = "test123123123"
testCases := []struct {
name string
fields map[string]string
mgr *pectx.Manager
ctx context.Context
}{
{
name: "manager with 1 field",
fields: helperFields(1),
mgr: pectx.NewManager(contextKey),
ctx: context.Background(),
},
{
name: "manager with 10 fields",
fields: helperFields(10),
mgr: pectx.NewManager(contextKey),
ctx: context.Background(),
},
{
name: "manager with 100 fields",
fields: helperFields(100),
mgr: pectx.NewManager(contextKey),
ctx: context.Background(),
},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
tc.mgr.Set(tc.ctx, tc.fields)
}
})
}
}
func helperFieldsGet(amount int) (context.Context, *pectx.Manager) {
ctx := context.Background()
m := pectx.NewManager(contextKey)
m.Set(ctx, helperFields(amount))
return ctx, m
}
func BenchmarkManager_Get(b *testing.B) {
testCases := []struct {
name string
amount int
}{
{
name: "context with 1 field",
amount: 1,
},
{
name: "context with 10 fields",
amount: 10,
},
{
name: "context with 100 fields",
amount: 100,
},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
ctx, m := helperFieldsGet(tc.amount)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Get(ctx)
}
})
}
}
func BenchmarkManager_GetKeysAndValues(b *testing.B) {
testCases := []struct {
name string
amount int
}{
{
name: "context with 1 field",
amount: 1,
},
{
name: "context with 10 fields",
amount: 10,
},
{
name: "context with 100 fields",
amount: 100,
},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
ctx, m := helperFieldsGet(tc.amount)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.GetKeysAndValues(ctx)
}
})
}
}
func BenchmarkKeysAndValues_forloop(b *testing.B) {
manager := pectx.NewManager(contextKey)
// Define test cases
testCases := []struct {
name string
data map[string]string
}{
{
name: "Set 1 key-value pair",
data: map[string]string{
"key1": "value1",
},
},
{
name: "Set 10 key-value pairs",
data: helperFields(10),
},
{
name: "Set 100 key-value pairs",
data: helperFields(100),
},
}
for _, tc := range testCases {
testcase := tc
b.Run(testcase.name, func(b *testing.B) {
ctx := manager.Set(context.Background(), testcase.data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
manager.GetKeysAndValues(ctx)
}
})
}
}