Add system, user and idle times to benchmark command (#2571)

* Add system, user and idle times to benchmark command

* Feature-gate dependency on heim in benchmark

* Reorder let bindings in benchmark

* Fully feature-gate rich-benchmark and print 0sec on zero duration
This commit is contained in:
Radek Vít 2020-09-19 19:13:14 +02:00 committed by GitHub
parent 2b13ac3856
commit 422b6ca871
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 15 deletions

2
Cargo.lock generated
View File

@ -2916,6 +2916,7 @@ dependencies = [
"getset",
"git2",
"glob",
"heim",
"htmlescape",
"ical",
"ichwh",
@ -2969,6 +2970,7 @@ dependencies = [
"trash",
"umask",
"unicode-segmentation",
"uom 0.28.0",
"url 2.1.1",
"users",
"uuid 0.8.1",

View File

@ -79,6 +79,7 @@ default = [
"match",
"post",
"fetch",
"rich-benchmark",
]
extra = ["default", "binaryview", "tree", "clipboard-cli", "trash-support", "start", "bson", "sqlite", "s3"]
stable = ["default"]
@ -106,6 +107,7 @@ ctrlc-support = ["nu-cli/ctrlc"]
directories-support = ["nu-cli/directories", "nu-cli/dirs", "nu-data/directories", "nu-data/dirs"]
git-support = ["nu-cli/git2"]
ptree-support = ["nu-cli/ptree"]
rich-benchmark = ["nu-cli/rich-benchmark"]
rustyline-support = ["nu-cli/rustyline-support"]
term-support = ["nu-cli/term"]
trash-support = ["nu-cli/trash-support"]

View File

@ -47,6 +47,7 @@ futures_codec = "0.4.1"
getset = "0.1.1"
git2 = {version = "0.13.11", default_features = false, optional = true}
glob = "0.3.0"
heim = {version = "0.1.0-beta.3", optional = true }
htmlescape = "0.3.1"
ical = "0.6.0"
ichwh = {version = "0.3.4", optional = true}
@ -84,6 +85,7 @@ term_size = "0.3.2"
termcolor = "1.1.0"
toml = "0.5.6"
unicode-segmentation = "1.6.0"
uom = {version = "0.28.0", features = ["f64", "try-from"]}
uuid_crate = {package = "uuid", version = "0.8.1", features = ["v4"], optional = true}
which = {version = "4.0.2", optional = true}
zip = {version = "0.5.7", optional = true}
@ -123,3 +125,4 @@ clipboard-cli = ["clipboard"]
rustyline-support = ["rustyline"]
stable = []
trash-support = ["trash"]
rich-benchmark = ["heim"]

View File

@ -1,12 +1,12 @@
use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
#[cfg(feature = "rich-benchmark")]
use heim::cpu::time;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use chrono::prelude::*;
use nu_protocol::{hir::Block, Dictionary, Signature, SyntaxShape, UntaggedValue, Value};
use std::convert::TryInto;
use std::time::{Duration, Instant};
pub struct Benchmark;
@ -50,17 +50,21 @@ impl WholeStreamCommand for Benchmark {
}
}
#[cfg(feature = "rich-benchmark")]
async fn benchmark(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let tag = raw_args.call_info.args.span;
let mut context = Context::from_raw(&raw_args, &registry);
let scope = raw_args.call_info.scope.clone();
let (BenchmarkArgs { block }, input) = raw_args.process(&registry).await?;
let start_time: chrono::DateTime<_> = Utc::now();
let start_time = Instant::now();
let start = time().await;
let result = run_block(
&block,
@ -71,16 +75,80 @@ async fn benchmark(
&scope.env,
)
.await;
let _ = result?.drain_vec().await;
let run_duration: chrono::Duration = Utc::now().signed_duration_since(start_time);
let end = time().await;
let end_time = Instant::now();
context.clear_errors();
let output = Ok(ReturnSuccess::Value(Value {
value: UntaggedValue::Primitive(Primitive::from(run_duration)),
tag: Tag::from(block.span),
}));
if let (Ok(start), Ok(end)) = (start, end) {
let mut indexmap = IndexMap::with_capacity(4);
Ok(OutputStream::from(vec![output]))
let real_time = into_value(end_time - start_time, &tag);
indexmap.insert("real time".to_string(), real_time);
let user_time = into_value(end.user() - start.user(), &tag);
indexmap.insert("user time".to_string(), user_time);
let system_time = into_value(end.system() - start.system(), &tag);
indexmap.insert("system time".to_string(), system_time);
let idle_time = into_value(end.idle() - start.idle(), &tag);
indexmap.insert("idle time".to_string(), idle_time);
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
Ok(OutputStream::one(value))
} else {
Err(ShellError::untagged_runtime_error(
"Could not retreive CPU time",
))
}
}
#[cfg(not(feature = "rich-benchmark"))]
async fn benchmark(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let tag = raw_args.call_info.args.span;
let mut context = Context::from_raw(&raw_args, &registry);
let scope = raw_args.call_info.scope.clone();
let (BenchmarkArgs { block }, input) = raw_args.process(&registry).await?;
let start_time = Instant::now();
let result = run_block(
&block,
&mut context,
input,
&scope.it,
&scope.vars,
&scope.env,
)
.await;
let _ = result?.drain_vec().await;
let end_time = Instant::now();
context.clear_errors();
let mut indexmap = IndexMap::with_capacity(4);
let real_time = into_value(end_time - start_time, &tag);
indexmap.insert("real time".to_string(), real_time);
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
Ok(OutputStream::one(value))
}
fn into_value<T: TryInto<Duration>>(time: T, tag: &Span) -> Value {
UntaggedValue::duration(
time.try_into()
.unwrap_or_else(|_| Duration::new(0, 0))
.as_nanos()
.into(),
)
.into_value(tag)
}

View File

@ -297,6 +297,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
/// Format a duration in nanoseconds into a string
pub fn format_duration(duration: &BigInt) -> String {
let is_zero = duration.is_zero();
// FIXME: This involves a lot of allocation, but it seems inevitable with BigInt.
let big_int_1000 = BigInt::from(1000);
let big_int_60 = BigInt::from(60);
@ -327,8 +328,8 @@ pub fn format_duration(duration: &BigInt) -> String {
if !mins.is_zero() {
output_prep.push(format!("{}min", mins));
}
if !secs.is_zero() {
// output 0sec for zero duration
if is_zero || !secs.is_zero() {
output_prep.push(format!("{}sec", secs));
}