Skip to content

Instantly share code, notes, and snippets.

@jimjh
Created November 27, 2012 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimjh/4152494 to your computer and use it in GitHub Desktop.
Save jimjh/4152494 to your computer and use it in GitHub Desktop.
Benchmarks with 10% syncs versus no syncs.
package flushing
import (
"encoding/binary"
"fmt"
"math/rand"
"os"
"testing"
)
var MAX uint32 = 1000000
// Write and Reading 0..MAX integers without sync.
func BenchmarkReadWrite(b *testing.B) {
file, _ := os.OpenFile("log", os.O_RDWR, 0666)
var i uint32
for i = 0; i <= MAX; i++ {
binary.Write(file, binary.LittleEndian, i)
}
file.Seek(0, os.SEEK_SET)
for i = 0; i <= MAX; i = i {
var n uint32
binary.Read(file, binary.LittleEndian, &n)
if n != i {
panic(fmt.Sprintf("corrupted file; expected %d, was %d", i, n))
}
i++
}
}
// Write and Reading 0..MAX integers with 10% sync.
func BenchmarkReadWriteFlush(b *testing.B) {
file, _ := os.OpenFile("log", os.O_RDWR, 0666)
var i uint32
for i = 0; i <= MAX; i++ {
binary.Write(file, binary.LittleEndian, i)
if rand.Float64() < 0.01 {
file.Sync()
}
}
file.Seek(0, os.SEEK_SET)
for i = 0; i <= MAX; i++ {
var n uint32
binary.Read(file, binary.LittleEndian, &n)
if n != i {
panic("corrupted file")
}
}
file.Close()
}
@jimjh
Copy link
Author

jimjh commented Nov 27, 2012

To execute, run it with:

$> go test -c flushing_test.go
$> ./flushing.test -test.bench Bench

The output of a typical run might be:

PASS
BenchmarkReadWrite             1    2062437000 ns/op
BenchmarkReadWriteFlush        1    11519889000 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment