代码拉取完成,页面将自动刷新
package perf
import (
"fmt"
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/random"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/timer"
"gitee.com/h79/goutils/plugins"
"go.uber.org/zap"
"path/filepath"
"runtime"
"time"
)
type Config struct {
DisableLog bool `json:"disableLog" yaml:"disableLog"`
DisableWarn bool `json:"disableWarn" yaml:"disableWarn"`
}
type Perf struct {
Config
id string
model string
funName string
file string
line int
warn int64
start int64
mid int64
}
func (p *Perf) EnableLog(enable bool) *Perf {
p.DisableLog = !enable
return p
}
func (p *Perf) EnableWarn(enable bool) *Perf {
p.DisableWarn = !enable
return p
}
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()
p.mid = p.start
if !p.DisableLog {
zap.L().WithOptions(zap.AddCallerSkip(1)).Debug("Perf",
zap.String("perfId", p.id),
zap.String("model", p.model),
zap.String("detail", "start"),
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
p.mid = end
p.waring(end, dif, "end")
}
func (p *Perf) Out(detail string) {
end := timer.CurrentMS()
dif := end - p.mid
p.mid = end
p.waring(end, dif, detail)
}
func (p *Perf) waring(end, dif int64, detail string) {
if !p.DisableLog {
zap.L().WithOptions(zap.AddCallerSkip(1)).Debug("Perf",
zap.String("perfId", p.id),
zap.String("model", p.model),
zap.String("detail", detail),
zap.Int64("end", end),
zap.Duration("dif", time.Duration(dif)*time.Millisecond))
}
//执行时间太长,告警
wa := p.warn
if wa <= 0 {
wa = 2000
}
if p.DisableWarn {
return
}
if dif >= p.warn+1000 {
d := debug.New(result.ErrTimeout).
WithDetailFormat("%s, detail=> '%s', run time too long=> %dms", p.info(), detail, dif).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
} else if dif >= p.warn {
d := debug.New(result.ErrTimeout).
WithDetailFormat("%s, detail=> '%s', run time too long=> %dms", p.info(), detail, dif).
WithLevel(debug.DImportantLevel)
_ = plugins.DoWithError(plugins.KAlarm, d)
}
}
func (p *Perf) info() string {
return fmt.Sprintf("perfId: '%s', model: '%s',funcName: '%s', fileName: '%s', line: %d",
p.id, p.model, p.funName, p.file, p.line)
}
func This() *Perf {
pc, file, line, _ := runtime.Caller(1)
return &Perf{
id: random.GenerateNumString(16),
model: "",
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{
id: random.GenerateNumString(16),
model: "",
warn: 2000,
funName: filepath.Base(runtime.FuncForPC(pc).Name()),
file: filepath.Base(file),
line: line,
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。