Created
December 1, 2019 13:59
-
-
Save macrat/d362863730e20b76c59a98ea2028bbd6 to your computer and use it in GitHub Desktop.
golangでredisとetcdの速度を比べてみた
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
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