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