Skip to content

Instantly share code, notes, and snippets.

@dancannon
Created February 6, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dancannon/86f6c1ed2712edf0ebee to your computer and use it in GitHub Desktop.
Save dancannon/86f6c1ed2712edf0ebee to your computer and use it in GitHub Desktop.
package main
import (
"log"
"sync"
"testing"
gr "github.com/dancannon/gorethink"
)
func BenchmarkSequentialWrites(b *testing.B) {
var err error
c := make(chan struct{}, 100)
session, err := gr.Connect(gr.ConnectOpts{
Address: "localhost:28015",
Database: "test",
MaxIdle: 1000,
MaxOpen: 1000,
})
if err != nil {
log.Fatalln(err.Error())
}
si := 0
for i := 0; i < b.N; i++ {
c <- struct{}{}
si++
data := map[string]interface{}{
"customer_id": si,
}
go func() {
// Insert the new item into the database
_, err = gr.Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
}
<-c
}()
}
}
func BenchmarkSequentialWritesParallel(b *testing.B) {
var err error
var mu sync.Mutex
si := 0
c := make(chan struct{}, 100)
session, err := gr.Connect(gr.ConnectOpts{
Address: "localhost:28015",
Database: "test",
MaxIdle: 100,
MaxOpen: 100,
})
if err != nil {
log.Fatalln(err.Error())
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
mu.Lock()
si++
mu.Unlock()
c <- struct{}{}
data := map[string]interface{}{
"customer_id": si,
}
go func() {
// Insert the new item into the database
_, err = gr.Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
}
<-c
}()
}
})
}
PASS
BenchmarkRandomWrites-4 20000 474594 ns/op
BenchmarkRandomWritesParallel-4 10000 555673 ns/op
BenchmarkSequentialWrites-4 20000 480506 ns/op
BenchmarkSequentialWritesParallel-4 20000 484716 ns/op
ok github.com/jaredfolkins/rethinkdb_benchmarks 48.902s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment