Last active
May 20, 2021 09:25
-
-
Save kumakichi/6eeed985d9f359c6e7e8ab9b1079945f to your computer and use it in GitHub Desktop.
test redis pipeline
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 redis_pipeline_test | |
import ( | |
"strconv" | |
"testing" | |
"time" | |
"github.com/go-redis/redis" | |
) | |
func TestRedisPipeline(t *testing.T) { | |
itemCnt := 10000 | |
s := "127.0.0.1:6379" | |
expiration := time.Second * 1000 | |
// normal write | |
timeRecorder := time.Now() | |
client := redis.NewClient(&redis.Options{Addr: s}) | |
for i := 0; i < itemCnt; i++ { | |
client.Set("key"+strconv.Itoa(i), "normal"+strconv.Itoa(i), expiration) | |
} | |
t.Logf("normal write costs: %s\n", time.Since(timeRecorder).String()) | |
// pipeline write | |
timeRecorder = time.Now() | |
m := map[string]*redis.StatusCmd{} | |
pipe := client.Pipeline() | |
for i := 0; i < itemCnt; i++ { | |
m["key"+strconv.Itoa(i)] = pipe.Set("key"+strconv.Itoa(i), "pipe"+strconv.Itoa(i), expiration) | |
} | |
_, err := pipe.Exec() | |
if err != nil { | |
t.Fatal(err) | |
} | |
t.Logf("pipeline write costs: %s\n", time.Since(timeRecorder).String()) | |
// normal read | |
timeRecorder = time.Now() | |
result := map[string]string{} | |
for i := 0; i < itemCnt; i++ { | |
key := "key" + strconv.Itoa(i) | |
res, _ := client.Get(key).Result() | |
result[key] = res | |
} | |
t.Logf("normal read costs: %s\n", time.Since(timeRecorder).String()) | |
// pipeline read | |
timeRecorder = time.Now() | |
m1 := map[string]*redis.StringCmd{} | |
pipe = client.Pipeline() | |
for i := 0; i < itemCnt; i++ { | |
m1["key"+strconv.Itoa(i)] = pipe.Get("key" + strconv.Itoa(i)) | |
} | |
_, err = pipe.Exec() | |
if err != nil { | |
t.Fatal(err) | |
} | |
result2 := map[string]string{} | |
for k, v := range m { | |
res, _ := v.Result() | |
result2[k] = res | |
} | |
t.Logf("pipeline read costs: %s\n", time.Since(timeRecorder).String()) | |
} | |
/* | |
=== RUN TestRedisPipeline | |
redis_pipeline_test.go:22: normal write costs: 461.731932ms | |
redis_pipeline_test.go:35: pipeline write costs: 30.876714ms | |
redis_pipeline_test.go:45: normal read costs: 475.519428ms | |
redis_pipeline_test.go:64: pipeline read costs: 18.313877ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment