Debian Codegraph @

internal/thirdparty/frame_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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/gobwas/ws"
11 _ "github.com/gorilla/websocket"
12 _ "github.com/lesismal/nbio/nbhttp/websocket"
13
14 _ "github.com/coder/websocket"
15 )
16
17 func basicMask(b []byte, maskKey [4]byte, pos int) int {
18 for i := range b {
19 b[i] ^= maskKey[pos&3]
20 pos++
21 }
22 return pos & 3
23 }
24
25 //go:linkname maskGo github.com/coder/websocket.maskGo
26 func maskGo(b []byte, key32 uint32) int
27
28 //go:linkname maskAsm github.com/coder/websocket.maskAsm
29 func maskAsm(b *byte, len int, key32 uint32) uint32
30
31 //go:linkname nbioMaskBytes github.com/lesismal/nbio/nbhttp/websocket.maskXOR
32 func nbioMaskBytes(b, key []byte) int
33
34 //go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
35 func gorillaMaskBytes(key [4]byte, pos int, b []byte) int
36
37 func Benchmark_mask(b *testing.B) {
38 b.Run(runtime.GOARCH, benchmark_mask)
39 }
40
41 func benchmark_mask(b *testing.B) {
42 sizes := []int{
43 8,
44 16,
45 32,
46 128,
47 256,
48 512,
49 1024,
50 2048,
51 4096,
52 8192,
53 16384,
54 }
55
56 fns := []struct {
57 name string
58 fn func(b *testing.B, key [4]byte, p []byte)
59 }{
60 {
61 name: "basic",
62 fn: func(b *testing.B, key [4]byte, p []byte) {
63 for i := 0; i < b.N; i++ {
64 basicMask(p, key, 0)
65 }
66 },
67 },
68
69 {
70 name: "nhooyr-go",
71 fn: func(b *testing.B, key [4]byte, p []byte) {
72 key32 := binary.LittleEndian.Uint32(key[:])
73 b.ResetTimer()
74
75 for i := 0; i < b.N; i++ {
76 maskGo(p, key32)
77 }
78 },
79 },
80 {
81 name: "wdvxdr1123-asm",
82 fn: func(b *testing.B, key [4]byte, p []byte) {
83 key32 := binary.LittleEndian.Uint32(key[:])
84 b.ResetTimer()
85
86 for i := 0; i < b.N; i++ {
87 maskAsm(&p[0], len(p), key32)
88 }
89 },
90 },
91
92 {
93 name: "gorilla",
94 fn: func(b *testing.B, key [4]byte, p []byte) {
95 for i := 0; i < b.N; i++ {
96 gorillaMaskBytes(key, 0, p)
97 }
98 },
99 },
100 {
101 name: "gobwas",
102 fn: func(b *testing.B, key [4]byte, p []byte) {
103 for i := 0; i < b.N; i++ {
104 ws.Cipher(p, key, 0)
105 }
106 },
107 },
108 {
109 name: "nbio",
110 fn: func(b *testing.B, key [4]byte, p []byte) {
111 keyb := key[:]
112 for i := 0; i < b.N; i++ {
113 nbioMaskBytes(p, keyb)
114 }
115 },
116 },
117 }
118
119 key := [4]byte{1, 2, 3, 4}
120
121 for _, fn := range fns {
122 b.Run(fn.name, func(b *testing.B) {
123 for _, size := range sizes {
124 p := make([]byte, size)
125
126 b.Run(strconv.Itoa(size), func(b *testing.B) {
127 b.SetBytes(int64(size))
128
129 fn.fn(b, key, p)
130 })
131 }
132 })
133 }
134 }