aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/util
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2019-05-13 12:51:54 +0100
committerLuck <git@lucko.me>2019-05-13 12:51:54 +0100
commitb1bdc139b48517a8bcc88888147f5f1a65b48f93 (patch)
treeeb6aaa3bc5ba38ed39d3ec4b4d943bff38a19f66 /spark-common/src/main/java/me/lucko/spark/common/util
parent6602483b18ff556147094c2e6b816b0898ed698e (diff)
downloadspark-b1bdc139b48517a8bcc88888147f5f1a65b48f93.tar.gz
spark-b1bdc139b48517a8bcc88888147f5f1a65b48f93.tar.bz2
spark-b1bdc139b48517a8bcc88888147f5f1a65b48f93.zip
Change the way CPU monitoring is performed
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.java53
1 files changed, 53 insertions, 0 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
new file mode 100644
index 0000000..5b1fd21
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java
@@ -0,0 +1,53 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+public class RollingAverage {
+
+ private final Queue<BigDecimal> samples = new ArrayDeque<>();
+ private final int size;
+ private BigDecimal total = BigDecimal.ZERO;
+
+ public RollingAverage(int size) {
+ this.size = size;
+ }
+
+ 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());
+ }
+ }
+
+ public double getAverage() {
+ if (this.samples.isEmpty()) {
+ return 0;
+ }
+ return this.total.divide(BigDecimal.valueOf(this.samples.size()), 30, RoundingMode.HALF_UP).doubleValue();
+ }
+
+}