1 package ldevents
2
3 import (
4 "testing"
5
6 "github.com/launchdarkly/go-sdk-common/v3/ldattr"
7 "github.com/launchdarkly/go-sdk-common/v3/ldreason"
8 "github.com/launchdarkly/go-sdk-common/v3/ldtime"
9 "github.com/launchdarkly/go-sdk-common/v3/lduser"
10 "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
11 )
12
13 func BenchmarkEventOutputFormatterBasicEvents(b *testing.B) {
14 events := makeBasicEvents()
15 ef := eventOutputFormatter{}
16 b.ResetTimer()
17
18 for i := 0; i < b.N; i++ {
19 benchmarkBytesResult, _ = ef.makeOutputEvents(events, eventSummary{})
20 }
21 }
22
23 func BenchmarkEventOutputFormatterBasicEventsWithPrivateAttributes(b *testing.B) {
24 events := makeBasicEvents()
25 ef := eventOutputFormatter{
26 contextFormatter: newEventContextFormatter(EventsConfiguration{
27 PrivateAttributes: []ldattr.Ref{
28 ldattr.NewLiteralRef("name"),
29 ldattr.NewLiteralRef("custom-attr"),
30 },
31 }),
32 }
33 b.ResetTimer()
34
35 for i := 0; i < b.N; i++ {
36 benchmarkBytesResult, _ = ef.makeOutputEvents(events, eventSummary{})
37 }
38 }
39
40 func makeBasicEvents() []anyEventOutput {
41 baseEvent := BaseEvent{
42 CreationDate: ldtime.UnixMillisNow(),
43 Context: Context(lduser.NewUserBuilder("user-key").
44 Email("test@example.com").
45 Name("user-name").
46 Custom("custom-attr", ldvalue.Bool(true)).
47 Build()),
48 }
49 return []anyEventOutput{
50 EvaluationData{
51 BaseEvent: baseEvent,
52 Key: "flag1",
53 Variation: ldvalue.NewOptionalInt(1),
54 Value: ldvalue.Bool(true),
55 Default: ldvalue.Bool(false),
56 Reason: ldreason.NewEvalReasonFallthrough(),
57 Version: ldvalue.NewOptionalInt(10),
58 },
59 CustomEventData{
60 BaseEvent: baseEvent,
61 Key: "event1",
62 Data: ldvalue.String("data"),
63 HasMetric: true,
64 MetricValue: 1234,
65 },
66 IdentifyEventData{BaseEvent: baseEvent},
67 indexEvent{BaseEvent: baseEvent},
68 }
69 }
70
71 func BenchmarkEventOutputSummaryMultipleCounters(b *testing.B) {
72 user := Context(lduser.NewUser("u"))
73 flag1v1 := FlagEventProperties{Key: "flag1", Version: 100}
74 flag1v2 := FlagEventProperties{Key: "flag1", Version: 200}
75 flag1Default := ldvalue.String("default1")
76 flag2 := FlagEventProperties{Key: "flag2", Version: 1}
77 flag2Default := ldvalue.String("default2")
78 factory := NewEventFactory(false, fakeTimeFn)
79
80 ef := eventOutputFormatter{config: basicConfigWithoutPrivateAttrs()}
81
82 es := newEventSummarizer()
83 es.summarizeEvent(factory.NewEvaluationData(flag1v1, user, ldreason.NewEvaluationDetail(ldvalue.String("a"), 1, noReason),
84 false, flag1Default, ""))
85 es.summarizeEvent(factory.NewEvaluationData(flag1v1, user, ldreason.NewEvaluationDetail(ldvalue.String("b"), 2, noReason),
86 false, flag1Default, ""))
87 es.summarizeEvent(factory.NewEvaluationData(flag1v1, user, ldreason.NewEvaluationDetail(ldvalue.String("a"), 1, noReason),
88 false, flag1Default, ""))
89 es.summarizeEvent(factory.NewEvaluationData(flag1v2, user, ldreason.NewEvaluationDetail(ldvalue.String("a"), 1, noReason),
90 false, flag1Default, ""))
91 es.summarizeEvent(factory.NewEvaluationData(flag2, user, ldreason.NewEvaluationDetail(ldvalue.String("c"), 3, noReason),
92 false, flag2Default, ""))
93 summary := es.snapshot()
94
95 b.ResetTimer()
96
97 for i := 0; i < b.N; i++ {
98 benchmarkBytesResult, _ = ef.makeOutputEvents(nil, summary)
99 }
100 }
101
View as plain text