2020-07-19 19:39:43 +02:00
|
|
|
use crate::commands::classified::block::run_block;
|
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
2020-09-19 19:13:14 +02:00
|
|
|
#[cfg(feature = "rich-benchmark")]
|
|
|
|
use heim::cpu::time;
|
2020-07-19 19:39:43 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-09-21 19:30:16 +02:00
|
|
|
use nu_protocol::{
|
|
|
|
hir::{Block, ClassifiedCommand, Commands, InternalCommand},
|
|
|
|
Dictionary, Scope, Signature, SyntaxShape, UntaggedValue, Value,
|
|
|
|
};
|
2020-09-19 19:13:14 +02:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::time::{Duration, Instant};
|
2020-07-19 19:39:43 +02:00
|
|
|
|
|
|
|
pub struct Benchmark;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct BenchmarkArgs {
|
|
|
|
block: Block,
|
2020-09-21 19:30:16 +02:00
|
|
|
passthrough: Option<Block>,
|
2020-07-19 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl WholeStreamCommand for Benchmark {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"benchmark"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-09-21 19:30:16 +02:00
|
|
|
Signature::build("benchmark")
|
|
|
|
.required(
|
|
|
|
"block",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"the block to run and benchmark",
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"passthrough",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"Display the benchmark results and pass through the block's output",
|
|
|
|
Some('p'),
|
|
|
|
)
|
2020-07-19 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-09-18 08:13:53 +02:00
|
|
|
"Runs a block and returns the time it took to execute it"
|
2020-07-19 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
benchmark(args, registry).await
|
|
|
|
}
|
2020-09-18 08:13:53 +02:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
2020-09-21 19:30:16 +02:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Benchmarks a command within a block",
|
|
|
|
example: "benchmark { sleep 500ms }",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Benchmarks a command within a block and passes its output through",
|
|
|
|
example: "echo 45 | benchmark { sleep 500ms } --passthrough {}",
|
|
|
|
result: Some(vec![UntaggedValue::int(45).into()]),
|
|
|
|
},
|
|
|
|
]
|
2020-09-18 08:13:53 +02:00
|
|
|
}
|
2020-07-19 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn benchmark(
|
|
|
|
raw_args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
|
|
|
|
2020-09-19 19:13:14 +02:00
|
|
|
let tag = raw_args.call_info.args.span;
|
2020-09-19 23:29:51 +02:00
|
|
|
let mut context = EvaluationContext::from_raw(&raw_args, ®istry);
|
2020-07-19 19:39:43 +02:00
|
|
|
let scope = raw_args.call_info.scope.clone();
|
2020-09-21 19:30:16 +02:00
|
|
|
let (BenchmarkArgs { block, passthrough }, input) = raw_args.process(®istry).await?;
|
2020-07-19 19:39:43 +02:00
|
|
|
|
2020-09-19 19:13:14 +02:00
|
|
|
let start_time = Instant::now();
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
#[cfg(feature = "rich-benchmark")]
|
2020-09-19 19:13:14 +02:00
|
|
|
let start = time().await;
|
2020-07-19 19:39:43 +02:00
|
|
|
|
|
|
|
let result = run_block(
|
|
|
|
&block,
|
|
|
|
&mut context,
|
|
|
|
input,
|
|
|
|
&scope.it,
|
|
|
|
&scope.vars,
|
|
|
|
&scope.env,
|
|
|
|
)
|
|
|
|
.await;
|
2020-09-21 19:30:16 +02:00
|
|
|
let output = result?.into_vec().await;
|
2020-09-19 19:13:14 +02:00
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
#[cfg(feature = "rich-benchmark")]
|
2020-09-19 19:13:14 +02:00
|
|
|
let end = time().await;
|
|
|
|
|
|
|
|
let end_time = Instant::now();
|
|
|
|
context.clear_errors();
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
// return basic runtime
|
|
|
|
#[cfg(not(feature = "rich-benchmark"))]
|
|
|
|
{
|
|
|
|
let mut indexmap = IndexMap::with_capacity(1);
|
|
|
|
|
|
|
|
let real_time = into_big_int(end_time - start_time);
|
|
|
|
indexmap.insert("real time".to_string(), real_time);
|
|
|
|
benchmark_output(indexmap, output, passthrough, &tag, &mut context, &scope).await
|
|
|
|
}
|
|
|
|
// return advanced stats
|
|
|
|
#[cfg(feature = "rich-benchmark")]
|
2020-09-19 19:13:14 +02:00
|
|
|
if let (Ok(start), Ok(end)) = (start, end) {
|
|
|
|
let mut indexmap = IndexMap::with_capacity(4);
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
let real_time = into_big_int(end_time - start_time);
|
2020-09-19 19:13:14 +02:00
|
|
|
indexmap.insert("real time".to_string(), real_time);
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
let user_time = into_big_int(end.user() - start.user());
|
2020-09-19 19:13:14 +02:00
|
|
|
indexmap.insert("user time".to_string(), user_time);
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
let system_time = into_big_int(end.system() - start.system());
|
2020-09-19 19:13:14 +02:00
|
|
|
indexmap.insert("system time".to_string(), system_time);
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
let idle_time = into_big_int(end.idle() - start.idle());
|
2020-09-19 19:13:14 +02:00
|
|
|
indexmap.insert("idle time".to_string(), idle_time);
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
benchmark_output(indexmap, output, passthrough, &tag, &mut context, &scope).await
|
2020-09-19 19:13:14 +02:00
|
|
|
} else {
|
|
|
|
Err(ShellError::untagged_runtime_error(
|
|
|
|
"Could not retreive CPU time",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
async fn benchmark_output<T, Output>(
|
|
|
|
indexmap: IndexMap<String, BigInt>,
|
|
|
|
block_output: Output,
|
|
|
|
passthrough: Option<Block>,
|
|
|
|
tag: T,
|
|
|
|
context: &mut EvaluationContext,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Result<OutputStream, ShellError>
|
|
|
|
where
|
|
|
|
T: Into<Tag> + Copy,
|
|
|
|
Output: Into<OutputStream>,
|
|
|
|
{
|
|
|
|
let value = UntaggedValue::Row(Dictionary::from(
|
|
|
|
indexmap
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| (k, UntaggedValue::duration(v).into_value(tag)))
|
|
|
|
.collect::<IndexMap<String, Value>>(),
|
|
|
|
))
|
|
|
|
.into_value(tag);
|
|
|
|
|
|
|
|
if let Some(time_block) = passthrough {
|
|
|
|
let benchmark_output = InputStream::one(value);
|
|
|
|
|
|
|
|
// add autoview for an empty block
|
|
|
|
let time_block = add_implicit_autoview(time_block);
|
|
|
|
|
|
|
|
let _ = run_block(
|
|
|
|
&time_block,
|
|
|
|
context,
|
|
|
|
benchmark_output,
|
|
|
|
&scope.it,
|
|
|
|
&scope.vars,
|
|
|
|
&scope.env,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
context.clear_errors();
|
2020-08-03 00:34:33 +02:00
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
Ok(block_output.into())
|
|
|
|
} else {
|
|
|
|
let benchmark_output = OutputStream::one(value);
|
|
|
|
Ok(benchmark_output)
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 19:13:14 +02:00
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
fn add_implicit_autoview(mut block: Block) -> Block {
|
|
|
|
if block.block.is_empty() {
|
|
|
|
block.push({
|
|
|
|
let mut commands = Commands::new(block.span);
|
|
|
|
commands.push(ClassifiedCommand::Internal(InternalCommand::new(
|
|
|
|
"autoview".to_string(),
|
|
|
|
block.span,
|
|
|
|
block.span,
|
|
|
|
)));
|
|
|
|
commands
|
|
|
|
});
|
|
|
|
}
|
|
|
|
block
|
2020-09-19 19:13:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:30:16 +02:00
|
|
|
fn into_big_int<T: TryInto<Duration>>(time: T) -> BigInt {
|
|
|
|
time.try_into()
|
|
|
|
.unwrap_or_else(|_| Duration::new(0, 0))
|
|
|
|
.as_nanos()
|
|
|
|
.into()
|
2020-07-19 19:39:43 +02:00
|
|
|
}
|