单个命令执行
package main import ( "context" "fmt" "github.com/go-redis/redis/v8" "time" ) func main() { // 创建Redis客户端 rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) // 单个命令执行 start := time.Now() for i := 0; i < 1000; i++ { rdb.Ping(context.Background()).Result() } elapsed := time.Since(start) fmt.Printf("Elapsed time for single command execution: %s\n", elapsed) }
一次性使用管道(Pipelined)
package main import ( "context" "fmt" "github.com/go-redis/redis/v8" "time" ) func main() { // 创建Redis客户端 rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) // 一次性使用管道(Pipelined) start := time.Now() pipe := rdb.Pipeline() for i := 0; i < 1000; i++ { pipe.Ping(context.Background()) } _, err := pipe.Exec(context.Background()) if err != nil { panic(err) } elapsed := time.Since(start) fmt.Printf("Elapsed time for pipelined execution: %s\n", elapsed) }
事务管道(TxPipelined)
package main import ( "context" "fmt" "github.com/go-redis/redis/v8" "time" ) func main() { // 创建Redis客户端 rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) // 事务管道(TxPipelined) start := time.Now() pipe := rdb.TxPipeline() for i := 0; i < 1000; i++ { pipe.Ping(context.Background()) } _, err := pipe.Exec(context.Background()) if err != nil { panic(err) } elapsed := time.Since(start) fmt.Printf("Elapsed time for transactional pipelined execution: %s\n", elapsed) }
标签:多个,start,Redis,fmt,redis,命令,context,time,main From: https://www.cnblogs.com/beatle-go/p/18191218