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 /// Returns the total number of blocks in the benchmark, if known.
24 ///
25 /// For [`BenchMode::Range`] this is the length of the range.
26 /// For [`BenchMode::Continuous`] the total is unbounded, so `None` is returned.
27 pub const fn total_blocks(&self) -> Option<u64> {
28 match self {
29 Self::Continuous(_) => None,
30 Self::Range(range) => {
31 Some(range.end().saturating_sub(*range.start()).saturating_add(1))
32 }
33 }
34 }
35
36 /// Create a [`BenchMode`] from optional `from` and `to` fields.
37 pub fn new(from: Option<u64>, to: Option<u64>, latest_block: u64) -> Result<Self, eyre::Error> {
38 // If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
39 // starting at the latest block.
40 match (from, to) {
41 (Some(from), Some(to)) => Ok(Self::Range(from..=to)),
42 (None, None) => Ok(Self::Continuous(latest_block)),
43 (Some(start), None) => Ok(Self::Continuous(start)),
44 _ => {
45 // both or neither are allowed, everything else is ambiguous
46 Err(eyre::eyre!("`from` and `to` must be provided together, or not at all."))
47 }
48 }
49 }
50}