Skip to content

Commit

Permalink
adding benchmark for Pipe, cmp with std lib for frequent small writes
Browse files Browse the repository at this point in the history
  • Loading branch information
djherbis committed Jan 18, 2017
1 parent 25e500e commit fe07db0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions nio_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package nio

import (
"bufio"
"bytes"
"io"
"testing"

"io/ioutil"

"github.com/djherbis/buffer"
)

Expand Down Expand Up @@ -118,3 +121,32 @@ func TestEmpty(t *testing.T) {
t.Errorf("wrote wrong # of bytes %d", n)
}
}

func BenchmarkPipe(b *testing.B) {
r, w := Pipe(buffer.New(1024))
benchPipe(r, w, b)
}

func BenchmarkIOPipe(b *testing.B) {
r, w := io.Pipe()
benchPipe(r, w, b)
}

func benchPipe(r io.Reader, w io.WriteCloser, b *testing.B) {
b.ReportAllocs()
f, err := ioutil.TempFile("", "benchPipe")
if err != nil {
b.Error(err)
}

go func() {
defer f.Close()
io.Copy(bufio.NewWriter(f), r)
}()

defer w.Close()

for i := 0; i < b.N; i++ {
w.Write([]byte("hello world"))
}
}

0 comments on commit fe07db0

Please sign in to comment.