Skip to content

Instantly share code, notes, and snippets.

@poonai
Last active September 2, 2019 15:40
Show Gist options
  • Save poonai/72aa18f178020b4438a532fef9c909c5 to your computer and use it in GitHub Desktop.
Save poonai/72aa18f178020b4438a532fef9c909c5 to your computer and use it in GitHub Desktop.
sync bench
package main
import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os"
"sync"
"testing"
)
// goos: linux
// goarch: amd64
// BenchmarkSyncConcurrent/FILE_WITH_SYNC-16 1 1376064452 ns/op 9424 B/op 27 allocs/op
// BenchmarkSyncConcurrent/ONE_BATCH_SYNC-16 10 142692956 ns/op 720 B/op 12 allocs/op
// BenchmarkSyncConcurrent/TWO_BATCH_SERIAL_SYNC-16 5 214881655 ns/op 1424 B/op 23 allocs/op
// BenchmarkSyncConcurrent/TWO_BATCH_CONCURRENT_SYNC-16 10 146918995 ns/op 4964 B/op 31 allocs/op
// PASS
// ok _/home/schoolboy/sample 5.845s
// Success: Benchmarks passed.
func BenchmarkSyncConcurrent(b *testing.B) {
tempName := func() string {
name := make([]byte, 10)
rand.Read(name)
dir, _ := os.Getwd()
return dir + "/" + string(hex.EncodeToString(name)) + ".txt"
}
_ = func() (*os.File, *os.File) {
fp, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
panic(err)
}
fp2, err := ioutil.TempFile(os.TempDir(), "")
if err != nil {
panic(err)
}
data := make([]byte, 8)
rand.Read(data)
for i := 0; i < 1000; i++ {
fp.Write(data)
fp2.Write(data)
}
return fp, fp2
}
// b.Run("FILE WITH SYNC", func(b *testing.B) {
// do := func() {
// fp, err := os.OpenFile(tempName(), os.O_CREATE|os.O_SYNC|os.O_RDWR, 0666)
// if err != nil {
// panic(err)
// }
// data := make([]byte, 8)
// rand.Read(data)
// for i := 0; i < 1000; i++ {
// fp.Write(data)
// }
// fp.Close()
// }
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// do()
// }
// })
b.Run("ONE BATCH SYNC", func(b *testing.B) {
fp, err := os.OpenFile(tempName(), os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
data := make([]byte, 8)
rand.Read(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < 1000; i++ {
fp.Write(data)
fp.Write(data)
if i%10 == 0 {
fp.Sync()
}
}
}
})
// b.Run("TWO BATCH SERIAL SYNC", func(b *testing.B) {
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// fp, err := os.OpenFile(tempName(), os.O_CREATE|os.O_RDWR, 0666)
// if err != nil {
// panic(err)
// }
// fp2, err := os.OpenFile(tempName(), os.O_CREATE|os.O_RDWR, 0666)
// if err != nil {
// panic(err)
// }
// data := make([]byte, 8)
// rand.Read(data)
// for i := 0; i < 1000; i++ {
// fp.Write(data)
// fp2.Write(data)
// if i%10 == 0 {
// fp.Sync()
// fp2.Sync()
// }
// }
// fp.Close()
// fp2.Close()
// }
// })
b.Run("TWO BATCH CONCURRENT SYNC", func(b *testing.B) {
var wg sync.WaitGroup
syncer := func(f *os.File) {
f.Sync()
wg.Done()
}
fp, err := os.OpenFile(tempName(), os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
fp2, err := os.OpenFile(tempName(), os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
data := make([]byte, 8)
rand.Read(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < 1000; i++ {
fp.Write(data)
fp2.Write(data)
if i%10 == 0 {
wg.Add(2)
go syncer(fp)
go syncer(fp2)
wg.Wait()
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment