macro_rules! compare {
($name:expr, $path_taken:expr, || $block_1:expr, || $block_2:expr) => { ... };
($name:expr, $path_taken:expr, move || $block_1:expr, move || $block_2:expr) => { ... };
($name:expr, $path_taken:expr, || $block_1:expr, || $block_2:expr; $( $key:expr => $value:expr ), *) => { ... };
($name:expr, $path_taken:expr, move || $block_1:expr, move || $block_2:expr; $( $key:expr => $value:expr ), *) => { ... };
}Expand description
Run an experiment comparing the execution time of two blocks of code Example:
// This could also be a network request to an experiment management service, or anything you want
let path = rng.gen_range(0..2);
compare!("test", path, || {
sleep(Duration::from_millis(rng.gen_range(5..10)));
}, || {
sleep(Duration::from_millis(rng.gen_range(5..12)));
}; "some" => "tag");The above code will run the first block passed, and after execution, will emit a timing metric to datadog. The metric will be named with the value of EXPERIMENTS_METRIC_NAME, and will be tagged with the name of the experiment (“experiment_name:test”), the path taken (“path_taken:0”), and any additional tags provided (“some:tag”).
The blocks can be arbitrary code, and the macro is async-safe. In an async context, the timing will continue across await points, which means that if, for example, you are awaiting a network request, the timing will include the time spent waiting for the network request to complete.
NOTE: Try to minimise variation in tag values (avoid things like timestamps or ids). See note in lib docs!