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 ///
38 /// If only `--to` is provided, `from` is derived as `latest_block + 1`.
39 pub const fn new(from: Option<u64>, to: Option<u64>, latest_block: u64) -> Self {
40 // If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
41 // starting at the latest block.
42 match (from, to) {
43 (Some(from), Some(to)) => Self::Range(from..=to),
44 (None, None) => Self::Continuous(latest_block),
45 (Some(start), None) => Self::Continuous(start),
46 (None, Some(to)) => Self::Range(latest_block + 1..=to),
47 }
48 }
49}