File: wsjson_test.go

package info (click to toggle)
golang-nhooyr-websocket 1.8.12-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: asm: 158; sh: 101; javascript: 62; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 882 bytes parent folder | download | duplicates (2)
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
package wsjson_test

import (
	"encoding/json"
	"io"
	"strconv"
	"testing"

	"github.com/coder/websocket/internal/test/xrand"
)

func BenchmarkJSON(b *testing.B) {
	sizes := []int{
		8,
		16,
		32,
		128,
		256,
		512,
		1024,
		2048,
		4096,
		8192,
		16384,
	}

	b.Run("json.Encoder", func(b *testing.B) {
		for _, size := range sizes {
			b.Run(strconv.Itoa(size), func(b *testing.B) {
				msg := xrand.String(size)
				b.SetBytes(int64(size))
				b.ReportAllocs()
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					json.NewEncoder(io.Discard).Encode(msg)
				}
			})
		}
	})
	b.Run("json.Marshal", func(b *testing.B) {
		for _, size := range sizes {
			b.Run(strconv.Itoa(size), func(b *testing.B) {
				msg := xrand.String(size)
				b.SetBytes(int64(size))
				b.ReportAllocs()
				b.ResetTimer()
				for i := 0; i < b.N; i++ {
					json.Marshal(msg)
				}
			})
		}
	})
}