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    /// Run the benchmark as a continuous stream of payloads, until the benchmark is interrupted.
9    Continuous(u64),
10    /// Run the benchmark for a specific range of blocks.
11    Range(RangeInclusive<u64>),
12}
13
14impl BenchMode {
15    /// Check if the block number is in the range
16    pub fn contains(&self, block_number: u64) -> bool {
17        match self {
18            Self::Continuous(start) => block_number >= *start,
19            Self::Range(range) => range.contains(&block_number),
20        }
21    }
22
23    /// Create a [`BenchMode`] from optional `from` and `to` fields.
24    pub fn new(from: Option<u64>, to: Option<u64>, latest_block: u64) -> Result<Self, eyre::Error> {
25        // If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
26        // starting at the latest block.
27        match (from, to) {
28            (Some(from), Some(to)) => Ok(Self::Range(from..=to)),
29            (None, None) => Ok(Self::Continuous(latest_block)),
30            (Some(start), None) => Ok(Self::Continuous(start)),
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}