From e02d52ce8d45550a4d77f11971e31cf0732e5f0c Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 4 Feb 2020 00:49:40 +0000 Subject: Monitor average tick durations & report them in /spark tps --- .../me/lucko/spark/common/util/RollingAverage.java | 42 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'spark-common/src/main/java/me/lucko/spark/common/util') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java index 514876d..5cf5bb5 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java @@ -37,18 +37,46 @@ public class RollingAverage { } public void add(BigDecimal num) { - this.total = this.total.add(num); - this.samples.add(num); - if (this.samples.size() > this.size) { - this.total = this.total.subtract(this.samples.remove()); + synchronized (this) { + this.total = this.total.add(num); + this.samples.add(num); + if (this.samples.size() > this.size) { + this.total = this.total.subtract(this.samples.remove()); + } } } public double getAverage() { - if (this.samples.isEmpty()) { - return 0; + synchronized (this) { + if (this.samples.isEmpty()) { + return 0; + } + return this.total.divide(BigDecimal.valueOf(this.samples.size()), 30, RoundingMode.HALF_UP).doubleValue(); + } + } + + public double getMax() { + synchronized (this) { + BigDecimal max = BigDecimal.ZERO; + for (BigDecimal sample : this.samples) { + if (sample.compareTo(max) > 0) { + max = sample; + } + } + return max.doubleValue(); + } + } + + public double getMin() { + synchronized (this) { + BigDecimal min = BigDecimal.ZERO; + for (BigDecimal sample : this.samples) { + if (min == BigDecimal.ZERO || sample.compareTo(min) < 0) { + min = sample; + } + } + return min.doubleValue(); } - return this.total.divide(BigDecimal.valueOf(this.samples.size()), 30, RoundingMode.HALF_UP).doubleValue(); } } -- cgit