Skip to content

Instantly share code, notes, and snippets.

@macrat
Created Dec 1, 2019
Embed
What would you like to do?
golangでredisとetcdの速度を比べてみた
package test
import (
"testing"
"context"
"github.com/go-redis/redis"
etcd "go.etcd.io/etcd/client"
)
func BenchmarkRedisSet(b *testing.B) {
client := redis.NewClient(&redis.Options{})
if err := client.Ping().Err(); err != nil {
b.Fatalf("failed to connect: %s", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
client.Set("key", "value", 0).Err()
}
}
func BenchmarkRedisGet(b *testing.B) {
client := redis.NewClient(&redis.Options{})
if err := client.Ping().Err(); err != nil {
b.Fatalf("failed to connect: %s", err)
}
if err := client.Set("key", "value", 0).Err(); err != nil {
b.Fatalf("failed to set data: %s", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
client.Get("key").Result()
}
}
func BenchmarkEtcdSet(b *testing.B) {
client, err := etcd.New(etcd.Config{
Endpoints: []string{"http://127.0.0.1:4001"},
})
if err != nil {
b.Errorf("failed to connect: %s", err)
}
kapi := etcd.NewKeysAPI(client)
b.ResetTimer()
for i := 0; i < b.N; i++ {
kapi.Set(context.Background(), "/key", "value", nil)
}
}
func BenchmarkEtcdGet(b *testing.B) {
client, err := etcd.New(etcd.Config{
Endpoints: []string{"http://127.0.0.1:4001"},
})
if err != nil {
b.Errorf("failed to connect: %s", err)
}
kapi := etcd.NewKeysAPI(client)
kapi.Set(context.Background(), "/key", "value", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
kapi.Get(context.Background(), "/key", nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment