2023-06-30 17:59:38 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-06-29 22:35:31 +08:00
package statistics
import (
2023-06-30 09:45:02 +08:00
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2023-06-29 22:35:31 +08:00
"time"
)
type Statistics struct {
2023-06-30 09:45:02 +08:00
AllCount * uint64
2023-06-29 22:35:31 +08:00
ModuleName string
PrintArgs string
2023-06-30 09:45:02 +08:00
SleepTime uint64
2023-06-29 22:35:31 +08:00
}
func ( s * Statistics ) output ( ) {
2023-06-30 09:45:02 +08:00
var intervalCount uint64
2023-06-29 22:35:31 +08:00
t := time . NewTicker ( time . Duration ( s . SleepTime ) * time . Second )
defer t . Stop ( )
var sum uint64
2023-06-30 09:45:02 +08:00
var timeIntervalNum uint64
2023-06-29 22:35:31 +08:00
for {
2023-06-30 09:45:02 +08:00
sum = * s . AllCount
2023-06-29 22:35:31 +08:00
select {
case <- t . C :
}
2023-06-30 09:45:02 +08:00
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 )
2023-06-29 22:35:31 +08:00
}
}
2023-06-30 09:45:02 +08:00
func NewStatistics ( allCount * uint64 , moduleName , printArgs string , sleepTime int ) * Statistics {
p := & Statistics { AllCount : allCount , ModuleName : moduleName , SleepTime : uint64 ( sleepTime ) , PrintArgs : printArgs }
2023-06-29 22:35:31 +08:00
go p . output ( )
return p
}