Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created March 4, 2015 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save karlseguin/0ba24030fb12b10b686b to your computer and use it in GitHub Desktop.
Save karlseguin/0ba24030fb12b10b686b to your computer and use it in GitHub Desktop.
bolt vs redis (appendonly on) vs postgresql
/*
bolt 5000 277963 ns/op
redis 30000 48081 ns/op
pg 10000 149691 ns/op
Yes, the Bolt transactions could be batched. But so too could the PG transactions,
and the Redis work could be pipelined. And that isn't always a workable solution.
*/
import (
"encoding/binary"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/jackc/pgx"
"github.com/karlseguin/bolt"
"os"
"strconv"
"testing"
)
const (
ITERATION = 10000
)
func main() {
fmt.Println("bolt ", testing.Benchmark(testBolt))
fmt.Println("redis", testing.Benchmark(testRedis))
fmt.Println("pg ", testing.Benchmark(testPG))
}
func testPG(b *testing.B) {
conn, err := pgx.Connect(pgx.ConnConfig{Host: "localhost", Port: 5432, Database: "xx"})
if err != nil {
panic(err)
}
conn.Exec("truncate table ids")
conn.Prepare("ids", "insert into ids values($1, $2)")
b.ResetTimer()
for i := 0; i < b.N; i++ {
k, v := kv(i)
conn.Exec("ids", k, v)
}
}
func testRedis(b *testing.B) {
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
panic(err)
}
conn.Do("flushdb")
b.ResetTimer()
for i := 0; i < b.N; i++ {
k, v := kv(i)
conn.Do("set", k, v)
}
}
func testBolt(b *testing.B) {
os.Remove("bolt.db")
db, err := bolt.Open("bolt.db", 0600, nil)
if err != nil {
panic(err)
}
bucket := []byte("MAIN")
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucketIfNotExists(bucket)
return nil
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
k, v := kv(i)
db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(bucket).Put(k, v)
})
}
}
func kv(i int) ([]byte, []byte) {
k := []byte(strconv.Itoa(i))
v := make([]byte, 8)
binary.LittleEndian.PutUint64(v, uint64(i))
return k, v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment