Skip to content

Instantly share code, notes, and snippets.

@rfjakob
Created June 9, 2017 20:16
Show Gist options
  • Select an option

  • Save rfjakob/91bfe3755910d7e97a424a33d1964a09 to your computer and use it in GitHub Desktop.

Select an option

Save rfjakob/91bfe3755910d7e97a424a33d1964a09 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"sync"
"testing"
)
var b bytes.Buffer
var l sync.Mutex
func prefetch(n int, pre int) []byte {
b2 := make([]byte, n)
l.Lock()
have, err := b.Read(b2)
if have == n && err == nil {
l.Unlock()
return b2
}
b.Reset()
b.Write(RandBytes(pre))
_, err = b.Read(b2)
if err != nil {
panic("!!!")
}
l.Unlock()
return b2
}
func Benchmark16(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
RandBytes(16)
}
}
func Benchmark64(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 64)
}
}
func Benchmark128(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 128)
}
}
func Benchmark256(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 256)
}
}
func Benchmark512(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 512)
}
}
func Benchmark1024(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 1024)
}
}
func Benchmark2048(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 2048)
}
}
func Benchmark4096(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 4096)
}
}
func Benchmark40960(b *testing.B) {
b.SetBytes(16)
for i := 0; i < b.N; i++ {
prefetch(16, 40960)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment