tl;dr

  • データベースに接続済みの状態からstringで値をセット・ゲットするベンチマーク
    • BoltのGetがめちゃめちゃ速い
    • go-redisよりはredigoの方が速い
    • Boltのセットがメモリアロケーションすごく多い

result

1
2
3
4
5
6
7
8
9
$ go test -bench .
BenchmarkRedisSet-4    	   10000	    246527 ns/op	     249 B/op	       9 allocs/op
BenchmarkRedisGet-4    	    5000	    231569 ns/op	     225 B/op	       9 allocs/op
BenchmarkRedigoSet-4   	    5000	    204545 ns/op	      70 B/op	       4 allocs/op
BenchmarkRedigoGet-4   	    5000	    209392 ns/op	      80 B/op	       6 allocs/op
BenchmarkBoltSet-4     	   10000	    166142 ns/op	   34287 B/op	      57 allocs/op
BenchmarkBoltGet-4     	 1000000	      1140 ns/op	     488 B/op	       8 allocs/op
PASS
ok  	practices/redis-bolt-benchmark	8.705s

source

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package rbbench_test

import (
	"testing"

	"github.com/boltdb/bolt"
	redigo "github.com/garyburd/redigo/redis"
	redis "github.com/go-redis/redis"
)

var redisOpts = &redis.Options{
	Addr:     "localhost:6379",
	Password: "",
	DB:       0,
}

func BenchmarkRedisSet(b *testing.B) {
	client := redis.NewClient(redisOpts)
	defer client.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		client.Set("key"+string(i), "value", 0).Err()
	}
}

func BenchmarkRedisGet(b *testing.B) {
	client := redis.NewClient(redisOpts)
	defer client.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		client.Get("key" + string(i)).Val()
	}
}

func BenchmarkRedigoSet(b *testing.B) {
	conn, _ := redigo.Dial("tcp", "localhost:6379")
	defer conn.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		conn.Do("SET", "key"+string(i), "value")
	}
}

func BenchmarkRedigoGet(b *testing.B) {
	conn, _ := redigo.Dial("tcp", "localhost:6379")
	defer conn.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		redigo.String(conn.Do("GET", "key"+string(i)))
	}
}

func BenchmarkBoltSet(b *testing.B) {
	db, _ := bolt.Open("bolt.db", 0600, nil)
	defer db.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		db.Update(func(tx *bolt.Tx) error {
			b, _ := tx.CreateBucketIfNotExists([]byte("bucket"))
			b.Put([]byte("key"+string(i)), []byte("value"))
			return nil
		})
	}
}

func BenchmarkBoltGet(b *testing.B) {
	db, _ := bolt.Open("bolt.db", 0600, nil)
	defer db.Close()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		db.View(func(tx *bolt.Tx) error {
			_ = string(tx.Bucket([]byte("bucket")).Get([]byte("key" + string(i))))
			return nil
		})
	}
}