forked from creachadair/jrpc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
58 lines (51 loc) · 1.25 KB
/
json_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
// Copyright (C) 2017 Michael J. Fromberger. All Rights Reserved.
package jrpc2
import (
"encoding/json"
"strconv"
"testing"
)
const payloadText = `{
"alpha": [1, 2, 3, 4, 5],
"bravo": true,
"charlie": {
"foo": ["xyz", "pdq"],
"bar": false,
"baz": 1.00003e+19
},
"delta": null,
"echo": 3.352391934e-19,
"foxtrot": "all your \"base\" are belong to us"
}`
const errMessage = "you did not do the correct thing and you should feel bad"
const methodName = "some long method name whatever"
func BenchmarkEncodeMessage(b *testing.B) {
msgs := jmessages{
{ID: []byte("12345"), M: methodName, R: []byte(payloadText)},
{ID: nil, M: methodName, R: []byte(payloadText)},
{ID: []byte("12345"), R: []byte(payloadText)},
{ID: []byte("12345"), E: &Error{Code: 12345, Message: errMessage}},
}
for i, msg := range msgs {
b.Run(strconv.Itoa(i+1)+"-std", func(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(msg)
}
})
b.Run(strconv.Itoa(i+1)+"-custom", func(b *testing.B) {
for i := 0; i < b.N; i++ {
msg.toJSON()
}
})
}
b.Run("batch-std", func(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(msgs)
}
})
b.Run("batch-custom", func(b *testing.B) {
for i := 0; i < b.N; i++ {
msgs.toJSON()
}
})
}