Skip to content

Instantly share code, notes, and snippets.

@a-robinson
Created November 28, 2017 15:53
Show Gist options
  • Save a-robinson/8f35afbcefa6fc23367b2420220c9965 to your computer and use it in GitHub Desktop.
Save a-robinson/8f35afbcefa6fc23367b2420220c9965 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
"fmt"
"os"
"os/exec"
)
func main() {
if err := os.RemoveAll("synctest.txt"); err != nil {
panic(err)
}
f, err := os.OpenFile("synctest.txt", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer f.Close()
buf := make([]byte, 1024*1024)
for i := 0; i < 2; i++ {
for j := 0; j < 256; j++ {
n1, err := rand.Read(buf)
if err != nil {
panic(err)
}
n2, err := f.Write(buf)
if err != nil {
panic(err)
}
if n1 != n2 {
panic(fmt.Sprintf("wrote %d bytes; expected %d", n2, n1))
}
}
fmt.Printf("after batch #%d of writes: %q\n", i+1, dirtyBytes())
}
fmt.Printf("fsyncing: %q\n", dirtyBytes())
if err := f.Sync(); err != nil {
panic(err)
}
fmt.Printf("done fsyncing: %q\n", dirtyBytes())
}
func dirtyBytes() string {
output, err := exec.Command("grep", "Dirty", "/proc/meminfo").CombinedOutput()
if err != nil {
panic(err)
}
return string(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment