...
1 package ldmodel
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/launchdarkly/go-jsonstream/v3/jreader"
8 )
9
10 var (
11 benchmarkBytesResult []byte
12 benchmarkErrorResult error
13 benchmarkFlagResult FeatureFlag
14 benchmarkSegmentResult Segment
15 )
16
17 func BenchmarkMarshalFlag(b *testing.B) {
18 b.Run("all properties", func(b *testing.B) {
19 for i := 0; i < b.N; i++ {
20 benchmarkBytesResult, benchmarkErrorResult =
21 jsonDataModelSerialization{}.MarshalFeatureFlag(flagWithAllProperties)
22 }
23 })
24
25 b.Run("minimal properties", func(b *testing.B) {
26 for i := 0; i < b.N; i++ {
27 benchmarkBytesResult, benchmarkErrorResult =
28 jsonDataModelSerialization{}.MarshalFeatureFlag(flagWithMinimalProperties)
29 }
30 })
31 }
32
33 func BenchmarkMarshalSegment(b *testing.B) {
34 b.Run("all properties", func(b *testing.B) {
35 for i := 0; i < b.N; i++ {
36 benchmarkBytesResult, benchmarkErrorResult =
37 jsonDataModelSerialization{}.MarshalSegment(segmentWithAllProperties)
38 }
39 })
40
41 b.Run("minimal properties", func(b *testing.B) {
42 for i := 0; i < b.N; i++ {
43 benchmarkBytesResult, benchmarkErrorResult =
44 jsonDataModelSerialization{}.MarshalSegment(segmentWithMinimalProperties)
45 }
46 })
47 }
48
49 func BenchmarkUnmarshalFlag(b *testing.B) {
50 b.Run("all properties", func(b *testing.B) {
51 bytes, _ := json.Marshal(flagWithAllPropertiesJSON)
52 b.ResetTimer()
53 for i := 0; i < b.N; i++ {
54 benchmarkFlagResult, benchmarkErrorResult =
55 jsonDataModelSerialization{}.UnmarshalFeatureFlag(bytes)
56 }
57 })
58
59 b.Run("minimal properties", func(b *testing.B) {
60 bytes, _ := json.Marshal(flagWithMinimalPropertiesJSON)
61 b.ResetTimer()
62 for i := 0; i < b.N; i++ {
63 benchmarkFlagResult, benchmarkErrorResult =
64 jsonDataModelSerialization{}.UnmarshalFeatureFlag(bytes)
65 }
66 })
67 }
68
69 func BenchmarkUnmarshalSegment(b *testing.B) {
70 b.Run("all properties", func(b *testing.B) {
71 bytes, _ := json.Marshal(segmentWithAllPropertiesJSON)
72 b.ResetTimer()
73 for i := 0; i < b.N; i++ {
74 benchmarkSegmentResult, benchmarkErrorResult =
75 jsonDataModelSerialization{}.UnmarshalSegment(bytes)
76 }
77 })
78
79 b.Run("minimal properties", func(b *testing.B) {
80 bytes, _ := json.Marshal(segmentWithMinimalPropertiesJSON)
81 b.ResetTimer()
82 for i := 0; i < b.N; i++ {
83 benchmarkSegmentResult, benchmarkErrorResult =
84 jsonDataModelSerialization{}.UnmarshalSegment(bytes)
85 }
86 })
87 }
88
89 func BenchmarkLargeFlagComparative(b *testing.B) {
90 b.Run("our unmarshaler", func(b *testing.B) {
91 bytes := makeLargeFlagJSON()
92 b.ResetTimer()
93 for i := 0; i < b.N; i++ {
94 r := jreader.NewReader(bytes)
95 var f FeatureFlag
96 readFeatureFlag(&r, &f)
97
98
99 benchmarkErrorResult = r.Error()
100 if benchmarkErrorResult != nil {
101 b.Error(benchmarkErrorResult)
102 b.FailNow()
103 }
104 }
105 })
106
107 b.Run("UnmarshalJSON for equivalent struct", func(b *testing.B) {
108 bytes := makeLargeFlagJSON()
109 b.ResetTimer()
110 for i := 0; i < b.N; i++ {
111 var f featureFlagEquivalentStruct
112 benchmarkErrorResult = json.Unmarshal(bytes, &f)
113 if benchmarkErrorResult != nil {
114 b.Error(benchmarkErrorResult)
115 b.FailNow()
116 }
117 }
118 })
119 }
120
121 func BenchmarkLargeSegmentComparative(b *testing.B) {
122 b.Run("our unmarshaler", func(b *testing.B) {
123 bytes := makeLargeSegmentJSON()
124 b.ResetTimer()
125 for i := 0; i < b.N; i++ {
126 r := jreader.NewReader(bytes)
127 var s Segment
128 readSegment(&r, &s)
129
130
131 benchmarkErrorResult = r.Error()
132 if benchmarkErrorResult != nil {
133 b.Error(benchmarkErrorResult)
134 b.FailNow()
135 }
136 }
137 })
138
139 b.Run("UnmarshalJSON for equivalent struct", func(b *testing.B) {
140 bytes := makeLargeSegmentJSON()
141 b.ResetTimer()
142 for i := 0; i < b.N; i++ {
143 var s segmentEquivalentStruct
144 benchmarkErrorResult = json.Unmarshal(bytes, &s)
145 if benchmarkErrorResult != nil {
146 b.Error(benchmarkErrorResult)
147 b.FailNow()
148 }
149 }
150 })
151 }
152
View as plain text