Forked from karlseguin/gist:0ba24030fb12b10b686b
Last active
August 29, 2015 14:16
-
-
Save SchumacherFM/9c4b112fcb03b21a89be to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
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