Skip to content

Instantly share code, notes, and snippets.

@namratachaudhary
Last active September 6, 2018 04:04
Show Gist options
  • Save namratachaudhary/7087f72492b8ec764941a5b4814d614d to your computer and use it in GitHub Desktop.
Save namratachaudhary/7087f72492b8ec764941a5b4814d614d to your computer and use it in GitHub Desktop.
Fsync Benchmarking for Go
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"time"
)
var iterations = flag.Int("n", 100, "iterations to test")
func main() {
flag.Parse()
out, err := ioutil.TempFile("", "fsync")
if err != nil {
log.Fatal(err)
}
defer out.Close()
defer os.Remove(out.Name())
buffer := []byte{'A'}
N := *iterations
duration := time.Duration(0)
for i := 0; i < N; i++ {
_, err = out.Write(buffer)
if err != nil {
log.Fatal(err)
}
start := time.Now()
err = out.Sync()
end := time.Now()
duration += end.Sub(start)
if err != nil {
log.Fatal(err)
}
}
log.Printf("Time taken %s for %d fsyncs", duration, N)
log.Printf("Time taken %s per fsync", duration/time.Duration(N))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment