下面是从 Redis 获取值(value)的示例片段。我正在通过管道传输 3 个 redis 命令并获取值。这里的问题是“缺少毫秒”。 Redis 管道花费的时间明显更短(小于 5 毫秒),但执行 Get 操作花费的总时间超过 10 毫秒。不确定哪个操作需要时间,解码不是问题,因为我测量了 len(bytes) 和时间。任何帮助深表感谢。
请求/秒 = 300,在 3 个 AWS 大型实例上运行,具有强大的 25GB redis 实例。使用 10 个默认连接。
func Get(params...) <-chan CacheResult {
start := time.Now()
var res CacheResult
defer func() {
resCh <- res
}()
type timers struct {
total time.Duration
pipeline time.Duration
unmarshal time.Duration
}
t := timers{}
startPipeTime := time.Now()
// pipe line commands
pipe := c.client.Pipeline()
// 3 commands pipelined (HGET, HEGT, GET)
if _, res.Err = pipe.Exec(); res.Err != nil && res.Err != redis.Nil {
return resCh
}
sinceStartPipeTime := time.Since(startPipeTime)
// get query values like below for HGET & GET
if val, res.Err = cachedValue.Bytes(); res.Err != nil {
return resCh
}
// Unmarshal the query value
startUnmarshalTime := time.Now()
var cv common.CacheValue
if res.Err = json.Unmarshal(val, &cv); res.Err != nil {
return resCh
}
sinceStartUnmarshalTime := time.Since(startUnmarshalTime)
t.unmarshal = sinceStartUnmarshalTime
endTime := time.Since(start)
xlog.Infof("Timings total:%s, "+
"pipeline(redis):%s, unmarshaling(%vB):%s", t.total, t.pipeline, len(val), t.unmarshal)
return resCh
最佳答案
执行一条redis命令的时间包括:
- 应用服务器预处理
- 应用服务器和redis服务器之间的往返时间
- Redis 服务器处理时间
在正常操作中,(2) 花费的时间最多。
关于performance - 执行redis操作的Go代码中缺少毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50323873/