reth_bench/bench_mode.rs
1//! The benchmark mode defines whether the benchmark should run for a closed or open range of
2//! blocks.
3use std::ops::RangeInclusive;
4
5/// Whether or not the benchmark should run as a continuous stream of payloads.
6#[derive(Debug, PartialEq, Eq)]
7pub enum BenchMode {
8 // TODO: just include the start block in `Continuous`
9 /// Run the benchmark as a continuous stream of payloads, until the benchmark is interrupted.
10 Continuous,
11 /// Run the benchmark for a specific range of blocks.
12 Range(RangeInclusive<u64>),
13}
14
15impl BenchMode {
16 /// Check if the block number is in the range
17 pub fn contains(&self, block_number: u64) -> bool {
18 match self {
19 Self::Continuous => true,
20 Self::Range(range) => range.contains(&block_number),
21 }
22 }
23
24 /// Create a [`BenchMode`] from optional `from` and `to` fields.
25 pub fn new(from: Option<u64>, to: Option<u64>) -> Result<Self, eyre::Error> {
26 // If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
27 // starting at the latest block.
28 match (from, to) {
29 (Some(from), Some(to)) => Ok(Self::Range(from..=to)),
30 (None, None) => Ok(Self::Continuous),
31 _ => {
32 // both or neither are allowed, everything else is ambiguous
33 Err(eyre::eyre!("`from` and `to` must be provided together, or not at all."))
34 }
35 }
36 }
37}