代码拉取完成,页面将自动刷新
package perf
import (
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/timer"
"gitee.com/h79/goutils/plugins"
"go.uber.org/zap"
"path/filepath"
"runtime"
)
type Perf struct {
model string
funName string
file string
line int
warn int64
start int64
}
func (p *Perf) WithModel(model string) *Perf {
p.model = model
return p
}
func (p *Perf) WithWarn(t int64) *Perf {
p.warn = t
return p
}
func (p *Perf) Start() *Perf {
p.start = timer.CurrentMS()
logger.Context().Debug("Exec",
zap.String("model", p.model),
zap.Int64("Start", p.start),
zap.String("funcName", p.funName),
zap.String("fileName", p.file),
zap.Int("line", p.line))
return p
}
func (p *Perf) End() {
end := timer.CurrentMS()
dif := end - p.start
logger.Context().Debug("Exec",
zap.String("model", p.model),
zap.Int64("End", end),
zap.Int64("dif", dif))
//执行时间太长,告警
wa := p.warn
if wa <= 0 {
wa = 2000
}
if dif >= p.warn+1000 {
d := debug.New(result.ErrTimeout).
WithDetail("'%s' call %s function, run time too long= %d, in %v line= %d", p.model, p.funName, dif, p.file, p.line).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
if dif >= p.warn {
d := debug.New(result.ErrTimeout).
WithDetail("'%s' call %s function, run time too long= %d,in %v line= %d", p.model, p.funName, dif, p.file, p.line).
WithLevel(debug.DImportantLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
}
func This() *Perf {
pc, file, line, _ := runtime.Caller(1)
return &Perf{
warn: 2000,
funName: filepath.Base(runtime.FuncForPC(pc).Name()),
file: filepath.Base(file),
line: line,
}
}
func Caller() *Perf {
pc, file, line, _ := runtime.Caller(2)
return &Perf{
warn: 2000,
funName: filepath.Base(runtime.FuncForPC(pc).Name()),
file: filepath.Base(file),
line: line,
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。