Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active December 17, 2022 04:24
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 rueian/146b85ff23607fad169306449529cf1e to your computer and use it in GitHub Desktop.
Save rueian/146b85ff23607fad169306449529cf1e to your computer and use it in GitHub Desktop.
package allocs
import (
"context"
"testing"
"time"
"github.com/go-redis/redis/v9" // v9-rc2
"github.com/rueian/rueidis" // v0.0.90
)
func Benchmark(b *testing.B) {
rc, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
defer rc.Close()
gc := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: []string{"127.0.0.1:6379"}})
defer gc.Close()
run := func(b *testing.B, fn func() bool) func(b *testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
b.SetParallelism(64)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() && fn() {
}
})
}
}
if err := rc.Do(context.Background(), rc.B().Set().Key("myk").Value("myv").Build()).Error(); err != nil {
b.Fail()
}
b.Run("RueidisCSC", run(b, func() bool {
v, _ := rc.DoCache(context.Background(), rc.B().Get().Key("myk").Cache(), time.Minute).ToString()
return v == "myv"
}))
b.Run("RueidisGET", run(b, func() bool {
v, _ := rc.Do(context.Background(), rc.B().Get().Key("myk").Build()).ToString()
return v == "myv"
}))
b.Run("GoredisGET", run(b, func() bool {
v, _ := gc.Get(context.Background(), "myk").Result()
return v == "myv"
}))
}
// Result:
// ▶ go test -bench=Benchmark -benchmem -benchtime 2s .
// goos: darwin
// goarch: arm64
// pkg: rueidis-benchmark/allocs
// Benchmark/RueidisCSC-10 15000582 158.6 ns/op 0 B/op 0 allocs/op
// Benchmark/RueidisGET-10 5815784 454.8 ns/op 3 B/op 1 allocs/op
// Benchmark/GoredisGET-10 411134 5863 ns/op 200 B/op 8 allocs/op
// PASS
// ok rueidis-benchmark/allocs 8.475s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment