From b5ae024cc8369355309e8e85a3efec17e1e3a8d3 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Sat, 20 Feb 2021 07:37:14 -0600 Subject: [PATCH] add the ability to time commands (#3081) --- crates/nu-cli/src/cli.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 381a00a1f..6254a923c 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -31,6 +31,7 @@ use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::{hir::ExternalRedirection, UntaggedValue, Value}; +use log::trace; use std::error::Error; use std::iter::Iterator; use std::path::PathBuf; @@ -116,7 +117,19 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { rl.set_helper(helper); }); + // start time for command duration + let startup_commands_start_time = std::time::Instant::now(); + // run the startup commands let _ = run_startup_commands(&mut context, &configuration).await; + // Store cmd duration in an env var + context.scope.add_env_var( + "CMD_DURATION", + format!("{:?}", startup_commands_start_time.elapsed()), + ); + trace!( + "startup commands took {:?}", + startup_commands_start_time.elapsed() + ); // Give ourselves a scope to work in context.scope.enter_scope(); @@ -227,6 +240,9 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { session_text.push('\n'); } + // start time for command duration + let cmd_start_time = std::time::Instant::now(); + let line = match convert_rustyline_result_to_string(readline) { LineResult::Success(_) => { process_script( @@ -241,6 +257,11 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { x => x, }; + // Store cmd duration in an env var + context + .scope + .add_env_var("CMD_DURATION", format!("{:?}", cmd_start_time.elapsed())); + // Check the config to see if we need to update the path // TODO: make sure config is cached so we don't path this load every call // FIXME: we probably want to be a bit more graceful if we can't set the environment