Created
November 27, 2012 05:06
-
-
Save jimjh/4152494 to your computer and use it in GitHub Desktop.
Benchmarks with 10% syncs versus no syncs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To execute, run it with:
The output of a typical run might be: