Display timing/duration of evaluation

Ville Misaki ville at misaki.fi
Mon Nov 9 13:49:44 UTC 2015


This came up during the session in JavaOne, and I thought to give it a
shot: display how long each evaluation took.

While this might not be a killer-feature (and my prototype implementation
is as naive as they get, no attempts at digging CPU time or similar), at
least I'm totally addicted to the timing feature of DB clients, always
keeping it on for quick prototyping and evaluation of query performance. I
would imagine it would have similar use when prototyping with Java.

Attached is a patch for my prototype. The timing display is off by default,
and can be toggled by /timing. There are different outputs for concise and
fluff, and the output is omitted for non-interactive outputs.

Comments?

- Ville
-------------- next part --------------
# HG changeset patch
# Parent  ab33a84365a033fa30f1301eb85d80a2b6407ca7

diff -r ab33a84365a0 -r a776bc351e64 src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Thu Nov 05 08:15:46 2015 -0800
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Mon Nov 09 22:23:01 2015 +0900
@@ -42,6 +42,8 @@
 import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.time.Duration;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -141,6 +143,7 @@

     private boolean debug = false;
     private boolean displayPrompt = true;
+    private boolean displayTiming = false;
     public boolean testPrompt = false;
     private Feedback feedback = Feedback.Default;
     private String cmdlineClasspath = null;
@@ -462,12 +465,20 @@
                 if (!trimmed.isEmpty()) {
                     String line = incomplete + trimmed;

+                    Instant start = Instant.now();
+
                     // No commands in the middle of unprocessed source
                     if (incomplete.isEmpty() && line.startsWith("/") && !line.startsWith("//") && !line.startsWith("/*")) {
                         processCommand(line.trim());
                     } else {
                         incomplete = processSourceCatchingReset(line);
                     }
+
+                    if (in.interactiveOutput() && displayTiming) {
+                        double duration = Duration.between(start, Instant.now()).toMillis() / 1000;
+                        fluff("Time elapsed: %.2fs\n", duration);
+                        concise("%.2fs\n", duration);
+                    }
                 }
             }
         } catch (IOException ex) {
@@ -687,6 +698,9 @@
         registerCommand(new Command("/prompt", "/p", null, "toggle display of a prompt",
                                     arg -> cmdPrompt(),
                                     EMPTY_COMPLETION_PROVIDER));
+        registerCommand(new Command("/timing", null, null, "toggle display of timing",
+                                    arg -> cmdTiming(),
+                                    EMPTY_COMPLETION_PROVIDER));
         registerCommand(new Command("/classpath", "/cp", "<path>", "add a path to the classpath",
                                     arg -> cmdClasspath(arg),
                                     classPathCompletion()));
@@ -1132,6 +1146,12 @@
         concise("Prompt: %s", displayPrompt ? "on" : "off");
     }

+    private void cmdTiming() {
+        displayTiming = !displayTiming;
+        fluff("Timing will %sdisplay. Use /timing to toggle.", displayTiming ? "" : "NOT ");
+        concise("Timing: %s", displayTiming ? "on" : "off");
+    }
+
     private void cmdReset() {
         live = false;
         fluff("Resetting state.");


More information about the kulla-dev mailing list