diff options
author | Luck <git@lucko.me> | 2020-02-04 00:49:40 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2020-02-04 00:49:40 +0000 |
commit | e02d52ce8d45550a4d77f11971e31cf0732e5f0c (patch) | |
tree | cfcfb2850ff6b279276e43233e5e1acf82993a98 /spark-common/src/main/java/me/lucko/spark/common/util | |
parent | d15a12788ddc8aba09f49003fcef55b927850de3 (diff) | |
download | spark-e02d52ce8d45550a4d77f11971e31cf0732e5f0c.tar.gz spark-e02d52ce8d45550a4d77f11971e31cf0732e5f0c.tar.bz2 spark-e02d52ce8d45550a4d77f11971e31cf0732e5f0c.zip |
Monitor average tick durations & report them in /spark tps
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java | 42 |
1 files changed, 35 insertions, 7 deletions
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(); } } |