Files
JChargePointProtocol/jcpp-infrastructure-stats/src/main/java/sanbing/jcpp/infrastructure/stats/StatsTimer.java
2025-03-04 16:43:37 +08:00

47 lines
986 B
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 开源代码,仅供学习和交流研究使用,商用请联系三丙
* 微信mohan_88888
* 抖音:程序员三丙
* 付费课程知识星球https://t.zsxq.com/aKtXo
*/
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;
}
public void record(long timeMs) {
count++;
totalTime += timeMs;
timer.record(timeMs, TimeUnit.MILLISECONDS);
}
public double getAvg() {
if (count == 0) {
return 0.0;
}
return (double) totalTime / count;
}
public void reset() {
count = 0;
totalTime = 0;
}
}