Skip to content

Instantly share code, notes, and snippets.

@maguec
Created March 4, 2022 21:32
Show Gist options
  • Save maguec/7cce27f3d9f7a031c2a6d72bf55d2bea to your computer and use it in GitHub Desktop.
Save maguec/7cce27f3d9f7a031c2a6d72bf55d2bea to your computer and use it in GitHub Desktop.
Redis pub/sub benchmark subscriber
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/go-redis/redis/v8"
"github.com/pborman/getopt/v2"
)
func subworker(
id int,
ctx context.Context,
redisOps redis.Options,
) {
redisClient := redis.NewClient(&redisOps)
subscriber := redisClient.Subscribe(ctx, "KEY")
for {
_, err := subscriber.ReceiveMessage(ctx)
if err != nil {
fmt.Println(err)
continue
}
//fmt.Println(id, msg.Channel, msg.Payload)
}
}
func main() {
var ctx = context.Background()
helpFlag := getopt.BoolLong("help", 'h', "display help")
redisHost := getopt.StringLong("host", 's', "127.0.0.1", "Redis Host")
redisPassword := getopt.StringLong("password", 'a', "", "Redis Password")
redisPort := getopt.IntLong("port", 'p', 6379, "Redis Port")
threadCount := getopt.IntLong("threads", 't', 10, "run this many threads")
getopt.Parse()
if *helpFlag {
getopt.PrintUsage(os.Stderr)
os.Exit(1)
}
client := redis.Options{
Addr: fmt.Sprintf("%s:%d", *redisHost, *redisPort),
Password: *redisPassword,
PoolSize: 2,
MinIdleConns: 1,
PoolTimeout: 0,
IdleTimeout: 20 * time.Second,
DialTimeout: 2 * time.Second,
}
for w := 1; w <= *threadCount; w++ {
go subworker(w, ctx, client)
}
for {
time.Sleep(time.Second)
}
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment