Skip to main content

reth_bench/bench/
mod.rs

1//! `reth benchmark` command. Collection of various benchmarking routines.
2
3use clap::{Parser, Subcommand};
4use reth_cli_runner::CliContext;
5use reth_node_core::args::LogArgs;
6use reth_tracing::FileWorkerGuard;
7
8mod context;
9mod generate_big_block;
10pub(crate) mod helpers;
11pub use generate_big_block::{
12    RawTransaction, RpcTransactionSource, TransactionCollector, TransactionSource,
13};
14pub(crate) mod metrics_scraper;
15mod new_payload_fcu;
16mod new_payload_only;
17mod output;
18mod replay_payloads;
19mod send_invalid_payload;
20mod send_payload;
21
22/// `reth bench` command
23#[derive(Debug, Parser)]
24pub struct BenchmarkCommand {
25    #[command(subcommand)]
26    command: Subcommands,
27
28    #[command(flatten)]
29    logs: LogArgs,
30}
31
32/// `reth benchmark` subcommands
33#[derive(Subcommand, Debug)]
34pub enum Subcommands {
35    /// Benchmark which calls `newPayload`, then `forkchoiceUpdated`.
36    NewPayloadFcu(new_payload_fcu::Command),
37
38    /// Benchmark which only calls subsequent `newPayload` calls.
39    NewPayloadOnly(new_payload_only::Command),
40
41    /// Command for generating and sending an `engine_newPayload` request constructed from an RPC
42    /// block.
43    ///
44    /// This command takes a JSON block input (either from a file or stdin) and generates
45    /// an execution payload that can be used with the `engine_newPayloadV*` API.
46    ///
47    /// One powerful use case is pairing this command with the `cast block` command, for example:
48    ///
49    /// `cast block latest --full --json | reth-bench send-payload --rpc-url localhost:5000
50    /// --jwt-secret $(cat ~/.local/share/reth/mainnet/jwt.hex)`
51    SendPayload(send_payload::Command),
52
53    /// Generate a large block by packing transactions from existing blocks.
54    ///
55    /// This command fetches transactions from real blocks and packs them into a single
56    /// block using the `testing_buildBlockV1` RPC endpoint.
57    ///
58    /// Example:
59    ///
60    /// `reth-bench generate-big-block --rpc-url http://localhost:8545 --engine-rpc-url
61    /// http://localhost:8551 --jwt-secret ~/.local/share/reth/mainnet/jwt.hex --target-gas
62    /// 30000000`
63    GenerateBigBlock(generate_big_block::Command),
64
65    /// Replay pre-generated payloads from a directory.
66    ///
67    /// This command reads payload files from a previous `generate-big-block` run and replays
68    /// them in sequence using `newPayload` followed by `forkchoiceUpdated`.
69    ///
70    /// Example:
71    ///
72    /// `reth-bench replay-payloads --payload-dir ./payloads --engine-rpc-url
73    /// http://localhost:8551 --jwt-secret ~/.local/share/reth/mainnet/jwt.hex`
74    ReplayPayloads(replay_payloads::Command),
75
76    /// Generate and send an invalid `engine_newPayload` request for testing.
77    ///
78    /// Takes a valid block and modifies fields to make it invalid, allowing you to test
79    /// Engine API rejection behavior. Block hash is recalculated after modifications
80    /// unless `--invalid-block-hash` or `--skip-hash-recalc` is used.
81    ///
82    /// Example:
83    ///
84    /// `cast block latest --full --json | reth-bench send-invalid-payload --rpc-url localhost:5000
85    /// --jwt-secret $(cat ~/.local/share/reth/mainnet/jwt.hex) --invalid-state-root`
86    SendInvalidPayload(Box<send_invalid_payload::Command>),
87}
88
89impl BenchmarkCommand {
90    /// Execute `benchmark` command
91    pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
92        // Initialize tracing
93        let _guard = self.init_tracing()?;
94
95        match self.command {
96            Subcommands::NewPayloadFcu(command) => command.execute(ctx).await,
97            Subcommands::NewPayloadOnly(command) => command.execute(ctx).await,
98            Subcommands::SendPayload(command) => command.execute(ctx).await,
99            Subcommands::GenerateBigBlock(command) => command.execute(ctx).await,
100            Subcommands::ReplayPayloads(command) => command.execute(ctx).await,
101            Subcommands::SendInvalidPayload(command) => (*command).execute(ctx).await,
102        }
103    }
104
105    /// Initializes tracing with the configured options.
106    ///
107    /// If file logging is enabled, this function returns a guard that must be kept alive to ensure
108    /// that all logs are flushed to disk.
109    ///
110    /// Always enables log target display (`RUST_LOG_TARGET=1`) so that the `reth-bench` target
111    /// is visible in output, making it easy to distinguish reth-bench logs from reth logs when
112    /// both are streamed to the same console or file.
113    pub fn init_tracing(&self) -> eyre::Result<Option<FileWorkerGuard>> {
114        // Always show the log target so "reth-bench" is visible in the output.
115        if std::env::var_os("RUST_LOG_TARGET").is_none() {
116            // SAFETY: This is called early during single-threaded initialization, before any
117            // threads are spawned and before the tracing subscriber is set up.
118            unsafe { std::env::set_var("RUST_LOG_TARGET", "1") };
119        }
120
121        let guard = self.logs.init_tracing()?;
122        Ok(guard)
123    }
124}