Skip to content

Commit edda9c6

Browse files
committed
Add gobwas/ws and gorilla/websocket to mask benchmarks
On average, we are 1.75x faster than both now 🚀 goos: linux goarch: amd64 pkg: nhooyr.io/websocket Benchmark_mask/2/basic-8 263923018 4.55 ns/op 439.76 MB/s Benchmark_mask/2/nhooyr-8 200203578 6.00 ns/op 333.32 MB/s Benchmark_mask/2/gorilla-8 241622557 4.95 ns/op 404.38 MB/s Benchmark_mask/2/gobwas-8 200392592 6.05 ns/op 330.69 MB/s Benchmark_mask/3/basic-8 207684956 5.78 ns/op 519.07 MB/s Benchmark_mask/3/nhooyr-8 169297215 7.10 ns/op 422.49 MB/s Benchmark_mask/3/gorilla-8 205775799 5.81 ns/op 516.23 MB/s Benchmark_mask/3/gobwas-8 165921662 7.23 ns/op 415.06 MB/s Benchmark_mask/4/basic-8 167034886 7.18 ns/op 557.30 MB/s Benchmark_mask/4/nhooyr-8 287656454 4.30 ns/op 931.11 MB/s Benchmark_mask/4/gorilla-8 166140434 7.30 ns/op 547.57 MB/s Benchmark_mask/4/gobwas-8 138138087 8.73 ns/op 458.20 MB/s Benchmark_mask/8/basic-8 121179904 9.92 ns/op 806.67 MB/s Benchmark_mask/8/nhooyr-8 199632992 6.07 ns/op 1318.60 MB/s Benchmark_mask/8/gorilla-8 100000000 10.8 ns/op 739.65 MB/s Benchmark_mask/8/gobwas-8 157898031 7.54 ns/op 1061.27 MB/s Benchmark_mask/16/basic-8 73648268 16.5 ns/op 971.56 MB/s Benchmark_mask/16/nhooyr-8 186871615 6.38 ns/op 2506.61 MB/s Benchmark_mask/16/gorilla-8 72374540 16.6 ns/op 964.36 MB/s Benchmark_mask/16/gobwas-8 127698723 9.36 ns/op 1709.99 MB/s Benchmark_mask/32/basic-8 40010325 29.8 ns/op 1073.76 MB/s Benchmark_mask/32/nhooyr-8 168590156 7.12 ns/op 4494.72 MB/s Benchmark_mask/32/gorilla-8 67282072 17.7 ns/op 1808.59 MB/s Benchmark_mask/32/gobwas-8 120038877 9.96 ns/op 3213.55 MB/s Benchmark_mask/128/basic-8 10134963 118 ns/op 1082.74 MB/s Benchmark_mask/128/nhooyr-8 100000000 11.8 ns/op 10852.23 MB/s Benchmark_mask/128/gorilla-8 45452385 26.4 ns/op 4853.64 MB/s Benchmark_mask/128/gobwas-8 57188290 20.8 ns/op 6153.80 MB/s Benchmark_mask/512/basic-8 2707371 442 ns/op 1159.38 MB/s Benchmark_mask/512/nhooyr-8 37049421 32.4 ns/op 15785.82 MB/s Benchmark_mask/512/gorilla-8 19006171 62.8 ns/op 8150.01 MB/s Benchmark_mask/512/gobwas-8 21394864 55.8 ns/op 9169.49 MB/s Benchmark_mask/4096/basic-8 346566 3467 ns/op 1181.46 MB/s Benchmark_mask/4096/nhooyr-8 5170425 232 ns/op 17648.07 MB/s Benchmark_mask/4096/gorilla-8 2963664 405 ns/op 10105.59 MB/s Benchmark_mask/4096/gobwas-8 2989596 402 ns/op 10192.40 MB/s Benchmark_mask/16384/basic-8 86671 13833 ns/op 1184.38 MB/s Benchmark_mask/16384/nhooyr-8 1332649 889 ns/op 18436.40 MB/s Benchmark_mask/16384/gorilla-8 763900 1556 ns/op 10527.53 MB/s Benchmark_mask/16384/gobwas-8 764034 1553 ns/op 10548.60 MB/s PASS ok nhooyr.io/websocket 64.091s Results from a 8 GB 8 core Haswell VM on GCP.
1 parent c781bdf commit edda9c6

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

frame_test.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
"strings"
1414
"testing"
1515
"time"
16+
_ "unsafe"
1617

18+
"github.com/gobwas/ws"
1719
"github.com/google/go-cmp/cmp"
20+
_ "github.com/gorilla/websocket"
1821

1922
"nhooyr.io/websocket/internal/assert"
2023
)
@@ -325,14 +328,17 @@ func Test_mask(t *testing.T) {
325328
}
326329
}
327330

328-
func basixMask(maskKey [4]byte, pos int, b []byte) int {
331+
func basicMask(maskKey [4]byte, pos int, b []byte) int {
329332
for i := range b {
330333
b[i] ^= maskKey[pos&3]
331334
pos++
332335
}
333336
return pos & 3
334337
}
335338

339+
//go:linkname gorillaMaskBytes github.com/gorilla/websocket.maskBytes
340+
func gorillaMaskBytes(key [4]byte, pos int, b []byte) int
341+
336342
func Benchmark_mask(b *testing.B) {
337343
sizes := []int{
338344
2,
@@ -355,12 +361,13 @@ func Benchmark_mask(b *testing.B) {
355361
name: "basic",
356362
fn: func(b *testing.B, key [4]byte, p []byte) {
357363
for i := 0; i < b.N; i++ {
358-
basixMask(key, 0, p)
364+
basicMask(key, 0, p)
359365
}
360366
},
361367
},
368+
362369
{
363-
name: "fast",
370+
name: "nhooyr",
364371
fn: func(b *testing.B, key [4]byte, p []byte) {
365372
key32 := binary.LittleEndian.Uint32(key[:])
366373
b.ResetTimer()
@@ -370,6 +377,22 @@ func Benchmark_mask(b *testing.B) {
370377
}
371378
},
372379
},
380+
{
381+
name: "gorilla",
382+
fn: func(b *testing.B, key [4]byte, p []byte) {
383+
for i := 0; i < b.N; i++ {
384+
gorillaMaskBytes(key, 0, p)
385+
}
386+
},
387+
},
388+
{
389+
name: "gobwas",
390+
fn: func(b *testing.B, key [4]byte, p []byte) {
391+
for i := 0; i < b.N; i++ {
392+
ws.Cipher(p, key, 0)
393+
}
394+
},
395+
},
373396
}
374397

375398
var key [4]byte

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ go 1.13
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect
8+
github.com/gobwas/pool v0.2.0 // indirect
9+
github.com/gobwas/ws v1.0.2
710
github.com/golang/protobuf v1.3.2
811
github.com/google/go-cmp v0.3.1
12+
github.com/gorilla/websocket v1.4.1
913
github.com/kr/pretty v0.1.0 // indirect
1014
github.com/stretchr/testify v1.4.0 // indirect
1115
go.uber.org/atomic v1.4.0 // indirect

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
6+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
7+
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
8+
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
9+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
10+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
511
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
612
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
713
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
814
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
15+
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
16+
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
917
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1018
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1119
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

0 commit comments

Comments
 (0)