File tree Expand file tree Collapse file tree 1 file changed +37
-8
lines changed Expand file tree Collapse file tree 1 file changed +37
-8
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,51 @@ package wsjson_test
3
3
import (
4
4
"encoding/json"
5
5
"io"
6
- "strings "
6
+ "strconv "
7
7
"testing"
8
+
9
+ "nhooyr.io/websocket/internal/test/xrand"
8
10
)
9
11
10
12
func BenchmarkJSON (b * testing.B ) {
11
- msg := []byte (strings .Repeat ("1234" , 128 ))
12
- b .SetBytes (int64 (len (msg )))
13
- b .ReportAllocs ()
13
+ sizes := []int {
14
+ 8 ,
15
+ 16 ,
16
+ 32 ,
17
+ 128 ,
18
+ 256 ,
19
+ 512 ,
20
+ 1024 ,
21
+ 2048 ,
22
+ 4096 ,
23
+ 8192 ,
24
+ 16384 ,
25
+ }
26
+
14
27
b .Run ("json.Encoder" , func (b * testing.B ) {
15
- for i := 0 ; i < b .N ; i ++ {
16
- json .NewEncoder (io .Discard ).Encode (msg )
28
+ for _ , size := range sizes {
29
+ b .Run (strconv .Itoa (size ), func (b * testing.B ) {
30
+ msg := xrand .String (size )
31
+ b .SetBytes (int64 (size ))
32
+ b .ReportAllocs ()
33
+ b .ResetTimer ()
34
+ for i := 0 ; i < b .N ; i ++ {
35
+ json .NewEncoder (io .Discard ).Encode (msg )
36
+ }
37
+ })
17
38
}
18
39
})
19
40
b .Run ("json.Marshal" , func (b * testing.B ) {
20
- for i := 0 ; i < b .N ; i ++ {
21
- json .Marshal (msg )
41
+ for _ , size := range sizes {
42
+ b .Run (strconv .Itoa (size ), func (b * testing.B ) {
43
+ msg := xrand .String (size )
44
+ b .SetBytes (int64 (size ))
45
+ b .ReportAllocs ()
46
+ b .ResetTimer ()
47
+ for i := 0 ; i < b .N ; i ++ {
48
+ json .Marshal (msg )
49
+ }
50
+ })
22
51
}
23
52
})
24
53
}
You can’t perform that action at this time.
0 commit comments