1 Star 0 Fork 0

wuzpdev/goworld

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
profile.go 1.29 KB
Copy Edit Raw Blame History
seis authored 2017-06-29 11:39 . GetMails
package main
import (
"fmt"
"os"
"sync"
"time"
)
var (
profLock sync.Mutex
profSectionStartTime time.Time
thingMaxTime map[string]time.Duration
thingMinTime map[string]time.Duration
thingTimes map[string]time.Duration
thingCounts map[string]int
)
func recordThingTime(thing string, d time.Duration) {
profLock.Lock()
now := time.Now()
if now.Sub(profSectionStartTime) >= time.Second {
// start new profile section
dumpThingProfile()
profSectionStartTime = now
thingTimes = map[string]time.Duration{}
thingCounts = map[string]int{}
thingMinTime = map[string]time.Duration{}
thingMaxTime = map[string]time.Duration{}
}
thingCounts[thing] += 1
thingTimes[thing] += d
if oldMaxTime, ok := thingMaxTime[thing]; !ok || oldMaxTime < d {
thingMaxTime[thing] = d
}
if oldMinTime, ok := thingMaxTime[thing]; !ok || oldMinTime > d {
thingMinTime[thing] = d
}
profLock.Unlock()
}
func dumpThingProfile() {
for thing, count := range thingCounts {
totalTime := thingTimes[thing]
fmt.Fprintf(os.Stdout, "> %-32s *%d AVG %s RANGE %s ~ %s\n", thing, count, totalTime/time.Duration(count), thingMinTime[thing], thingMaxTime[thing])
}
fmt.Fprintln(os.Stdout, "===============================================================================")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.6

Search