Debian Codegraph @

internal/thirdparty/frame_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package thirdparty
2
3 import (
4 "encoding/binary"
5 "runtime"
6 "strconv"
7 "testing"
8 _ "unsafe"
9
10 _ "github.com/gorilla/websocket"
11
12 _ "github.com/coder/websocket"
13 )
14
15 func basicMask(b []byte, maskKey [4]byte, pos int) int {
16 for i := range b {
17 b[i] ^= maskKey[pos&3]
18 pos++
19 }
20 return pos & 3
21 }
22
23 //go:linkname maskGo github.com/coder/websocket.maskGo
24 func maskGo(b []byte, key32 uint32) int
25
26 //go:linkname maskAsm github.com/coder/websocket.maskAsm
27 func maskAsm(b *byte, len int, key32 uint32) uint32
28
29 //go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
30 func gorillaMaskBytes(key [4]byte, pos int, b []byte) int
31
32 func Benchmark_mask(b *testing.B) {
33 b.Run(runtime.GOARCH, benchmark_mask)
34 }
35
36 func benchmark_mask(b *testing.B) {
37 sizes := []int{
38 8,
39 16,
40 32,
41 128,
42 256,
43 512,
44 1024,
45 2048,
46 4096,
47 8192,
48 16384,
49 }
50
51 fns := []struct {
52 name string
53 fn func(b *testing.B, key [4]byte, p []byte)
54 }{
55 {
56 name: "basic",
57 fn: func(b *testing.B, key [4]byte, p []byte) {
58 for i := 0; i < b.N; i++ {
59 basicMask(p, key, 0)
60 }
61 },
62 },
63
64 {
65 name: "nhooyr-go",
66 fn: func(b *testing.B, key [4]byte, p []byte) {
67 key32 := binary.LittleEndian.Uint32(key[:])
68 b.ResetTimer()
69
70 for i := 0; i < b.N; i++ {
71 maskGo(p, key32)
72 }
73 },
74 },
75 {
76 name: "wdvxdr1123-asm",
77 fn: func(b *testing.B, key [4]byte, p []byte) {
78 key32 := binary.LittleEndian.Uint32(key[:])
79 b.ResetTimer()
80
81 for i := 0; i < b.N; i++ {
82 maskAsm(&p[0], len(p), key32)
83 }
84 },
85 },
86
87 {
88 name: "gorilla",
89 fn: func(b *testing.B, key [4]byte, p []byte) {
90 for i := 0; i < b.N; i++ {
91 gorillaMaskBytes(key, 0, p)
92 }
93 },
94 },
95 }
96
97 key := [4]byte{1, 2, 3, 4}
98
99 for _, fn := range fns {
100 b.Run(fn.name, func(b *testing.B) {
101 for _, size := range sizes {
102 p := make([]byte, size)
103
104 b.Run(strconv.Itoa(size), func(b *testing.B) {
105 b.SetBytes(int64(size))
106
107 fn.fn(b, key, p)
108 })
109 }
110 })
111 }
112 }