benchmarks for write with compression enabled/disabled

This commit is contained in:
Alexandr Emelin 2017-01-13 12:06:51 +03:00
parent 17634340a8
commit 34e053563d
1 changed files with 35 additions and 0 deletions

View File

@ -2,7 +2,9 @@ package websocket
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/ioutil"
"testing" "testing"
) )
@ -29,3 +31,36 @@ func TestTruncWriter(t *testing.T) {
} }
} }
} }
func textMessages(num int) [][]byte {
messages := make([][]byte, num)
for i := 0; i < num; i++ {
msg := fmt.Sprintf("planet: %d, country: %d, city: %d, street: %d", i, i, i, i)
messages[i] = []byte(msg)
}
return messages
}
func BenchmarkWriteNoCompression(b *testing.B) {
w := ioutil.Discard
c := newConn(fakeNetConn{Reader: nil, Writer: w}, false, 1024, 1024)
messages := textMessages(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(TextMessage, messages[i%len(messages)])
}
b.ReportAllocs()
}
func BenchmarkWriteWithCompression(b *testing.B) {
w := ioutil.Discard
c := newConn(fakeNetConn{Reader: nil, Writer: w}, false, 1024, 1024)
messages := textMessages(100)
c.enableWriteCompression = true
c.newCompressionWriter = compressNoContextTakeover
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(TextMessage, messages[i%len(messages)])
}
b.ReportAllocs()
}