代码拉取完成,页面将自动刷新
package monitor
import (
"context"
"fmt"
"path/filepath"
"runtime"
"sync"
"time"
)
type Monitor struct {
}
var (
tick *time.Ticker
m sync.Map
buf []string
)
func mRangeCallBack(key, value any) bool {
ss := value.(*SyncSlice)
totalTime := time.Duration(0)
ssCount := ss.Count()
if ssCount > 0 {
sss := ss.Clear()
for _, v := range sss {
totalTime += v.(time.Duration)
}
buf = append(buf, fmt.Sprintf(`[%s] runs for %d times, with an average running time of %v.`, key, ssCount, totalTime/time.Duration(ssCount)))
}
return true
}
func Run() {
if mFlag&Close != 0 {
return
}
if mFlag&Periodic == 0 {
return
}
go func() {
for {
select {
case <-tick.C:
m.Range(mRangeCallBack)
if len(buf) > 0 {
for _, v := range buf {
tccb(v)
}
buf = buf[:0]
}
}
}
}()
}
// TimeCost ,The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller.
func (this *Monitor) TimeCost(ctx context.Context, skip int) func() {
if mFlag&Close != 0 {
return func() {}
}
start := time.Now()
return func() {
pc, _, lineNo, ok := runtime.Caller(skip)
if !ok {
return
}
path_name := runtime.FuncForPC(pc).Name()
_, funcName := filepath.Split(path_name)
tc := time.Since(start)
if mFlag&Func != 0 {
tccb(fmt.Sprintf(`[%s:%d] [time cost = %v]`, funcName, lineNo, tc))
}
inter, OK := m.Load(path_name)
if OK != true {
inter = &SyncSlice{}
m.Store(path_name, inter)
}
ss := inter.(*SyncSlice)
ss.Add(tc)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。