Skip to content

Commit 0596e7a

Browse files
committed
wsjson: Extend benchmark with multiple sizes
[qrvnl@dios ~/src/websocket] 130$ go test -bench=. ./wsjson/ goos: linux goarch: amd64 pkg: nhooyr.io/websocket/wsjson cpu: 12th Gen Intel(R) Core(TM) i5-1235U BenchmarkJSON/json.Encoder/8-12 14041426 72.59 ns/op 110.21 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/16-12 13936426 86.99 ns/op 183.92 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/32-12 11416401 115.3 ns/op 277.59 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/128-12 4600574 264.7 ns/op 483.55 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/256-12 2710398 433.9 ns/op 590.06 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/512-12 1588930 717.3 ns/op 713.82 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/1024-12 823138 1484 ns/op 689.80 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/2048-12 402823 2875 ns/op 712.32 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/4096-12 213926 5602 ns/op 731.14 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/8192-12 92864 11281 ns/op 726.19 MB/s 16 B/op 1 allocs/op BenchmarkJSON/json.Encoder/16384-12 39318 29203 ns/op 561.04 MB/s 19 B/op 1 allocs/op BenchmarkJSON/json.Marshal/8-12 10768671 114.5 ns/op 69.89 MB/s 48 B/op 2 allocs/op BenchmarkJSON/json.Marshal/16-12 10140996 113.9 ns/op 140.51 MB/s 64 B/op 2 allocs/op BenchmarkJSON/json.Marshal/32-12 9211780 121.6 ns/op 263.06 MB/s 64 B/op 2 allocs/op BenchmarkJSON/json.Marshal/128-12 4632796 264.2 ns/op 484.53 MB/s 224 B/op 2 allocs/op BenchmarkJSON/json.Marshal/256-12 2441511 473.5 ns/op 540.65 MB/s 432 B/op 2 allocs/op BenchmarkJSON/json.Marshal/512-12 1298788 896.2 ns/op 571.27 MB/s 912 B/op 2 allocs/op BenchmarkJSON/json.Marshal/1024-12 602084 1866 ns/op 548.83 MB/s 1808 B/op 2 allocs/op BenchmarkJSON/json.Marshal/2048-12 341151 3817 ns/op 536.61 MB/s 3474 B/op 2 allocs/op BenchmarkJSON/json.Marshal/4096-12 175594 7034 ns/op 582.32 MB/s 6548 B/op 2 allocs/op BenchmarkJSON/json.Marshal/8192-12 83222 15023 ns/op 545.30 MB/s 13591 B/op 2 allocs/op BenchmarkJSON/json.Marshal/16384-12 33087 39348 ns/op 416.39 MB/s 27304 B/op 2 allocs/op PASS ok nhooyr.io/websocket/wsjson 32.934s
1 parent 640e3c2 commit 0596e7a

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

wsjson/wsjson_test.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,51 @@ package wsjson_test
33
import (
44
"encoding/json"
55
"io"
6-
"strings"
6+
"strconv"
77
"testing"
8+
9+
"nhooyr.io/websocket/internal/test/xrand"
810
)
911

1012
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+
1427
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+
})
1738
}
1839
})
1940
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+
})
2251
}
2352
})
2453
}

0 commit comments

Comments
 (0)