Skip to content

Commit c993d8e

Browse files
Add benchmark and complex tests for metadata
Before we can fix the metadata performance and memory issues we need to have a baseline.
1 parent 8ca50cd commit c993d8e

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

metadata/metadata_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ var jsonTestCases = []struct {
172172
"another": "value"
173173
}`,
174174
},
175+
{
176+
"metadata with 'complex' json",
177+
func() metadata.Metadata {
178+
m := metadata.New()
179+
m = metadata.WithValue(m, "test", nil)
180+
m = metadata.WithValue(m, "another", "value")
181+
m = metadata.WithValue(m, "arr", []interface{}{"a", "b", "c"})
182+
m = metadata.WithValue(m, "arrInArr", []interface{}{"a", []interface{}{"b"}})
183+
m = metadata.WithValue(m, "obj", map[string]interface{}{"a": float64(1)})
184+
m = metadata.WithValue(m, "objInObj", map[string]interface{}{
185+
"a": float64(1),
186+
"b": map[string]interface{}{"a": float64(2)},
187+
})
188+
return m
189+
},
190+
`{
191+
"test": null,
192+
"another": "value",
193+
"arr": [ "a", "b", "c" ],
194+
"arrInArr": [ "a", [ "b" ] ],
195+
"obj": { "a": 1 },
196+
"objInObj": { "a": 1, "b": {"a": 2} }
197+
}`,
198+
},
175199
}
176200

177201
func TestMetadata_MarshalJSON(t *testing.T) {
@@ -209,8 +233,22 @@ func TestJSONMetadata_UnmarshalJSON(t *testing.T) {
209233
err := json.Unmarshal([]byte(testCase.json), &m)
210234

211235
// Need to use AsMap otherwise we can have inconsistent tests results.
212-
assert.Equal(t, testCase.metadata().AsMap(), m.Metadata.AsMap())
213-
assert.NoError(t, err)
236+
if assert.NoError(t, err) {
237+
assert.Equal(t, testCase.metadata().AsMap(), m.Metadata.AsMap())
238+
}
214239
})
215240
}
216241
}
242+
243+
func BenchmarkJSONMetadata_UnmarshalJSON(b *testing.B) {
244+
payload := []byte(`{"_aggregate_id": "b9ebca7a-c1eb-40dd-94a4-fac7c5e84fb5", "_aggregate_type": "bank_account", "_aggregate_version": 1}`)
245+
246+
b.ResetTimer()
247+
for i := 0; i < b.N; i++ {
248+
var m metadata.JSONMetadata
249+
err := json.Unmarshal(payload, &m)
250+
if err != nil {
251+
b.Fail()
252+
}
253+
}
254+
}

0 commit comments

Comments
 (0)