bare-script 0.1.1

The type-safe scripting authority for Rust. A framework for building robust shell commands and automation with 'Parse, don't validate' philosophy.
Documentation
//! Benchmarks for bare-script.
//!
//! Run with: cargo bench
//!
//! These benchmarks measure the overhead of command execution.
//! Note: These benchmarks spawn actual system commands, so times
//! include process spawn overhead (~5-10ms on most systems).

#![allow(missing_docs)]

use bare_script::sync::CommandBuilder;
use thiserror as _;
use tokio as _;

#[cfg(feature = "tokio-rt")]
use bare_script::proc::CommandBuilder as AsyncCommandBuilder;

criterion::criterion_group!(
    benches,
    bench_command_execute,
    bench_command_with_args,
    bench_command_with_env,
);

criterion::criterion_group!(benches_async, bench_async_command_execute,);

fn bench_command_execute(c: &mut criterion::Criterion) {
    c.bench_function("command_execute_echo", |b| {
        #[cfg(windows)]
        b.iter(|| {
            let _ = CommandBuilder::new("cmd")
                .args(["/C", "echo test"])
                .capture_output()
                .execute();
        });

        #[cfg(not(windows))]
        b.iter(|| {
            let _ = CommandBuilder::new("echo")
                .arg("test")
                .capture_output()
                .execute();
        });
    });
}

fn bench_command_with_args(c: &mut criterion::Criterion) {
    c.bench_function("command_with_args", |b| {
        #[cfg(windows)]
        b.iter(|| {
            let _ = CommandBuilder::new("cmd")
                .args(["/C", "echo"])
                .arg("hello")
                .arg("world")
                .capture_output()
                .execute();
        });

        #[cfg(not(windows))]
        b.iter(|| {
            let _ = CommandBuilder::new("echo")
                .arg("hello")
                .arg("world")
                .capture_output()
                .execute();
        });
    });
}

fn bench_command_with_env(c: &mut criterion::Criterion) {
    c.bench_function("command_with_env", |b| {
        #[cfg(windows)]
        b.iter(|| {
            let _ = CommandBuilder::new("cmd")
                .args(["/C", "echo %TEST_VAR%"])
                .env("TEST_VAR", "value")
                .capture_output()
                .execute();
        });

        #[cfg(not(windows))]
        b.iter(|| {
            let _ = CommandBuilder::new("sh")
                .args(["-c", "echo $TEST_VAR"])
                .env("TEST_VAR", "value")
                .capture_output()
                .execute();
        });
    });
}

#[cfg(feature = "tokio-rt")]
fn bench_async_command_execute(c: &mut criterion::Criterion) {
    use tokio::runtime::Runtime;

    let rt = Runtime::new().unwrap();

    c.bench_function("async_command_execute_echo", |b| {
        #[cfg(windows)]
        b.to_async(&rt).iter(|| async {
            let _ = AsyncCommandBuilder::new("cmd")
                .args(["/C", "echo test"])
                .capture_output()
                .execute()
                .await;
        });

        #[cfg(not(windows))]
        b.to_async(&rt).iter(|| async {
            let _ = AsyncCommandBuilder::new("echo")
                .arg("test")
                .capture_output()
                .execute()
                .await;
        });
    });
}

#[cfg(not(feature = "tokio-rt"))]
criterion::criterion_main!(benches);

#[cfg(feature = "tokio-rt")]
criterion::criterion_main!(benches, benches_async);