open-im-server/pkg/statistics/statistics.go
Xinwei Xiong(cubxxw-openim) 166ddb1e34 style: add format
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-07-10 18:13:29 +08:00

57 lines
1.0 KiB
Go

package statistics
import (
"context"
"time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
)
type Statistics struct {
AllCount *uint64
ModuleName string
PrintArgs string
SleepTime uint64
}
func (s *Statistics) output() {
var intervalCount uint64
t := time.NewTicker(time.Duration(s.SleepTime) * time.Second)
defer t.Stop()
var sum uint64
var timeIntervalNum uint64
for {
sum = *s.AllCount
select {
case <-t.C:
}
if *s.AllCount-sum <= 0 {
intervalCount = 0
} else {
intervalCount = *s.AllCount - sum
}
timeIntervalNum++
log.ZWarn(
context.Background(),
" system stat ",
nil,
"args",
s.PrintArgs,
"intervalCount",
intervalCount,
"total:",
*s.AllCount,
"intervalNum",
timeIntervalNum,
"avg",
(*s.AllCount)/(timeIntervalNum)/s.SleepTime,
)
}
}
func NewStatistics(allCount *uint64, moduleName, printArgs string, sleepTime int) *Statistics {
p := &Statistics{AllCount: allCount, ModuleName: moduleName, SleepTime: uint64(sleepTime), PrintArgs: printArgs}
go p.output()
return p
}