You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fish shell has a $CMD_DURATION variable which is the number of milliseconds that the last interactive command ran. I use that in my right prompt to show a human friendly duration for the most recently run command. An Elvish user can simulate that via the $edit:before-readline and $edit:after-readline hooks but that has problems.
First, it requires a lot of effort from the user to produce data that should be provided by the shell.
Second, unless you carefully structure the implementation, similar to https://github.com/zzamboni/elvish-modules/blob/master/long-running-notifications.elv, you won't get even a semi-accurate measurement. Even @zzamboni's carefully written solution is less accurate than it could be because it includes additional overhead that could materially increase the computed duration. A naive user will attempt to save the start time in the $edit:before-readline hook and use that in the $edit:after-readline hook to calculate the duration. Oops! The duration now includes the time between issuing a new prompt and the user pressing [enter].
I would like to see something like a $edit:cmd-duration variable introduced. This should not be in the builtin namespace because we don't want, or need, to measure the elapsed duration of every statement by default. This is an interactive REPL feature primarily meant to be used in the interactive prompts or related command hooks.
Also, note that the solution used by https://github.com/zzamboni/elvish-modules/blob/master/long-running-notifications.elv has a resolution of seconds. The proposed $edit:cmd-duration variable should have millisecond, or better, resolution. And the @zzamboni implementation requires running the external date command. Which shouldn't be necessary. But that's a separate issue.
I'd rather we introduce an after-cmd hook and supply this information as part of the hook, which localizes the information instead of making it a global variable.
An after-cmd hook might be a good idea but creates some questions. In particular, it would be an expensive mechanism to solely capture the execution duration of the most recent interactive command.
I agree that assigning a float64 of the elapsed fractional seconds to a edit:-cmd-duration-sec var (I don't see the point of including last- in that var name) is better than a nominal integer scaled by milliseconds or microseconds.
FWIW, On my primary macOS server using the @zzamboni approach, but using date '%s.%N' for nanosecond resolution, consistently results in a command duration of 6 to 8 ms when simply pressing [enter]; i.e., not running any command at all. Which shows that using the existing hooks is unexpectedly expensive.
I agree that it might be better for precision that the shell itself measures and exposes how much a command took. Display/reporting can then of course be done by hooks.
Though honestly, I don't think this is a must - I can't really think of a good use case (other than the kind of thing long-running-notifications does) for having this measurements. So I would consider it as a low priority.
Finally, a question based on the discussion above: isn't the existing before-readline hook effectively the same as the proposed after-cmd hook? What am I missing?
isn't the existing before-readline hook effectively the same as the proposed after-cmd hook?
No, since the after-cmd hook would only fire when a command was actually run. Simply displaying a new prompt, such as from pressing [enter] on an empty command line, wouldn't trigger it. Also, it wouldn't be tied to interactive prompts, making it usable from a script. Which means that a after-cmd hook would eliminate the need for the time command.
What arguments would be passed to a after-cmd hook? Obviously the statement that was run and the elapsed time in seconds. A third argument could be a list of exceptions thrown by the statement or an empty list if there were no exceptions. A fourth argument could be the getrusage data for any external commands. A fifth argument could be the getrusage delta for the shell itself.
I started working on a resolution for this issue and realized that simply pressing Enter should not affect the recorded duration of the most recent command since no command was executed. Also, introducing an after-cmd hook might be useful but is not needed to resolve this issue and needlessly complicates capturing the duration of an interactive statement. The resolution I'm working on introduces a edit:command:duration float64 variable.
I'd rather we introduce an after-cmd hook and supply this information as part of the hook, which localizes the information instead of making it a global variable.
I've been using a change that introduces the $edit:command:-duration variable for a few days. Along with a related change to my ~/.elvish/rc.elv that displays that var in my right prompt. I'm pretty happy with the behavior. Especially that it captures the cost to source ~/.elvish/rc.elv before the first prompt is rendered. Which, on my primary server, is on the order of 400 ms. It also ignores any code block that consists entirely of whitespace. This means that simply pressing Enter does not modify the value of that variable (assuming only whitespace characters were entered before pressing Enter).
I am also working on a change to introduce a edit:after-command hook. It is still unclear if the value of that hook justifies the cost(s) but it does open other possibilities such as capturing any error from executing a code block.
Activity
krader1961 commentedon May 16, 2020
I would like to see something like a
$edit:cmd-durationvariable introduced. This should not be in thebuiltinnamespace because we don't want, or need, to measure the elapsed duration of every statement by default. This is an interactive REPL feature primarily meant to be used in the interactive prompts or related command hooks.krader1961 commentedon May 16, 2020
Also, note that the solution used by https://github.com/zzamboni/elvish-modules/blob/master/long-running-notifications.elv has a resolution of seconds. The proposed
$edit:cmd-durationvariable should have millisecond, or better, resolution. And the @zzamboni implementation requires running the externaldatecommand. Which shouldn't be necessary. But that's a separate issue.xiaq commentedon May 17, 2020
I'd rather we introduce an
after-cmdhook and supply this information as part of the hook, which localizes the information instead of making it a global variable.xiaq commentedon May 17, 2020
Before #1030 is resolved, I am open to an experimental API that exposes this information as a global, perhaps named
edit:-last-cmd-duration-sec.krader1961 commentedon May 18, 2020
An
after-cmdhook might be a good idea but creates some questions. In particular, it would be an expensive mechanism to solely capture the execution duration of the most recent interactive command.I agree that assigning a
float64of the elapsed fractional seconds to aedit:-cmd-duration-secvar (I don't see the point of includinglast-in that var name) is better than a nominal integer scaled by milliseconds or microseconds.FWIW, On my primary macOS server using the @zzamboni approach, but using
date '%s.%N'for nanosecond resolution, consistently results in a command duration of 6 to 8 ms when simply pressing [enter]; i.e., not running any command at all. Which shows that using the existing hooks is unexpectedly expensive.zzamboni commentedon May 18, 2020
I agree that it might be better for precision that the shell itself measures and exposes how much a command took. Display/reporting can then of course be done by hooks.
Though honestly, I don't think this is a must - I can't really think of a good use case (other than the kind of thing
long-running-notificationsdoes) for having this measurements. So I would consider it as a low priority.Finally, a question based on the discussion above: isn't the existing
before-readlinehook effectively the same as the proposedafter-cmdhook? What am I missing?krader1961 commentedon May 18, 2020
No, since the
after-cmdhook would only fire when a command was actually run. Simply displaying a new prompt, such as from pressing [enter] on an empty command line, wouldn't trigger it. Also, it wouldn't be tied to interactive prompts, making it usable from a script. Which means that aafter-cmdhook would eliminate the need for thetimecommand.What arguments would be passed to a
after-cmdhook? Obviously the statement that was run and the elapsed time in seconds. A third argument could be a list of exceptions thrown by the statement or an empty list if there were no exceptions. A fourth argument could be thegetrusagedata for any external commands. A fifth argument could be thegetrusagedelta for the shell itself.krader1961 commentedon Mar 16, 2021
I started working on a resolution for this issue and realized that simply pressing Enter should not affect the recorded duration of the most recent command since no command was executed. Also, introducing an
after-cmdhook might be useful but is not needed to resolve this issue and needlessly complicates capturing the duration of an interactive statement. The resolution I'm working on introduces aedit:command:durationfloat64 variable.krader1961 commentedon Mar 18, 2021
I've been using a change that introduces the
$edit:command:-durationvariable for a few days. Along with a related change to my ~/.elvish/rc.elv that displays that var in my right prompt. I'm pretty happy with the behavior. Especially that it captures the cost to source ~/.elvish/rc.elv before the first prompt is rendered. Which, on my primary server, is on the order of 400 ms. It also ignores any code block that consists entirely of whitespace. This means that simply pressing Enter does not modify the value of that variable (assuming only whitespace characters were entered before pressing Enter).I am also working on a change to introduce a
edit:after-commandhook. It is still unclear if the value of that hook justifies the cost(s) but it does open other possibilities such as capturing any error from executing a code block.Add $edit:after-command and $edit:command-duration