diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-22 16:12:59 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-22 16:12:59 +0100 |
| commit | 25b9a8449d674b5842e80e10ff5d9dd272f7b309 (patch) | |
| tree | 47d012d5b739d7fa5a53e179152b062faa44b53c | |
| parent | chore: fix clippy issues (diff) | |
| download | sl0pstatus-25b9a8449d674b5842e80e10ff5d9dd272f7b309.tar.gz sl0pstatus-25b9a8449d674b5842e80e10ff5d9dd272f7b309.tar.xz sl0pstatus-25b9a8449d674b5842e80e10ff5d9dd272f7b309.zip | |
feat: add bench for command widget
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | benches/widgets.rs | 40 | ||||
| -rw-r--r-- | justfile | 4 | ||||
| -rw-r--r-- | src/widgets/command.rs | 3 |
4 files changed, 46 insertions, 4 deletions
@@ -4,6 +4,9 @@ version = "0.10.0" authors = ["Daniel Jankowski"] edition = "2018" +[features] +bench = [] + [dependencies] zellij-tile = "0.39.0" chrono = { version = "0.4.0", default-features = false } diff --git a/benches/widgets.rs b/benches/widgets.rs index e4b1245..b68ccac 100644 --- a/benches/widgets.rs +++ b/benches/widgets.rs @@ -1,5 +1,6 @@ +use chrono::{Duration, Local}; use criterion::{criterion_group, criterion_main, Criterion}; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, ops::Sub}; use zellij_tile::prelude::*; use zjstatus::{ @@ -7,6 +8,42 @@ use zjstatus::{ widgets::{self, widget::Widget}, }; +fn bench_widget_command(c: &mut Criterion) { + let config = BTreeMap::from([ + ( + "command_test".to_owned(), + "#[fg=#9399B2,bg=#181825,bold,italic] {exit_code} {stdout} ".to_owned(), + ), + ("command_interval".to_owned(), "100".to_owned()), + ]); + + let wid = widgets::command::CommandWidget::new(&config); + + let ts = Local::now().sub(Duration::seconds(1)); + + let state = ZellijState { + command_results: BTreeMap::from([( + "command_test".to_owned(), + widgets::command::CommandResult { + exit_code: Some(0), + stdout: "test".to_owned(), + stderr: "".to_owned(), + context: BTreeMap::from([( + "timestamp".to_owned(), + ts.format(widgets::command::TIMESTAMP_FORMAT).to_string(), + )]), + }, + )]), + ..Default::default() + }; + + c.bench_function("widgets::CommandWidget", |b| { + b.iter(|| { + wid.process("command_test", &state); + }) + }); +} + fn bench_widget_tabs(c: &mut Criterion) { let config = BTreeMap::from([ ( @@ -79,6 +116,7 @@ fn criterion_benchmark(c: &mut Criterion) { bench_widget_datetime(c); bench_widget_mod(c); bench_widget_tabs(c); + bench_widget_command(c); } criterion_group!(benches, criterion_benchmark); @@ -1,5 +1,5 @@ bench: - cargo wasi bench -- --color always | grep --color=never -v "Criterion.rs ERROR" + cargo wasi bench --features bench -- --color always | grep --color=never -v "Criterion.rs ERROR" build: cargo build @@ -8,7 +8,7 @@ test: cargo wasi test -- --nocapture lint: - cargo clippy --all-targets --all-features -- -D warnings + cargo clippy --all-targets -- -D warnings cargo audit release version: diff --git a/src/widgets/command.rs b/src/widgets/command.rs index 17c5e22..cd65105 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -14,7 +14,7 @@ use crate::render::FormattedPart; use crate::{config::ZellijState, widgets::widget::Widget}; -const TIMESTAMP_FORMAT: &str = "%s"; +pub const TIMESTAMP_FORMAT: &str = "%s"; lazy_static! { static ref COMMAND_REGEX: Regex = Regex::new("_[a-zA-Z0-9]+$").unwrap(); @@ -115,6 +115,7 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell let command = commandline_parser(&command_config.command); let command = command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(); + #[cfg(not(feature = "bench"))] run_command(&command, context); } } |
