Created
July 5, 2014 11:25
-
-
Save marete/98c3a3d5d7eabc873e7b to your computer and use it in GitHub Desktop.
fsync benchmark
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 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