Author: jawi
Date: Wed Mar  5 16:04:09 2014
New Revision: 1574549

URL: http://svn.apache.org/r1574549
Log:
Added time function to time the total execution time of other functions.


Modified:
    ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/misc/MiscCommands.java

Modified: 
ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/misc/MiscCommands.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/misc/MiscCommands.java?rev=1574549&r1=1574548&r2=1574549&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/misc/MiscCommands.java 
(original)
+++ 
ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/misc/MiscCommands.java 
Wed Mar  5 16:04:09 2014
@@ -21,17 +21,40 @@ package org.apache.ace.gogo.misc;
 import java.util.Timer;
 import java.util.TimerTask;
 
+import org.apache.felix.service.command.CommandSession;
 import org.apache.felix.service.command.Descriptor;
+import org.apache.felix.service.command.Function;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 
 public class MiscCommands {
 
     public final static String SCOPE = "misc";
-    public final static String[] FUNCTIONS = new String[] { "shutdown", 
"sleep" };
+    public final static String[] FUNCTIONS = new String[] { "shutdown", 
"sleep", "time" };
 
     private volatile BundleContext m_context;
 
+    @Descriptor("report the time the execution of one or more given 
function(s) took")
+    public void time(CommandSession session, Function[] args) throws Exception 
{
+        if (args.length < 1) {
+            throw new IllegalArgumentException("Usage: time {op1} ... {opN}");
+        }
+        long start = System.currentTimeMillis();
+        for (Function func : args) {
+            func.execute(session, null);
+        }
+        long diff = System.currentTimeMillis() - start;
+
+        // Try to format into a little more readable units...
+        double time = diff;
+        String unit = "msec";
+        if (time > 1000.0) {
+            time = time / 1000.0;
+            unit = "sec";
+        }
+        session.getConsole().printf("execution took %.3f %s.%n", time, unit);
+    }
+
     @Descriptor("let the thread sleep")
     public void sleep(long delay) {
         try {


Reply via email to