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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package thirdparty
import (
"encoding/binary"
"runtime"
"strconv"
"testing"
_ "unsafe"
_ "github.com/gorilla/websocket"
_ "github.com/coder/websocket"
)
func basicMask(b []byte, maskKey [4]byte, pos int) int {
for i := range b {
b[i] ^= maskKey[pos&3]
pos++
}
return pos & 3
}
//go:linkname maskGo github.com/coder/websocket.maskGo
func maskGo(b []byte, key32 uint32) int
//go:linkname maskAsm github.com/coder/websocket.maskAsm
func maskAsm(b *byte, len int, key32 uint32) uint32
//go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
func gorillaMaskBytes(key [4]byte, pos int, b []byte) int
func Benchmark_mask(b *testing.B) {
b.Run(runtime.GOARCH, benchmark_mask)
}
func benchmark_mask(b *testing.B) {
sizes := []int{
8,
16,
32,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
}
fns := []struct {
name string
fn func(b *testing.B, key [4]byte, p []byte)
}{
{
name: "basic",
fn: func(b *testing.B, key [4]byte, p []byte) {
for i := 0; i < b.N; i++ {
basicMask(p, key, 0)
}
},
},
{
name: "nhooyr-go",
fn: func(b *testing.B, key [4]byte, p []byte) {
key32 := binary.LittleEndian.Uint32(key[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
maskGo(p, key32)
}
},
},
{
name: "wdvxdr1123-asm",
fn: func(b *testing.B, key [4]byte, p []byte) {
key32 := binary.LittleEndian.Uint32(key[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
maskAsm(&p[0], len(p), key32)
}
},
},
{
name: "gorilla",
fn: func(b *testing.B, key [4]byte, p []byte) {
for i := 0; i < b.N; i++ {
gorillaMaskBytes(key, 0, p)
}
},
},
}
key := [4]byte{1, 2, 3, 4}
for _, fn := range fns {
b.Run(fn.name, func(b *testing.B) {
for _, size := range sizes {
p := make([]byte, size)
b.Run(strconv.Itoa(size), func(b *testing.B) {
b.SetBytes(int64(size))
fn.fn(b, key, p)
})
}
})
}
}
|