Last active
September 6, 2018 04:04
-
-
Save namratachaudhary/7087f72492b8ec764941a5b4814d614d to your computer and use it in GitHub Desktop.
Fsync Benchmarking for Go
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/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