2022-02-25 19:29:39 +08:00
package statistics
2022-02-25 19:39:42 +08:00
import (
2023-03-15 15:26:37 +08:00
"context"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2022-02-25 19:39:42 +08:00
"time"
)
2022-02-25 19:29:39 +08:00
type Statistics struct {
2022-05-09 18:23:06 +08:00
AllCount * uint64
2022-02-25 19:39:42 +08:00
ModuleName string
PrintArgs string
2022-05-18 21:58:32 +08:00
SleepTime uint64
2022-02-25 19:29:39 +08:00
}
func ( s * Statistics ) output ( ) {
2022-05-10 10:52:32 +08:00
var intervalCount uint64
2022-02-25 19:39:42 +08:00
t := time . NewTicker ( time . Duration ( s . SleepTime ) * time . Second )
defer t . Stop ( )
2022-03-09 11:25:51 +08:00
var sum uint64
2022-05-18 21:09:49 +08:00
var timeIntervalNum uint64
2022-02-25 19:29:39 +08:00
for {
2022-05-09 18:23:06 +08:00
sum = * s . AllCount
2022-02-25 19:39:42 +08:00
select {
case <- t . C :
}
2022-05-10 10:52:32 +08:00
if * s . AllCount - sum <= 0 {
intervalCount = 0
} else {
intervalCount = * s . AllCount - sum
}
2022-05-18 21:09:49 +08:00
timeIntervalNum ++
2023-03-15 15:26:37 +08:00
log . ZWarn ( context . Background ( ) , " system stat " , nil , "args" , s . PrintArgs , "intervalCount" , intervalCount , "total:" , * s . AllCount , "intervalNum" , timeIntervalNum , "avg" , ( * s . AllCount ) / ( timeIntervalNum ) / s . SleepTime )
2022-02-25 19:29:39 +08:00
}
}
2022-05-09 18:23:06 +08:00
func NewStatistics ( allCount * uint64 , moduleName , printArgs string , sleepTime int ) * Statistics {
2022-05-18 21:58:32 +08:00
p := & Statistics { AllCount : allCount , ModuleName : moduleName , SleepTime : uint64 ( sleepTime ) , PrintArgs : printArgs }
2022-02-25 19:29:39 +08:00
go p . output ( )
2022-02-25 19:39:42 +08:00
return p
2022-02-25 19:29:39 +08:00
}