2024-10-08 09:38:54 +08:00
|
|
|
|
/**
|
2025-03-04 10:42:17 +08:00
|
|
|
|
* 开源代码,仅供学习和交流研究使用,商用请联系三丙
|
|
|
|
|
|
* 微信:mohan_88888
|
|
|
|
|
|
* 抖音:程序员三丙
|
|
|
|
|
|
* 付费课程知识星球:https://t.zsxq.com/aKtXo
|
2024-10-08 09:38:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
package sanbing.jcpp.infrastructure.stats;
|
|
|
|
|
|
|
|
|
|
|
|
import io.micrometer.core.instrument.Timer;
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
public class StatsTimer {
|
|
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
|
private final String name;
|
|
|
|
|
|
private final Timer timer;
|
|
|
|
|
|
|
|
|
|
|
|
private int count;
|
|
|
|
|
|
private long totalTime;
|
|
|
|
|
|
|
|
|
|
|
|
public StatsTimer(String name, Timer micrometerTimer) {
|
|
|
|
|
|
this.name = name;
|
|
|
|
|
|
this.timer = micrometerTimer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-04 16:43:37 +08:00
|
|
|
|
public void record(long timeMs) {
|
2024-10-08 09:38:54 +08:00
|
|
|
|
count++;
|
|
|
|
|
|
totalTime += timeMs;
|
|
|
|
|
|
timer.record(timeMs, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double getAvg() {
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
|
return 0.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (double) totalTime / count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-04 16:43:37 +08:00
|
|
|
|
public void reset() {
|
2024-10-08 09:38:54 +08:00
|
|
|
|
count = 0;
|
|
|
|
|
|
totalTime = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|