Benchmark each pipeline element (#7854)

# Description

Adds a `profile` command that profiles each pipeline element of a block
and can also recursively step into child blocks.

# Limitations
* It is implemented using pipeline metadata which currently get lost in
some circumstances (e.g.,
https://github.com/nushell/nushell/issues/4501). This means that the
profiler will lose data coming from subexpressions. This issue will
hopefully be solved in the future.
* It also does not step into individual loop iteration which I'm not
sure why but maybe that's a good thing.

# User-Facing Changes

Shouldn't change any existing behavior.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Jakub Žádník
2023-02-11 23:35:48 +02:00
committed by GitHub
parent 64b6c02a22
commit 58529aa0b2
12 changed files with 328 additions and 25 deletions

View File

@ -7,6 +7,44 @@ use crate::{ShellError, Span, Value, VarId};
/// Environment variables per overlay
pub type EnvVars = HashMap<String, HashMap<String, Value>>;
#[derive(Debug, Clone)]
pub struct ProfilingConfig {
pub max_depth: i64,
pub depth: i64,
pub collect_source: bool,
pub collect_values: bool,
}
impl ProfilingConfig {
pub fn new(max_depth: i64, collect_source: bool, collect_values: bool) -> Self {
ProfilingConfig {
max_depth,
depth: 0,
collect_source,
collect_values,
}
}
pub fn enter_block(&mut self) {
self.depth += 1;
}
pub fn leave_block(&mut self) {
self.depth -= 1;
}
pub fn should_debug(&self) -> bool {
self.depth <= self.max_depth
}
pub fn reset(&mut self) {
self.max_depth = 0;
self.depth = 0;
self.collect_source = false;
self.collect_values = false;
}
}
/// A runtime value stack used during evaluation
///
/// A note on implementation:
@ -35,6 +73,7 @@ pub struct Stack {
/// List of active overlays
pub active_overlays: Vec<String>,
pub recursion_count: Box<u64>,
pub profiling_config: ProfilingConfig,
}
impl Stack {
@ -45,6 +84,7 @@ impl Stack {
env_hidden: HashMap::new(),
active_overlays: vec![DEFAULT_OVERLAY_NAME.to_string()],
recursion_count: Box::new(0),
profiling_config: ProfilingConfig::new(0, false, false),
}
}
@ -126,6 +166,7 @@ impl Stack {
env_hidden: HashMap::new(),
active_overlays: self.active_overlays.clone(),
recursion_count: self.recursion_count.to_owned(),
profiling_config: self.profiling_config.clone(),
}
}
@ -151,6 +192,7 @@ impl Stack {
env_hidden: HashMap::new(),
active_overlays: self.active_overlays.clone(),
recursion_count: self.recursion_count.to_owned(),
profiling_config: self.profiling_config.clone(),
}
}