Skip to content

Commit 39b7a4d

Browse files
Add MarshalJSON benchmark
Before we can optimize the metadata JSON marshalling it's time to get a base line. ``` go test -tags=unit -benchmem -bench . BenchmarkMarshalJSON-4 300000 4309 ns/op 1160 B/op 18 allocs/op ```
1 parent 13e8668 commit 39b7a4d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

metadata/metadata_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ var jsonTestCases = []struct {
198198
},
199199
}
200200

201-
func TestMetadata_MarshalJSON(t *testing.T) {
201+
func TestMarshalJSON(t *testing.T) {
202202
for _, testCase := range jsonTestCases {
203203
t.Run(testCase.title, func(t *testing.T) {
204204
m := testCase.metadata()
@@ -211,7 +211,7 @@ func TestMetadata_MarshalJSON(t *testing.T) {
211211
}
212212
}
213213

214-
func TestJSONMetadata_UnmarshalJSON(t *testing.T) {
214+
func TestUnmarshalJSON(t *testing.T) {
215215
for _, testCase := range jsonTestCases {
216216
t.Run(testCase.title, func(t *testing.T) {
217217
m, err := metadata.UnmarshalJSON([]byte(testCase.json))
@@ -224,7 +224,7 @@ func TestJSONMetadata_UnmarshalJSON(t *testing.T) {
224224
}
225225
}
226226

227-
func BenchmarkJSONMetadata_UnmarshalJSON(b *testing.B) {
227+
func BenchmarkUnmarshalJSON(b *testing.B) {
228228
payload := []byte(`{"_aggregate_id": "b9ebca7a-c1eb-40dd-94a4-fac7c5e84fb5", "_aggregate_type": "bank_account", "_aggregate_version": 1}`)
229229

230230
b.ResetTimer()
@@ -235,3 +235,18 @@ func BenchmarkJSONMetadata_UnmarshalJSON(b *testing.B) {
235235
}
236236
}
237237
}
238+
239+
func BenchmarkMarshalJSON(b *testing.B) {
240+
m := metadata.New()
241+
m = metadata.WithValue(m, "_aggregate_id", "b9ebca7a-c1eb-40dd-94a4-fac7c5e84fb5")
242+
m = metadata.WithValue(m, "_aggregate_type", "bank_account")
243+
m = metadata.WithValue(m, "_aggregate_version", 1)
244+
245+
b.ResetTimer()
246+
for i := 0; i < b.N; i++ {
247+
_, err := json.Marshal(m)
248+
if err != nil {
249+
b.Fail()
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)