You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2020/01/08 14:51:36 UTC

[juneau] branch master updated: Add min/max to MethodExecStats.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new c8fa3b2  Add min/max to MethodExecStats.
c8fa3b2 is described below

commit c8fa3b2fdedbcd9bd9efb973d050cc710158f41c
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed Jan 8 09:50:56 2020 -0500

    Add min/max to MethodExecStats.
---
 .../org/apache/juneau/utils/MethodInvokerTest.java |  6 ++--
 .../org/apache/juneau/utils/MethodExecStats.java   | 37 ++++++++++++++++++----
 .../org/apache/juneau/utils/MethodInvoker.java     |  4 +--
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/utils/MethodInvokerTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/utils/MethodInvokerTest.java
index 541f9d7..e629d27 100644
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/utils/MethodInvokerTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/utils/MethodInvokerTest.java
@@ -39,7 +39,7 @@ public class MethodInvokerTest {
 		mi.invoke(a);
 		mi.invoke(a);
 
-		assertObjectMatches("{method:'A.foo',runs:3,running:0,errors:0,avgTime:*,totalTime:*,exceptions:[]}", mes);
+		assertObjectMatches("{method:'A.foo',runs:3,running:0,errors:0,minTime:*,maxTime:*,avgTime:*,totalTime:*,exceptions:[]}", mes);
 	}
 
 	@Test
@@ -60,7 +60,7 @@ public class MethodInvokerTest {
 			mi.invoke(a);
 		} catch (Exception e) {}
 
-		assertObjectMatches("{method:'A.bar',runs:3,running:0,errors:3,avgTime:0,totalTime:*,exceptions:[{exception:'RuntimeException',hash:'*',count:3}]}", mes);
+		assertObjectMatches("{method:'A.bar',runs:3,running:0,errors:3,minTime:*,maxTime:*,avgTime:*,totalTime:*,exceptions:[{exception:'RuntimeException',hash:'*',count:3}]}", mes);
 	}
 
 	@Test
@@ -81,7 +81,7 @@ public class MethodInvokerTest {
 			mi.invoke(a, 1, "x");
 		} catch (Exception e) {}
 
-		assertObjectMatches("{method:'A.baz',runs:3,running:0,errors:3,avgTime:0,totalTime:*,exceptions:[{exception:'IllegalArgumentException',hash:'*',count:3}]}", mes);
+		assertObjectMatches("{method:'A.baz',runs:3,running:0,errors:3,minTime:*,maxTime:*,avgTime:*,totalTime:*,exceptions:[{exception:'IllegalArgumentException',hash:'*',count:3}]}", mes);
 	}
 
 	@Test
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
index 831181e..883f48a 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
@@ -24,10 +24,12 @@ import org.apache.juneau.marshall.*;
  *
  * Keeps track of number of starts/finishes on tasks and keeps an average run time.
  */
-@Bean(bpi="method,runs,running,errors,avgTime,totalTime,exceptions")
+@Bean(bpi="method,runs,running,errors,minTime,maxTime,avgTime,totalTime,exceptions")
 public class MethodExecStats implements Comparable<MethodExecStats> {
+
 	private String method;
 	private WeightedAverage avgTime = new WeightedAverage();
+	private volatile int minTime = -1, maxTime;
 
 	private AtomicInteger
 		starts = new AtomicInteger(),
@@ -68,12 +70,15 @@ public class MethodExecStats implements Comparable<MethodExecStats> {
 
 	/**
 	 * Call when task is finished.
-	 * @param time The execution time of the task.
+	 * @param nanoTime The execution time of the task in nanoseconds.
 	 */
-	public void finished(long time) {
+	public void finished(long nanoTime) {
 		finishes.incrementAndGet();
-		totalTime.addAndGet(time);
-		avgTime.add(1, time);
+		int milliTime = (int)(nanoTime/1_000_000);
+		totalTime.addAndGet(nanoTime);
+		avgTime.add(1, nanoTime);
+		minTime = minTime == -1 ? milliTime : Math.min(minTime, milliTime);
+		maxTime = Math.max(maxTime, milliTime);
 	}
 
 	/**
@@ -122,12 +127,30 @@ public class MethodExecStats implements Comparable<MethodExecStats> {
 	}
 
 	/**
+	 * Returns the max execution time.
+	 *
+	 * @return The average execution time in milliseconds.
+	 */
+	public int getMinTime() {
+		return minTime == -1 ? 0 : minTime;
+	}
+
+	/**
+	 * Returns the max execution time.
+	 *
+	 * @return The average execution time in milliseconds.
+	 */
+	public int getMaxTime() {
+		return maxTime;
+	}
+
+	/**
 	 * Returns the average execution time.
 	 *
 	 * @return The average execution time in milliseconds.
 	 */
 	public int getAvgTime() {
-		return (int)avgTime.getValue();
+		return (int)avgTime.getValue() / 1_000_000;
 	}
 
 	/**
@@ -136,7 +159,7 @@ public class MethodExecStats implements Comparable<MethodExecStats> {
 	 * @return The total execution time in milliseconds.
 	 */
 	public long getTotalTime() {
-		return totalTime.get();
+		return totalTime.get() / 1_000_000;
 	}
 
 	/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
index 4290f6a..393128f 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
@@ -52,7 +52,7 @@ public class MethodInvoker {
 	 * @throws InvocationTargetException Thrown from underlying method.
 	 */
 	public Object invoke(Object o, Object...args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
-		long startTime = System.currentTimeMillis();
+		long startTime = System.nanoTime();
 		stats.started();
 		try {
 			return m.invoke(o, args);
@@ -63,7 +63,7 @@ public class MethodInvoker {
 			stats.error(e.getTargetException());
 			throw e;
 		} finally {
-			stats.finished(System.currentTimeMillis() - startTime);
+			stats.finished(System.nanoTime() - startTime);
 		}
 	}