1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
//! Contains macros which together define a benchmark harness that can be used in place of the
//! standard benchmark harness. This allows the user to run Iai benchmarks with `cargo bench`.
/// The `iai_callgrind::main` macro expands to a `main` function which runs all of the benchmarks.
///
/// Using Iai-callgrind requires disabling the benchmark harness. This can be done like so in the
/// `Cargo.toml` file:
///
/// ```toml
/// [[bench]]
/// name = "my_bench"
/// harness = false
/// ```
///
/// To be able to run any iai-callgrind benchmarks, you'll also need the `iai-callgrind-runner`
/// installed with the binary somewhere in your `$PATH` for example with
///
/// ```shell
/// cargo install iai-callgrind-runner
/// ```
///
/// `my_bench` has to be a rust file inside the 'benches' directory.
///
/// # Library Benchmarks
///
/// The [`crate::main`] macro has one form to run library benchmarks:
///
/// ```rust
/// # use iai_callgrind::{main, library_benchmark_group, library_benchmark};
/// # #[library_benchmark]
/// # fn bench_fibonacci() { }
/// # library_benchmark_group!(
/// # name = some_group;
/// # benchmarks = bench_fibonacci
/// # );
/// # fn main() {
/// main!(library_benchmark_groups = some_group);
/// # }
/// ```
///
/// which accepts the following top-level arguments:
///
/// * __`library_benchmark_groups`__ (mandatory): The `name` of one or
/// more [`library_benchmark_group!`](crate::library_benchmark_group) macros.
/// * __`config`__ (optional): Optionally specify a [`crate::LibraryBenchmarkConfig`]
/// valid for all benchmark groups
///
/// A library benchmark consists of
/// [`library_benchmark_groups`](crate::library_benchmark_group) and with
/// [`#[library_benchmark]`](crate::library_benchmark) annotated benchmark functions.
///
/// ```rust
/// use iai_callgrind::{black_box, main, library_benchmark_group, library_benchmark};
///
/// fn fibonacci(n: u64) -> u64 {
/// match n {
/// 0 => 1,
/// 1 => 1,
/// n => fibonacci(n - 1) + fibonacci(n - 2),
/// }
/// }
///
/// #[library_benchmark]
/// #[bench::short(10)]
/// #[bench::long(30)]
/// fn bench_fibonacci(value: u64) -> u64 {
/// black_box(fibonacci(value))
/// }
///
/// library_benchmark_group!(
/// name = bench_fibonacci_group;
/// benchmarks = bench_fibonacci
/// );
///
/// # fn main() {
/// main!(library_benchmark_groups = bench_fibonacci_group);
/// # }
/// ```
///
/// If you need to pass arguments to valgrind's callgrind, you can specify raw callgrind
/// arguments via the [`crate::LibraryBenchmarkConfig`]:
///
/// ```rust
/// # use iai_callgrind::{main, library_benchmark_group, library_benchmark, LibraryBenchmarkConfig};
/// # #[library_benchmark]
/// # fn bench_fibonacci() { }
/// # library_benchmark_group!(
/// # name = some_group;
/// # benchmarks = bench_fibonacci
/// # );
/// # fn main() {
/// main!(
/// config = LibraryBenchmarkConfig::default()
/// .raw_callgrind_args(
/// ["--arg-with-flags=yes", "arg-without-flags=is_ok_too"]
/// );
/// library_benchmark_groups = some_group
/// );
/// # }
/// ```
///
/// See also [Callgrind Command-line
/// options](https://valgrind.org/docs/manual/cl-manual.html#cl-manual.options).
///
/// For an in-depth description of library benchmarks and more examples see the
/// [README#Library
/// Benchmarks](https://github.com/iai-callgrind/iai-callgrind#library-benchmarks) of this
/// crate.
///
/// # Binary Benchmarks
///
/// The scheme to setup binary benchmarks makes use of [`crate::binary_benchmark_group`]
/// and [`crate::BinaryBenchmarkGroup`] to set up benches with [`crate::Run`] roughly
/// looking like this:
///
/// ```rust
/// use iai_callgrind::{main, binary_benchmark_group, Run, Arg};
///
/// binary_benchmark_group!(
/// name = my_group;
/// benchmark = |"my-exe", group: &mut BinaryBenchmarkGroup| {
/// group
/// .bench(Run::with_arg(Arg::new(
/// "positional arguments",
/// ["foo", "foo bar"],
/// )))
/// .bench(Run::with_arg(Arg::empty("no argument")));
/// }
/// );
///
/// # fn main() {
/// main!(binary_benchmark_groups = my_group);
/// # }
/// ```
///
/// See the documentation of [`crate::binary_benchmark_group`] and [`crate::Run`] for more
/// details.
#[macro_export]
macro_rules! main {
( $( options = $( $options:literal ),+ $(,)*; )?
$( before = $before:ident $(, bench = $bench_before:literal )? ; )?
$( after = $after:ident $(, bench = $bench_after:literal )? ; )?
$( setup = $setup:ident $(, bench = $bench_setup:literal )? ; )?
$( teardown = $teardown:ident $(, bench = $bench_teardown:literal )? ; )?
$( sandbox = $sandbox:literal; )?
$( fixtures = $fixtures:literal $(, follow_symlinks = $follow_symlinks:literal )? ; )?
$( run = cmd = $cmd:expr
$(, envs = [ $( $envs:literal ),* $(,)* ] )?,
$( id = $id:literal, args = [ $( $args:literal ),* $(,)* ] ),+ $(,)*
);+ $(;)*
) => {
compile_error!(
"You are using a deprecated syntax of the main! macro to set up binary benchmarks. \
See the README (https://github.com/iai-callgrind/iai-callgrind) and \
docs (https://docs.rs/iai-callgrind/latest/iai_callgrind/) for further details."
);
pub fn main() {}
};
(
$( config = $config:expr; $(;)* )?
binary_benchmark_groups =
) => {
compile_error!("The binary_benchmark_groups argument needs at least one `name` of a `binary_benchmark_group!`");
};
(
$( config = $config:expr; $(;)* )?
binary_benchmark_groups = $( $group:ident ),+ $(,)*
) => {
#[inline(never)]
fn run() {
let mut this_args = std::env::args();
let exe = option_env!("IAI_CALLGRIND_RUNNER")
.unwrap_or_else(|| option_env!("CARGO_BIN_EXE_iai-callgrind-runner").unwrap_or("iai-callgrind-runner"));
let library_version = "0.7.3";
let mut cmd = std::process::Command::new(exe);
cmd.arg(library_version);
cmd.arg("--bin-bench");
cmd.arg(env!("CARGO_MANIFEST_DIR"));
cmd.arg(file!());
cmd.arg(module_path!());
cmd.arg(this_args.next().unwrap()); // The executable benchmark binary
let mut benchmark = $crate::internal::InternalBinaryBenchmark::default();
$(
let mut group = $crate::BinaryBenchmarkGroup::from(
$crate::internal::InternalBinaryBenchmarkGroup {
id: Some(stringify!($group).to_owned()),
cmd: None,
config: $group::get_config(),
benches: Vec::default(),
assists: Vec::default(),
}
);
let (prog, assists) = $group::$group(&mut group);
let mut group: $crate::internal::InternalBinaryBenchmarkGroup = group.into();
group.cmd = prog;
group.assists = assists;
benchmark.groups.push(group);
)+
let mut config: Option<$crate::internal::InternalBinaryBenchmarkConfig> = None;
$(
config = Some($config.into());
)?
benchmark.config = if let Some(mut config) = config {
config.raw_callgrind_args.extend(this_args);
config
} else {
$crate::internal::InternalBinaryBenchmarkConfig {
raw_callgrind_args:
$crate::internal::InternalRawCallgrindArgs::from_iter(this_args),
..Default::default()
}
};
let encoded = $crate::bincode::serialize(&benchmark).expect("Encoded benchmark");
let mut child = cmd
.arg(encoded.len().to_string())
.stdin(std::process::Stdio::piped())
.spawn()
.expect("Failed to run benchmarks. \
Is iai-callgrind-runner installed and iai-callgrind-runner in your $PATH?. \
You can also set the environment variable IAI_CALLGRIND_RUNNER to the \
absolute path of the iai-callgrind-runner executable.");
let mut stdin = child.stdin.take().expect("Opening stdin to submit encoded benchmark");
std::thread::spawn(move || {
use std::io::Write;
stdin.write_all(&encoded).expect("Writing encoded benchmark to stdin");
});
let status = child.wait().expect("Wait for child process to exit");
if !status.success() {
std::process::exit(1);
}
}
fn main() {
let mut args_iter = $crate::black_box(std::env::args()).skip(1);
if args_iter
.next()
.as_ref()
.map_or(false, |value| value == "--iai-run")
{
match $crate::black_box(args_iter.next().expect("Expecting a function type")).as_str() {
$(
concat!(stringify!($group), "::", "before") => $group::before(),
concat!(stringify!($group), "::", "after") => $group::after(),
concat!(stringify!($group), "::", "setup") => $group::setup(),
concat!(stringify!($group), "::", "teardown") => $group::teardown(),
)+
name => panic!("function '{}' not found in this scope", name)
}
} else {
$crate::black_box(run());
};
}
};
(
$( config = $config:expr; $(;)* )?
library_benchmark_groups =
) => {
compile_error!("The library_benchmark_groups argument needs at least one `name` of a `library_benchmark_group!`");
};
(
$( config = $config:expr ; $(;)* )?
library_benchmark_groups = $( $group:ident ),+ $(,)*
) => {
#[inline(never)]
fn run() {
let mut this_args = std::env::args();
let exe = option_env!("IAI_CALLGRIND_RUNNER")
.unwrap_or_else(|| option_env!("CARGO_BIN_EXE_iai-callgrind-runner").unwrap_or("iai-callgrind-runner"));
let library_version = "0.7.3";
let mut cmd = std::process::Command::new(exe);
cmd.arg(library_version);
cmd.arg("--lib-bench");
cmd.arg(env!("CARGO_MANIFEST_DIR"));
cmd.arg(file!());
cmd.arg(module_path!());
cmd.arg(this_args.next().unwrap()); // The executable benchmark binary
let mut config: Option<$crate::internal::InternalLibraryBenchmarkConfig> = None;
$(
config = Some($config.into());
)?
let mut benchmark = $crate::internal::InternalLibraryBenchmark {
config: config.unwrap_or_default(),
groups: vec![],
command_line_args: this_args.collect()
};
$(
let mut group = $crate::internal::InternalLibraryBenchmarkGroup {
id: Some(stringify!($group).to_owned()),
config: $group::get_config(),
benches: vec![]
};
for (bench_name, get_config, macro_lib_benches) in $group::BENCHES {
let mut benches = $crate::internal::InternalLibraryBenchmarkBenches {
benches: vec![],
config: get_config()
};
for macro_lib_bench in macro_lib_benches.iter() {
let bench = $crate::internal::InternalLibraryBenchmarkBench {
id: macro_lib_bench.id_display.map(|i| i.to_string()),
args: macro_lib_bench.args_display.map(|i| i.to_string()),
bench: bench_name.to_string(),
config: macro_lib_bench.config.map(|f| f()),
};
benches.benches.push(bench);
}
group.benches.push(benches);
}
benchmark.groups.push(group);
)+
let encoded = $crate::bincode::serialize(&benchmark).expect("Encoded benchmark");
let mut child = cmd
.arg(encoded.len().to_string())
.stdin(std::process::Stdio::piped())
.spawn()
.expect("Failed to run benchmarks. \
Is iai-callgrind-runner installed and iai-callgrind-runner in your $PATH?. \
You can also set the environment variable IAI_CALLGRIND_RUNNER to the \
absolute path of the iai-callgrind-runner executable.");
let mut stdin = child.stdin.take().expect("Opening stdin to submit encoded benchmark");
std::thread::spawn(move || {
use std::io::Write;
stdin.write_all(&encoded).expect("Writing encoded benchmark to stdin");
});
let status = child.wait().expect("Wait for child process to exit");
if !status.success() {
std::process::exit(1);
}
}
fn main() {
let mut args_iter = $crate::black_box(std::env::args()).skip(1);
if args_iter
.next()
.as_ref()
.map_or(false, |value| value == "--iai-run")
{
match $crate::black_box(args_iter.next().expect("Expecting a function type")).as_str() {
$(
stringify!($group) => {
let group_index = $crate::black_box(
args_iter
.next()
.expect("Expecting a group index")
.parse::<usize>()
.expect("Expecting a valid group index")
);
let bench_index = $crate::black_box(
args_iter
.next()
.expect("Expecting a bench index")
.parse::<usize>()
.expect("Expecting a valid bench index")
);
$group::run(group_index, bench_index);
}
)+
name => panic!("function '{}' not found in this scope", name)
}
} else {
$crate::black_box(run());
};
}
};
(
callgrind_args = $( $args:literal ),* $(,)*; $(;)*
functions = $( $func_name:ident ),+ $(,)*
) => {
compile_error!(
"You are using a deprecated syntax of the main! macro to set up library benchmarks. \
See the README (https://github.com/iai-callgrind/iai-callgrind) and \
docs (https://docs.rs/iai-callgrind/latest/iai_callgrind/) for further details."
);
pub fn main() {}
};
( $( $func_name:ident ),+ $(,)* ) => {
compile_error!(
"You are using a deprecated syntax of the main! macro to set up library benchmarks. \
See the README (https://github.com/iai-callgrind/iai-callgrind) and \
docs (https://docs.rs/iai-callgrind/latest/iai_callgrind/) for further details."
);
pub fn main() {}
};
}
/// Macro used to define a group of binary benchmarks
///
/// A small introductory example which shows the basic setup:
///
/// ```rust
/// use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup};
///
/// binary_benchmark_group!(
/// name = my_group;
/// benchmark = |group: &mut BinaryBenchmarkGroup| {
/// // code to setup and configure the benchmarks in a group
/// }
/// );
///
/// iai_callgrind::main!(binary_benchmark_groups = my_group);
/// ```
///
/// To be benchmarked a `binary_benchmark_group` has to be added to the `main!` macro by adding its
/// name to the `binary_benchmark_groups` argument of the `main!` macro. See there for further
/// details about the [`crate::main`] macro.
///
/// This macro accepts two forms which slightly differ in the `benchmark` argument. In general, each
/// group shares the same `before`, `after`, `setup` and `teardown` functions, [`crate::Fixtures`]
/// and [`crate::BinaryBenchmarkConfig`].
///
/// The following top-level arguments are accepted:
///
/// ```rust
/// # use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup, BinaryBenchmarkConfig};
/// # fn run_before() {}
/// # fn run_after() {}
/// # fn run_setup() {}
/// # fn run_teardown() {}
/// binary_benchmark_group!(
/// name = my_group;
/// before = run_before;
/// after = run_after;
/// setup = run_setup;
/// teardown = run_teardown;
/// config = BinaryBenchmarkConfig::default();
/// benchmark = |"my-exe", group: &mut BinaryBenchmarkGroup| {
/// // code to setup and configure the benchmarks in a group
/// }
/// );
/// # fn main() {
/// # my_group::my_group(&mut BinaryBenchmarkGroup::default());
/// # }
/// ```
///
/// * __name__ (mandatory): A unique name used to identify the group for the `main!` macro
/// * __before__ (optional): A function which is run before all benchmarks
/// * __after__ (optional): A function which is run after all benchmarks
/// * __setup__ (optional): A function which is run before any benchmarks
/// * __teardown__ (optional): A function which is run before any benchmarks
/// * __config__ (optional): A [`crate::BinaryBenchmarkConfig`]
///
/// The `before`, `after`, `setup` and `teardown` arguments accept an additional argument `bench =
/// bool`
///
/// ```rust
/// # use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup};
/// # fn run_before() {}
/// # binary_benchmark_group!(
/// # name = my_group;
/// before = run_before, bench = true;
/// # benchmark = |"my-exe", group: &mut BinaryBenchmarkGroup| {
/// # }
/// # );
/// # fn main() {
/// # my_group::my_group(&mut BinaryBenchmarkGroup::default());
/// # }
/// ```
///
/// which enables benchmarking of the respective function if wished so. Note that setup and teardown
/// functions are benchmarked only once the first time they are invoked, much like the before and
/// after functions. However, both functions are run as usual before or after any benchmark.
///
/// Only the `benchmark` argument differs. In the first form
///
/// ```rust
/// # use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup, Run, Arg};
/// # binary_benchmark_group!(
/// # name = my_group;
/// benchmark = |"my-exe", group: &mut BinaryBenchmarkGroup| {
/// group.bench(Run::with_arg(Arg::new("some id", &["--foo=bar"])))
/// }
/// # );
/// # fn main() {
/// # my_group::my_group(&mut BinaryBenchmarkGroup::default());
/// # }
/// ```
///
/// it accepts a `command` which is the default for all [`crate::Run`] of the same benchmark group.
/// This `command` supports auto-discovery of a crate's binary. For example if a crate's binary is
/// named `my-exe` then it is sufficient to pass `"my-exe"` to the benchmark argument as shown
/// above.
///
/// In the second form:
///
/// ```rust
/// # use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup, Run, Arg};
/// # binary_benchmark_group!(
/// # name = my_group;
/// benchmark = |group: &mut BinaryBenchmarkGroup| {
/// // Usually, you should use `env!("CARGO_BIN_EXE_my-exe")` instead of an absolute path to a
/// // crate's binary
/// group.bench(Run::with_cmd(
/// "/path/to/my-exe",
/// Arg::new("some id", &["--foo=bar"]),
/// ))
/// }
/// # );
/// # fn main() {
/// # my_group::my_group(&mut BinaryBenchmarkGroup::default());
/// # }
/// ```
///
/// the command can be left out and each [`crate::Run`] of a benchmark group has to define a `cmd`
/// by itself. Note that [`crate::Run`] does not support auto-discovery of a crate's binary.
///
/// If you feel uncomfortable working within the macro you can simply move the code to setup the
/// group's benchmarks into a separate function like so
///
/// ```rust
/// use iai_callgrind::{binary_benchmark_group, BinaryBenchmarkGroup, Run, Arg};
///
/// fn setup_my_group(group: &mut BinaryBenchmarkGroup) {
/// group.bench(Run::with_arg(Arg::new("some id", &["--foo=bar"])));
/// }
///
/// binary_benchmark_group!(
/// name = my_group;
/// benchmark = |"my-exe", group: &mut BinaryBenchmarkGroup| setup_my_group(group)
/// );
/// # fn main() {
/// # my_group::my_group(&mut BinaryBenchmarkGroup::default());
/// # }
/// ```
#[macro_export]
macro_rules! binary_benchmark_group {
(
$( config = $config:expr ; $(;)* )?
benchmark = |$cmd:expr, $group:ident: &mut BinaryBenchmarkGroup| $body:expr
) => {
compile_error!("A binary_benchmark_group! needs a name\n\nbinary_benchmark_group!(name = some_ident; benchmark = ...);");
};
(
$( config = $config:expr ; $(;)* )?
benchmark = |$group:ident: &mut BinaryBenchmarkGroup| $body:expr
) => {
compile_error!("A binary_benchmark_group! needs a name\n\nbinary_benchmark_group!(name = some_ident; benchmark = ...);");
};
(
name = $name:ident;
$( config = $config:expr ; $(;)* )?
benchmark =
) => {
compile_error!(
r#"A binary_benchmark_group! needs an expression specifying `BinaryBenchmarkGroup`:
binary_benchmark_group!(name = some_ident; benchmark = |group: &mut BinaryBenchmarkGroup| ... );
OR
binary_benchmark_group!(name = some_ident; benchmark = |"my_exe", group: &mut BinaryBenchmarkGroup| ... );
"#);
};
(
name = $name:ident; $(;)*
$(before = $before:ident $(,bench = $bench_before:literal)? ; $(;)*)?
$(after = $after:ident $(,bench = $bench_after:literal)? ; $(;)*)?
$(setup = $setup:ident $(,bench = $bench_setup:literal)? ; $(;)*)?
$(teardown = $teardown:ident $(,bench = $bench_teardown:literal)? ; $(;)*)?
$( config = $config:expr ; $(;)* )?
benchmark = |$cmd:expr, $group:ident: &mut BinaryBenchmarkGroup| $body:expr
) => {
pub mod $name {
#[inline(never)]
pub fn before() {
$(
let _ = $crate::black_box(super::$before());
)?
}
#[inline(never)]
pub fn after() {
$(
let _ = $crate::black_box(super::$after());
)?
}
#[inline(never)]
pub fn setup() {
$(
let _ = $crate::black_box(super::$setup());
)?
}
#[inline(never)]
pub fn teardown() {
$(
let _ = $crate::black_box(super::$teardown());
)?
}
#[inline(never)]
pub fn get_config() -> Option<$crate::internal::InternalBinaryBenchmarkConfig> {
use super::*;
let mut config = None;
$(
config = Some($config.into());
)?
config
}
#[inline(never)]
pub fn $name($group: &mut $crate::BinaryBenchmarkGroup) ->
(Option<$crate::internal::InternalCmd>, Vec<$crate::internal::InternalAssistant>)
{
let cmd: &str = $cmd;
let cmd = (!cmd.is_empty()).then(|| $crate::internal::InternalCmd {
display: cmd.to_owned(),
cmd: option_env!(concat!("CARGO_BIN_EXE_", $cmd)).unwrap_or(cmd).to_owned()
}
);
let mut assists: Vec<$crate::internal::InternalAssistant> = vec![];
$(
let mut bench_before = false;
$(
bench_before = $bench_before;
)?
assists.push($crate::internal::InternalAssistant {
id: "before".to_owned(),
name: stringify!($before).to_owned(),
bench: bench_before
});
)?
$(
let mut bench_after = false;
$(
bench_after = $bench_after;
)?
assists.push($crate::internal::InternalAssistant {
id: "after".to_owned(),
name: stringify!($after).to_owned(),
bench: bench_after
});
)?
$(
let mut bench_setup = false;
$(
bench_setup = $bench_setup;
)?
assists.push($crate::internal::InternalAssistant {
id: "setup".to_owned(),
name: stringify!($setup).to_owned(),
bench: bench_setup
});
)?
$(
let mut bench_teardown = false;
$(
bench_teardown = $bench_teardown;
)?
assists.push($crate::internal::InternalAssistant {
id: "teardown".to_owned(),
name: stringify!($teardown).to_owned(),
bench: bench_teardown
});
)?
use super::*;
$body;
(cmd, assists)
}
}
};
(
name = $name:ident; $(;)*
$(before = $before:ident $(,bench = $bench_before:literal)? ; $(;)*)?
$(after = $after:ident $(,bench = $bench_after:literal)? ; $(;)*)?
$(setup = $setup:ident $(,bench = $bench_setup:literal)? ; $(;)*)?
$(teardown = $teardown:ident $(,bench = $bench_teardown:literal)? ; $(;)*)?
$( config = $config:expr ; $(;)* )?
benchmark = |$group:ident: &mut BinaryBenchmarkGroup| $body:expr
) => {
$crate::binary_benchmark_group!(
name = $name;
$(before = $before $(,bench = $bench_before)?;)?
$(after = $after $(,bench = $bench_after)?;)?
$(setup = $setup $(,bench = $bench_setup)?;)?
$(teardown = $teardown $(,bench = $bench_teardown)?;)?
$( config = $config; )?
benchmark = |"", $group: &mut BinaryBenchmarkGroup| $body
);
};
}
/// Macro used to define a group of library benchmarks
///
/// A small introductory example which shows the basic setup. This macro only accepts benchmarks
/// annotated with `#[library_benchmark]` ([`crate::library_benchmark`]).
///
/// ```rust
/// use iai_callgrind::{library_benchmark_group, library_benchmark};
///
/// #[library_benchmark]
/// fn bench_something() -> u64 {
/// 42
/// }
///
/// library_benchmark_group!(
/// name = my_group;
/// benchmarks = bench_something
/// );
///
/// # fn main() {
/// iai_callgrind::main!(library_benchmark_groups = my_group);
/// # }
/// ```
///
/// To be benchmarked a `library_benchmark_group` has to be added to the `main!` macro by adding its
/// name to the `library_benchmark_groups` argument of the `main!` macro. See there for further
/// details about the [`crate::main`] macro.
///
/// The following top-level arguments are accepted:
///
/// ```rust
/// # use iai_callgrind::{library_benchmark, library_benchmark_group, LibraryBenchmarkConfig};
/// # #[library_benchmark]
/// # fn some_func() {}
/// library_benchmark_group!(
/// name = my_group;
/// config = LibraryBenchmarkConfig::default();
/// benchmarks = some_func
/// );
/// # fn main() {
/// # }
/// ```
///
/// * __name__ (mandatory): A unique name used to identify the group for the `main!` macro
/// * __config__ (optional): A [`crate::LibraryBenchmarkConfig`] which is applied to all benchmarks
/// within
/// the same group.
#[macro_export]
macro_rules! library_benchmark_group {
(
$( config = $config:expr ; $(;)* )?
benchmarks = $( $function:ident ),+
) => {
compile_error!("A library_benchmark_group! needs a name\n\nlibrary_benchmark_group!(name = some_ident; benchmarks = ...);");
};
(
name = $name:ident;
$( config = $config:expr ; $(;)* )?
benchmarks =
) => {
compile_error!(
"A library_benchmark_group! needs at least 1 benchmark function \
annotated with #[library_benchmark]\n\n\
library_benchmark_group!(name = some_ident; benchmarks = some_library_benchmark);");
};
(
name = $name:ident; $(;)*
$( config = $config:expr ; $(;)* )?
benchmarks = $( $function:ident ),+ $(,)*
) => {
mod $name {
use super::*;
pub const BENCHES: &[&(
&'static str,
fn() -> Option<$crate::internal::InternalLibraryBenchmarkConfig>,
&[$crate::internal::InternalMacroLibBench]
)]= &[
$(
&(
stringify!($function),
super::$function::get_config,
super::$function::BENCHES
)
),+
];
pub fn get_config() -> Option<$crate::internal::InternalLibraryBenchmarkConfig> {
let mut config: Option<$crate::internal::InternalLibraryBenchmarkConfig> = None;
$(
config = Some($config.into());
)?
config
}
#[inline(never)]
pub fn run(group_index: usize, bench_index: usize) {
(BENCHES[group_index].2[bench_index].func)();
}
}
};
}