aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/music/util/TimeMeasure.java
diff options
context:
space:
mode:
authorMohammad Salehe <salehe@cs.toronto.edu>2018-11-23 12:07:27 -0500
committerMohammad Salehe <salehe@cs.toronto.edu>2018-12-22 14:40:38 -0500
commit964168542a4cfebf19414dba218ca9a5979f9be8 (patch)
treee85fa4bb90fa286d63f96283119b9c62896a65be /src/main/java/org/onap/music/util/TimeMeasure.java
parent9b709f3d33e23710aeae261198806d6a068c5a3b (diff)
Implement time measurement utility
Added a TimeMeasure interface as a utility class to be able to measure running time of specific parts of code. Usually, this will be used for benchmarking. Implemented SamplerHistogramTimeMeasurement that uses reservior sampling to calculate percentiles of large amount of data. Also updated maven pom.xml: - update java version from 1.7 to 1.8 to be able to use java 8 - update guava to 27.0 to use Stats Percentiles - update cassandra java plugin version to 3.6 to fix guava dependency problem Change-Id: I168432ff2e6f5507fedc1678684dd96608703e5a Issue-ID: MUSIC-148 Signed-off-by: Mohammad Salehe <salehe@cs.toronto.edu>
Diffstat (limited to 'src/main/java/org/onap/music/util/TimeMeasure.java')
-rw-r--r--src/main/java/org/onap/music/util/TimeMeasure.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/main/java/org/onap/music/util/TimeMeasure.java b/src/main/java/org/onap/music/util/TimeMeasure.java
new file mode 100644
index 00000000..cbb04ff6
--- /dev/null
+++ b/src/main/java/org/onap/music/util/TimeMeasure.java
@@ -0,0 +1,52 @@
+package org.onap.music.util;
+
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.concurrent.ThreadLocalRandom;
+
+public interface TimeMeasure {
+ void enter(String context);
+
+ void exit();
+
+ /*
+ Return a map of measure contexts to a list of 101 percentiles [0,100]
+ */
+ Map<String, ArrayList<Double>> percentiles();
+
+ /*
+ Returns a map of measure contexts to <mean, sme>
+ */
+ Map<String, Pair<Double, Double>> stats();
+}
+
+class TimeMeasureExample
+{
+ public static void main(String[] args) {
+ TimeMeasure tm = new SamplerHistogramTimeMeasure();
+ double x = 0;
+
+ tm.enter("A");
+ for (int i = 0; i < 100000; i++) {
+ tm.enter("B");
+ tm.enter("C");
+ x += ThreadLocalRandom.current().nextDouble(100);
+ tm.exit();
+ tm.exit();
+ }
+ tm.enter("C");
+ tm.exit();
+ tm.exit();
+
+ System.out.println(x);
+ Map<String, ArrayList<Double>> e = tm.percentiles();
+ Map<String, Pair<Double, Double>> m = tm.stats();
+ DecimalFormat df = new DecimalFormat("000.000000");
+ e.forEach((k,v) -> System.out.println("" + k + "\t\t: " + Arrays.toString(v.stream().map(w -> "" + df.format(w)).toArray())));
+ m.forEach((k,v) -> System.out.println("" + k + "\t\t: " + df.format(v.getLeft()) + " (" + df.format(v.getRight()) + ")"));
+ }
+}