Skip to content

Instantly share code, notes, and snippets.

@chenyhd
Last active September 19, 2021 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenyhd/7014e92bb2d109edfadd1f399f4b63fc to your computer and use it in GitHub Desktop.
Save chenyhd/7014e92bb2d109edfadd1f399f4b63fc to your computer and use it in GitHub Desktop.
Condition: 100K/1M keys already exist in the DB, compare the search performance of "key" and "scan"
GOROOT=/usr/local/go #gosetup
GOPATH=/home/henry/go #gosetup
/usr/local/go/bin/go test -c -o /tmp/GoLand/___gobench_redis_test_go.test /code/go/test1/redis_test.go #gosetup
/tmp/GoLand/___gobench_redis_test_go.test -test.v -test.paniconexit0 -test.bench ^\QBenchmark100KKeys\E|\QBenchmark100KScan\E|\QBenchmark1MKeys\E|\QBenchmark1MScan\E$ -test.run ^$
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
Benchmark100KKeys
redis_test.go:95: Prepare 100K keys for keys command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
redis_test.go:95: Prepare 100K keys for keys command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
redis_test.go:95: Prepare 100K keys for keys command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
Benchmark100KKeys-8 632 1939207 ns/op
Benchmark100KScan
redis_test.go:108: Prepare 100K keys for scan command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
redis_test.go:108: Prepare 100K keys for scan command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
redis_test.go:108: Prepare 100K keys for scan command
100K keys size: dbsize: 100000
Noise keys and target keys count: dbsize: 100010
Benchmark100KScan-8 9496 118768 ns/op
Benchmark1MKeys
redis_test.go:123: Prepare 1M keys for keys command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
redis_test.go:123: Prepare 1M keys for keys command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
Benchmark1MKeys-8 20 57191107 ns/op
Benchmark1MScan
redis_test.go:136: Prepare 1M keys for scan command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
redis_test.go:136: Prepare 1M keys for scan command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
redis_test.go:136: Prepare 1M keys for scan command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
redis_test.go:136: Prepare 1M keys for scan command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
redis_test.go:136: Prepare 1M keys for scan command
1M keys size: dbsize: 1000000
Noise keys and target keys count: dbsize: 1000010
Benchmark1MScan-8 18528 93045 ns/op
PASS
Process finished with the exit code 0
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"log"
"strconv"
"testing"
)
var ctx = context.Background()
// Init redis connection
func InitRedisConnect() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
}
// Init redis connection
var c = InitRedisConnect()
func TestKeysCommandCorrect(t *testing.T) {
c.FlushDB(ctx)
for i := 0; i < 10; i++ {
_ = c.Set(ctx, "a:"+strconv.Itoa(i), "1", 0).Err()
}
fmt.Println("Noise keys and target keys count: ", c.DBSize(ctx))
tempKeys, _ := c.Keys(ctx, "a:*").Result()
if len(tempKeys) != 10 {
log.Fatalln("The result not correct")
}
log.Print("Result ", tempKeys)
}
func TestScanCommandCorrect(t *testing.T) {
c.FlushDB(ctx)
for i := 0; i < 10; i++ {
_ = c.Set(ctx, "a:"+strconv.Itoa(i), "1", 0).Err()
}
fmt.Println("Noise keys and target keys count: ", c.DBSize(ctx))
tempKeys, _, err := c.Scan(ctx, 0, "a:*", 100).Result()
if err != nil {
return
}
if len(tempKeys) != 10 {
log.Fatalln("The result not correct")
}
log.Print("Result ", tempKeys)
}
// Insert 100K keys into DB
func Insert100K() {
c.FlushDB(ctx)
for i := 0; i < 100_000; i++ {
_ = c.Set(ctx, strconv.Itoa(i), "1", 0).Err()
}
fmt.Println("100K keys size: ", c.DBSize(ctx))
}
// Insert 1M keys into DB
func Insert1M() {
c.FlushDB(ctx)
for i := 0; i < 1_000_000; i++ {
_ = c.Set(ctx, strconv.Itoa(i), "1", 0).Err()
}
fmt.Println("1M keys size: ", c.DBSize(ctx))
}
func InsertTargetKeys() {
for i := 0; i < 10; i++ {
_ = c.Set(ctx, "a:"+strconv.Itoa(i), "1", 0).Err()
}
fmt.Println("Noise keys and target keys count: ", c.DBSize(ctx))
}
// Benchmark 100K keys "keys" command performance
func Benchmark100KKeys(b *testing.B) {
b.Log("Prepare 100K keys for keys command")
// Prepare 100K keys data and target 10 keys
Insert100K()
InsertTargetKeys()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Keys(ctx, "a:*")
}
}
// Benchmark 100K keys "scan" command performance
func Benchmark100KScan(b *testing.B) {
b.Log("Prepare 100K keys for scan command")
// Prepare 100K keys data and target 10 keys
Insert100K()
InsertTargetKeys()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Scan(ctx, 0, "a:*", 100)
}
}
// Benchmark 100K keys "keys" command performance
func Benchmark1MKeys(b *testing.B) {
b.Log("Prepare 1M keys for keys command")
// Prepare 1M keys data and target 10 keys
Insert1M()
InsertTargetKeys()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Keys(ctx, "a:*")
}
}
// Benchmark 1M keys "scan" command performance
func Benchmark1MScan(b *testing.B) {
b.Log("Prepare 1M keys for scan command")
// Prepare 1M keys data and target 10 keys
Insert1M()
InsertTargetKeys()
b.ResetTimer()
c.Scan(ctx, 0, "a:*", 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment