-
Notifications
You must be signed in to change notification settings - Fork 18
/
ldclient_evaluation_benchmark_test.go
564 lines (516 loc) · 16.6 KB
/
ldclient_evaluation_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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package ldclient
import (
"encoding/json"
"fmt"
"testing"
"github.com/launchdarkly/go-sdk-common/v3/ldattr"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/lduser"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
ldevents "github.com/launchdarkly/go-sdk-events/v3"
"github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders"
"github.com/launchdarkly/go-server-sdk-evaluation/v3/ldmodel"
"github.com/launchdarkly/go-server-sdk/v7/internal/datakinds"
"github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
)
// These benchmarks cover the LDClient evaluation flow, including looking up the target flag, applying all
// relevant targets and rules, and producing analytics event data (but then discarding the event data,
// so the event processor logic is not included).
//
// This was adapted from a user-contributed PR: https://github.com/launchdarkly/go-server-sdk/pull/28
// Note about heap allocations:
//
// Benchmarks whose names end in "NoAlloc" are expected _not_ to cause any heap allocations (not counting
// setup work done before ResetTimer()). This is enforced by the Makefile's benchmarks target. As long as
// events are disabled, it should be possible to do most kinds of flag evaluations without causing any
// heap allocations.
//
// See notes about heap allocations in CONTRIBUTING.md.
type evalBenchmarkEnv struct {
client *LDClient
evalUser ldcontext.Context
targetFeatureKey string
targetUsers []ldcontext.Context
}
func newEvalBenchmarkEnv() *evalBenchmarkEnv {
return &evalBenchmarkEnv{}
}
func (env *evalBenchmarkEnv) setUp(withEventGeneration bool, bc evalBenchmarkCase, variations []ldvalue.Value) {
// Set up the client.
env.client = makeTestClientWithConfigAndStore(func(c *Config) {
if withEventGeneration {
// In this mode, we use a stub EventProcessor implementation that immediately discards
// every event, but the SDK will still generate the events before passing them to the stub,
// so we are still measuring the overhead of that.
c.Events = benchmarkStubEventProcessorFactory{}
} else {
// Completely disable all event functionality, so we are only testing the evaluation logic
// (plus retrieval of the flag from the in-memory store). The SDK only behaves this way if
// Events is set to the specific factory returned by NoEvents().
c.Events = ldcomponents.NoEvents()
}
}, func(store subsystems.DataStore) {
// Set up the feature flag store. Note that we're using a regular in-memory data store, so the
// benchmarks will include the overhead of calling Get on the store.
testFlags := makeEvalBenchmarkFlags(bc, variations)
for _, ff := range testFlags {
_, _ = store.Upsert(datakinds.Features, ff.Key, sharedtest.FlagDescriptor(*ff))
}
})
env.evalUser = makeEvalBenchmarkUser(bc)
// Target a feature key in the middle of the list in case a linear search is being used.
targetFeatureKeyIndex := 0
if bc.numFlags > 0 {
targetFeatureKeyIndex = bc.numFlags / 2
}
env.targetFeatureKey = fmt.Sprintf("flag-%d", targetFeatureKeyIndex)
// Create users to match all of the user keys in the flag's target list. These will be used
// only in BenchmarkUsersFoundInTargets; with all the other benchmarks, we are deliberately
// using a user key that is *not* found in the targets.
env.targetUsers = make([]ldcontext.Context, bc.numTargets)
for i := 0; i < bc.numTargets; i++ {
env.targetUsers[i] = lduser.NewUser(makeEvalBenchmarkTargetUserKey(i))
}
}
func (env *evalBenchmarkEnv) tearDown() {
// Prepare for the next benchmark case.
env.client.Close()
env.client = nil
env.targetFeatureKey = ""
}
type benchmarkStubEventProcessorFactory struct{}
func (f benchmarkStubEventProcessorFactory) Build(context subsystems.ClientContext) (ldevents.EventProcessor, error) {
return ldcomponents.NoEvents().Build(context)
}
func makeEvalBenchmarkUser(bc evalBenchmarkCase) ldcontext.Context {
if bc.shouldMatch {
builder := lduser.NewUserBuilder("user-match")
switch bc.operator {
case ldmodel.OperatorGreaterThan:
builder.Custom("numAttr", ldvalue.Int(10000))
case ldmodel.OperatorContains:
builder.Name("name-0")
case ldmodel.OperatorMatches:
builder.Custom("stringAttr", ldvalue.String("stringAttr-0"))
case ldmodel.OperatorAfter:
builder.Custom("dateAttr", ldvalue.String("2999-12-31T00:00:00.000-00:00"))
case ldmodel.OperatorIn:
builder.Custom("stringAttr", ldvalue.String("stringAttr-0"))
}
return builder.Build()
}
// default is that the user will not be matched by any clause or target
return lduser.NewUserBuilder("user-nomatch").
Name("name-nomatch").
Custom("stringAttr", ldvalue.String("stringAttr-nomatch")).
Custom("numAttr", ldvalue.Int(0)).
Custom("dateAttr", ldvalue.String("1980-01-01T00:00:00.000-00:00")).
Build()
}
type evalBenchmarkCase struct {
numUsers int
numFlags int
numVariations int
numTargets int
numRules int
numClauses int
prereqsWidth int
prereqsDepth int
operator ldmodel.Operator
shouldMatch bool
}
var ruleEvalBenchmarkCases = []evalBenchmarkCase{
// simple
{
numUsers: 1000,
numFlags: 1000,
numVariations: 2,
numTargets: 1,
},
// realistic
{
numUsers: 10000,
numFlags: 10000,
numVariations: 2,
numTargets: 1,
},
{
numUsers: 10000,
numFlags: 10000,
numVariations: 2,
numTargets: 10,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 3,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 5,
numClauses: 3,
},
// prereqs
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
prereqsWidth: 5,
prereqsDepth: 1,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
prereqsWidth: 1,
prereqsDepth: 5,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numTargets: 1,
prereqsWidth: 2,
prereqsDepth: 2,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
prereqsWidth: 5,
prereqsDepth: 5,
},
// operations - if not specified, the default is OperatorIn
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
operator: ldmodel.OperatorGreaterThan,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
operator: ldmodel.OperatorContains,
},
{
numUsers: 10000,
numFlags: 1000,
numVariations: 2,
numRules: 1,
numClauses: 1,
operator: ldmodel.OperatorMatches,
},
}
var targetMatchBenchmarkCases = []evalBenchmarkCase{
{
numUsers: 1000,
numFlags: 1000,
numVariations: 2,
numTargets: 10,
},
{
numUsers: 1000,
numFlags: 1000,
numVariations: 2,
numTargets: 100,
},
{
numUsers: 1000,
numFlags: 1000,
numVariations: 2,
numTargets: 1000,
},
}
var ruleMatchBenchmarkCases = []evalBenchmarkCase{
// These cases are deliberately simple because the benchmark is meant to focus on the evaluation of
// one specific type of matching operation. The user will match the first clause in the first rule.
{
numFlags: 1,
numRules: 1,
numClauses: 1,
numVariations: 2,
operator: ldmodel.OperatorIn,
shouldMatch: true,
},
{
numFlags: 1,
numRules: 1,
numClauses: 1,
numVariations: 2,
operator: ldmodel.OperatorContains,
shouldMatch: true,
},
{
numFlags: 1,
numRules: 1,
numClauses: 1,
numVariations: 2,
operator: ldmodel.OperatorGreaterThan,
shouldMatch: true,
},
{
numFlags: 1,
numRules: 1,
numClauses: 1,
numVariations: 2,
operator: ldmodel.OperatorAfter,
shouldMatch: true,
},
{
numFlags: 1,
numRules: 1,
numClauses: 1,
numVariations: 2,
operator: ldmodel.OperatorMatches,
shouldMatch: true,
},
}
var (
// Always record the result of an operation to prevent the compiler eliminating the function call.
//
// Always store the result to a package level variable so the compiler cannot eliminate the benchmark itself.
boolResult bool
intResult int
stringResult string
jsonResult ldvalue.Value
)
func benchmarkEval(
b *testing.B,
withEventGeneration bool,
makeVariation func(int) ldvalue.Value,
cases []evalBenchmarkCase,
action func(*evalBenchmarkEnv),
) {
env := newEvalBenchmarkEnv()
for _, bc := range cases {
variations := make([]ldvalue.Value, bc.numVariations)
for i := 0; i < bc.numVariations; i++ {
variations[i] = makeVariation(i)
}
env.setUp(withEventGeneration, bc, variations)
b.Run(fmt.Sprintf("%+v", bc), func(b *testing.B) {
for i := 0; i < b.N; i++ {
action(env)
}
})
env.tearDown()
}
}
// This benchmark executes only a single basic evaluation case. It is mainly useful in very
// detailed profiling and allocation tracing where you don't want a huge log file.
func BenchmarkSingleVariation(b *testing.B) {
singleCase := []evalBenchmarkCase{ruleEvalBenchmarkCases[0]}
benchmarkEval(b, false, makeBoolVariation, singleCase, func(env *evalBenchmarkEnv) {
boolResult, _ = env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
})
}
func BenchmarkSingleVariationWithEvents(b *testing.B) {
singleCase := []evalBenchmarkCase{ruleEvalBenchmarkCases[0]}
benchmarkEval(b, true, makeBoolVariation, singleCase, func(env *evalBenchmarkEnv) {
boolResult, _ = env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
})
}
func BenchmarkBoolVariationNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeBoolVariation, ruleEvalBenchmarkCases, func(env *evalBenchmarkEnv) {
boolResult, _ = env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
})
}
// The ___WithEvents version of the benchmark enables the LDClient code path that creates an evaluation
// event instance, even though the event will not be sent anywhere, so we can measure the overhead of
// that step. It is not repeated for BenchmarkIntVariation, etc., because the data type of the
// variation makes no difference in how events are generated.
func BenchmarkBoolVariationWithEvents(b *testing.B) {
benchmarkEval(b, true, makeBoolVariation, ruleEvalBenchmarkCases, func(env *evalBenchmarkEnv) {
boolResult, _ = env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
})
}
func BenchmarkIntVariationNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeIntVariation, ruleEvalBenchmarkCases, func(env *evalBenchmarkEnv) {
intResult, _ = env.client.IntVariation(env.targetFeatureKey, env.evalUser, 0)
})
}
func BenchmarkStringVariationNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeStringVariation, ruleEvalBenchmarkCases, func(env *evalBenchmarkEnv) {
stringResult, _ = env.client.StringVariation(env.targetFeatureKey, env.evalUser, "variation-0")
})
}
func BenchmarkJSONVariationNoAlloc(b *testing.B) {
defaultValAsRawJSON := ldvalue.Raw(json.RawMessage(`{"result":{"value":[0]}}`))
benchmarkEval(b, false, makeJSONVariation, ruleEvalBenchmarkCases, func(env *evalBenchmarkEnv) {
jsonResult, _ = env.client.JSONVariation(env.targetFeatureKey, env.evalUser, defaultValAsRawJSON)
})
}
func BenchmarkUsersFoundInTargetsNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeBoolVariation,
targetMatchBenchmarkCases,
func(env *evalBenchmarkEnv) {
for _, user := range env.targetUsers {
r, _ := env.client.BoolVariation(env.targetFeatureKey, user, false)
boolResult = r
}
})
}
func BenchmarkUserNotFoundInTargetsNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeBoolVariation,
targetMatchBenchmarkCases,
func(env *evalBenchmarkEnv) {
for range env.targetUsers {
r, _ := env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
boolResult = r
}
})
}
func BenchmarkUserMatchesRuleNoAlloc(b *testing.B) {
benchmarkEval(b, false, makeBoolVariation,
ruleMatchBenchmarkCases,
func(env *evalBenchmarkEnv) {
boolResult, _ = env.client.BoolVariation(env.targetFeatureKey, env.evalUser, false)
})
}
// Input data creation
// Except for when we're running BenchmarkUserMatchesRule, the flag rules and clauses we create here are
// intended *not* to match the user, so the more of them we create, the more we are testing the overhead
// of iterating through and evaluating all the clauses.
func makeBoolVariation(i int) ldvalue.Value {
return ldvalue.Bool(i%2 == 0)
}
func makeIntVariation(i int) ldvalue.Value {
return ldvalue.Int(i)
}
func makeStringVariation(i int) ldvalue.Value {
return ldvalue.String(fmt.Sprintf("variation-%d", i))
}
func makeJSONVariation(i int) ldvalue.Value {
return ldvalue.ObjectBuild().Set(
"result",
ldvalue.ObjectBuild().Set("value", ldvalue.ArrayOf(ldvalue.Int(i))).Build(),
).Build()
}
func makeEvalBenchmarkClauses(numClauses int, op ldmodel.Operator) []ldmodel.Clause {
clauses := make([]ldmodel.Clause, 0, numClauses)
for i := 0; i < numClauses; i++ {
clause := ldmodel.Clause{Op: op}
switch op {
case ldmodel.OperatorGreaterThan:
clause.Attribute = ldattr.NewLiteralRef("numAttr")
clause.Values = []ldvalue.Value{ldvalue.Int(i)}
case ldmodel.OperatorContains:
clause.Attribute = ldattr.NewLiteralRef("name")
clause.Values = []ldvalue.Value{
ldvalue.String(fmt.Sprintf("name-%d", i)),
ldvalue.String(fmt.Sprintf("name-%d", i+1)),
ldvalue.String(fmt.Sprintf("name-%d", i+2)),
}
case ldmodel.OperatorMatches:
clause.Attribute = ldattr.NewLiteralRef("stringAttr")
clause.Values = []ldvalue.Value{
ldvalue.String(fmt.Sprintf("stringAttr-%d", i)),
ldvalue.String(fmt.Sprintf("stringAttr-%d", i+1)),
ldvalue.String(fmt.Sprintf("stringAttr-%d", i+2)),
}
case ldmodel.OperatorAfter:
clause.Attribute = ldattr.NewLiteralRef("dateAttr")
clause.Values = []ldvalue.Value{
ldvalue.String(fmt.Sprintf("%d-01-01T00:00:00.000-00:00", 2000+i)),
ldvalue.String(fmt.Sprintf("%d-01-01T00:00:00.000-00:00", 2001+i)),
ldvalue.String(fmt.Sprintf("%d-01-01T00:00:00.000-00:00", 2002+i)),
}
default:
clause.Op = ldmodel.OperatorIn
clause.Attribute = ldattr.NewLiteralRef("stringAttr")
clause.Values = []ldvalue.Value{
ldvalue.String(fmt.Sprintf("stringAttr-%d", i)),
ldvalue.String(fmt.Sprintf("stringAttr-%d", i+1)),
ldvalue.String(fmt.Sprintf("stringAttr-%d", i+2)),
}
}
clauses = append(clauses, clause)
}
return clauses
}
func makeEvalBenchmarkTargetUserKey(i int) string {
return fmt.Sprintf("user-%d", i)
}
func makeEvalBenchmarkFlags(bc evalBenchmarkCase, variations []ldvalue.Value) []*ldmodel.FeatureFlag {
testFlags := make([]*ldmodel.FeatureFlag, 0, bc.numFlags)
for i := 0; i < bc.numFlags; i++ {
flag := ldbuilders.NewFlagBuilder(fmt.Sprintf("flag-%d", i)).
Version(1).
On(true).
Variations(variations...).
FallthroughVariation(1)
for j := 0; j < bc.numVariations; j++ {
values := make([]string, bc.numTargets)
for k := 0; k < bc.numTargets; k++ {
values[k] = makeEvalBenchmarkTargetUserKey(k)
}
flag.AddTarget(j, values...)
}
for j := 0; j < bc.numRules; j++ {
flag.AddRule(ldbuilders.NewRuleBuilder().
ID(fmt.Sprintf("%d-%d", i, j)).
Clauses(makeEvalBenchmarkClauses(bc.numClauses, bc.operator)...).
Variation(0))
}
f := flag.Build()
testFlags = append(testFlags, &f)
}
if bc.prereqsWidth > 0 && bc.prereqsDepth > 0 {
assignPrereqTree(testFlags, bc.prereqsWidth, bc.prereqsDepth)
}
return testFlags
}
// assignPrereqTree assigns prerequisites to each of the given feature flags such that each flag
// has at most `width` children and `depth` ancestors. If the depth of the prerequisite "tree"
// exceeds `depth`, a new tree is assigned starting with the next feature flag the root node.
func assignPrereqTree(flags []*ldmodel.FeatureFlag, width, depth int) {
var parentLevel []*ldmodel.FeatureFlag
levelIndex := 0
i := 0
for i < len(flags) {
if levelIndex > depth {
levelIndex = 0
parentLevel = []*ldmodel.FeatureFlag{flags[i]}
}
if levelIndex == 0 {
levelIndex++
i++
continue
}
var childLevel []*ldmodel.FeatureFlag
for _, parent := range parentLevel {
for w := 0; w < width && i+w < len(flags); w++ {
child := flags[i+w]
child.Prerequisites = []ldmodel.Prerequisite{{Key: parent.Key, Variation: 0}}
childLevel = append(childLevel, child)
}
i += width
}
parentLevel = childLevel
levelIndex++
}
}