Skip to content

Instantly share code, notes, and snippets.

@marete
Created July 5, 2014 11:25
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 marete/98c3a3d5d7eabc873e7b to your computer and use it in GitHub Desktop.
Save marete/98c3a3d5d7eabc873e7b to your computer and use it in GitHub Desktop.
fsync benchmark
package main
import (
"flag"
"io"
"os"
)
const blockSize = 70 << 10
func write(dstFD *os.File, sync bool, limitBytes uint64) {
srcFD, err := os.Open("/dev/zero")
if err != nil {
panic(err)
}
defer srcFD.Close()
var count uint64
for {
_, err := io.CopyN(dstFD, srcFD, blockSize)
if err != nil {
panic(err)
}
if sync {
err := dstFD.Sync()
if err != nil {
panic(err)
}
}
count += blockSize
if count > limitBytes {
break
}
}
dstFD.Sync()
}
var output string
var limit uint64
var sync bool
func init() {
flag.StringVar(&output, "output", "", "output file")
flag.Uint64Var(&limit, "limit-bytes", 0, "write this number of bytes")
flag.BoolVar(&sync, "sync", true, "sync to disk after each block")
}
func main() {
flag.Parse()
fd, err := os.Create(output)
if err != nil {
panic(err)
}
defer fd.Close()
write(fd, sync, limit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment