Display timing/duration of evaluation
Ville Misaki
ville at misaki.fi
Mon Nov 9 22:01:26 UTC 2015
On Tue, Nov 10, 2015 at 6:03 AM, John Rose <john.r.rose at oracle.com> wrote:
> I'd prefer a threshold time: If the execution takes less than the
> threshold, nothing is printed.
>
Great idea, and makes a lot of sense. I've updated my implementation to
include it (a new patch of the whole implementation attached).
The /timing now takes an optional parameter, using which you can set the
threshold to any non-negative value.
- Ville
-------------- next part --------------
# HG changeset patch
# Parent ab33a84365a033fa30f1301eb85d80a2b6407ca7
diff -r ab33a84365a0 -r 89bbda0fdcfb 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,8 @@
private boolean debug = false;
private boolean displayPrompt = true;
+ private boolean displayTiming = false;
+ private double displayTimingThreshold = 5.0;
public boolean testPrompt = false;
private Feedback feedback = Feedback.Default;
private String cmdlineClasspath = null;
@@ -462,12 +466,22 @@
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 = 1.0 * Duration.between(start, Instant.now()).toMillis() / 1000;
+ if (duration >= displayTimingThreshold) {
+ fluff("Time elapsed: %.2fs\n", duration);
+ concise("%.2fs\n", duration);
+ }
+ }
}
}
} catch (IOException ex) {
@@ -687,6 +701,9 @@
registerCommand(new Command("/prompt", "/p", null, "toggle display of a prompt",
arg -> cmdPrompt(),
EMPTY_COMPLETION_PROVIDER));
+ registerCommand(new Command("/timing", null, "[threshold]", "toggle display of timing or set the threshold",
+ arg -> cmdTiming(arg),
+ EMPTY_COMPLETION_PROVIDER));
registerCommand(new Command("/classpath", "/cp", "<path>", "add a path to the classpath",
arg -> cmdClasspath(arg),
classPathCompletion()));
@@ -1132,6 +1149,26 @@
concise("Prompt: %s", displayPrompt ? "on" : "off");
}
+ private void cmdTiming(String arg) {
+ if (arg == null || arg.isEmpty()) {
+ displayTiming = !displayTiming;
+ } else {
+ try {
+ double threshold = Double.parseDouble(arg);
+ if (threshold >= 0) {
+ displayTiming = true;
+ displayTimingThreshold = threshold;
+ } else {
+ hard("Threshold value must be non-negative.");
+ }
+ } catch (NumberFormatException e) {
+ hard("Invalid threshold value: %s", arg);
+ }
+ }
+ fluff("Timing will %sdisplay. Threshold is %.2fs. Use /timing to toggle.", displayTiming ? "" : "NOT ", displayTimingThreshold);
+ concise("Timing: %s, %.2f", displayTiming ? "on" : "off", displayTimingThreshold);
+ }
+
private void cmdReset() {
live = false;
fluff("Resetting state.");
More information about the kulla-dev
mailing list