Skip to content

Commit

Permalink
Merge pull request #205 from FZambia/compression_benchmarks
Browse files Browse the repository at this point in the history
benchmarks for write with compression enabled/disabled
  • Loading branch information
garyburd committed Jan 14, 2017
2 parents 1763434 + 34e0535 commit 561ac01
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions compression_test.go
Expand Up @@ -2,7 +2,9 @@ package websocket

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
)

Expand All @@ -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()
}

0 comments on commit 561ac01

Please sign in to comment.