Files

69 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-07-04 11:15:20 +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"
2023-06-29 22:35:31 +08:00
"time"
2023-07-03 14:12:29 +08:00
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/log"
2023-06-29 22:35:31 +08:00
)
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
2024-02-26 21:47:54 +08:00
<-t.C
2023-06-30 09:45:02 +08:00
if *s.AllCount-sum <= 0 {
intervalCount = 0
} else {
intervalCount = *s.AllCount - sum
}
timeIntervalNum++
2023-07-03 16:29:22 +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,
)
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
}