我曾经time.Now().UnixNano()计算代码某些部分的执行时间,但我发现了一件有趣的事情。有时 IO 操作后经过的时间为零!它出什么问题了?
代码在Go 1.11中运行,并使用标准库"time"。Redis 库是"github.com/mediocregopher/radix.v2/redis". redis服务器版本是3.2。我在 Windows 上使用 VSCode 编辑器运行它。
isGatherTimeStat = false
if rand.Intn(100) < globalConfig.TimeStatProbability { // Here I set TimeStatProbability 100
isGatherTimeStat = true
}
if isGatherTimeStat {
timestampNano = time.Now()
}
globalLogger.Info("time %d", time.Now().UnixNano())
resp := t.redisConn.Cmd("llen", "log_system")
globalLogger.Info("time %d", time.Now().UnixNano())
if isGatherTimeStat {
currentTimeStat.time = time.Since(timestampNano).Nanoseconds()
currentTimeStat.name = "redis_llen"
globalLogger.Info("redis_llen time sub == %d", currentTimeStat.time)
select {
case t.chTimeStat <- currentTimeStat:
default:
}
}
以下是一些日志:
[INFO ][2019-07-31][14:47:53] time 1564555673269444200
[INFO ][2019-07-31][14:47:53] time 1564555673269444200
[INFO ][2019-07-31][14:47:53] redis_llen time sub == 0
[INFO ][2019-07-31][14:47:58] time 1564555678267691700
[INFO ][2019-07-31][14:47:58] time 1564555678270689300
[INFO ][2019-07-31][14:47:58] redis_llen time sub == 2997600
[INFO ][2019-07-31][14:48:03] time 1564555683268195600
[INFO ][2019-07-31][14:48:03] time 1564555683268195600
[INFO ][2019-07-31][14:48:03] redis_llen time sub == 0
[INFO ][2019-07-31][14:48:08] time 1564555688267631100
[INFO ][2019-07-31][14:48:08] time 1564555688267631100
[INFO ][2019-07-31][14:48:08] redis_llen time sub == 0
慕运维8079593
繁华开满天机
相关分类