Skip to main content

reth_bench/bench/
new_payload_only.rs

1//! Runs the `reth bench` command, sending only newPayload, without a forkchoiceUpdated call.
2
3use crate::{
4    bench::{
5        context::BenchContext,
6        metrics_scraper::MetricsScraper,
7        output::{
8            NewPayloadResult, TotalGasOutput, TotalGasRow, GAS_OUTPUT_SUFFIX,
9            NEW_PAYLOAD_OUTPUT_SUFFIX,
10        },
11    },
12    valid_payload::{block_to_new_payload, call_new_payload_with_reth},
13};
14use alloy_provider::{ext::DebugApi, Provider};
15use clap::Parser;
16use csv::Writer;
17use eyre::{Context, OptionExt};
18use reth_cli_runner::CliContext;
19use reth_node_core::args::BenchmarkArgs;
20use std::time::{Duration, Instant};
21use tracing::{debug, info};
22
23/// `reth benchmark new-payload-only` command
24#[derive(Debug, Parser)]
25pub struct Command {
26    /// The RPC url to use for getting data.
27    #[arg(long, value_name = "RPC_URL", verbatim_doc_comment)]
28    rpc_url: String,
29
30    /// The size of the block buffer (channel capacity) for prefetching blocks from the RPC
31    /// endpoint.
32    #[arg(
33        long = "rpc-block-buffer-size",
34        value_name = "RPC_BLOCK_BUFFER_SIZE",
35        default_value = "20",
36        verbatim_doc_comment
37    )]
38    rpc_block_buffer_size: usize,
39
40    #[command(flatten)]
41    benchmark: BenchmarkArgs,
42}
43
44impl Command {
45    /// Execute `benchmark new-payload-only` command
46    pub async fn execute(self, _ctx: CliContext) -> eyre::Result<()> {
47        let BenchContext {
48            benchmark_mode,
49            block_provider,
50            auth_provider,
51            mut next_block,
52            is_optimism,
53            use_reth_namespace,
54            rlp_blocks,
55        } = BenchContext::new(&self.benchmark, self.rpc_url).await?;
56
57        let total_blocks = benchmark_mode.total_blocks();
58
59        let mut metrics_scraper = MetricsScraper::maybe_new(self.benchmark.metrics_url.clone());
60
61        if use_reth_namespace {
62            info!("Using reth_newPayload endpoint");
63        }
64
65        let buffer_size = self.rpc_block_buffer_size;
66
67        // Use a oneshot channel to propagate errors from the spawned task
68        let (error_sender, mut error_receiver) = tokio::sync::oneshot::channel();
69        let (sender, mut receiver) = tokio::sync::mpsc::channel(buffer_size);
70
71        tokio::task::spawn(async move {
72            while benchmark_mode.contains(next_block) {
73                let block_res = block_provider
74                    .get_block_by_number(next_block.into())
75                    .full()
76                    .await
77                    .wrap_err_with(|| format!("Failed to fetch block by number {next_block}"));
78                let block = match block_res.and_then(|opt| opt.ok_or_eyre("Block not found")) {
79                    Ok(block) => block,
80                    Err(e) => {
81                        tracing::error!(target: "reth-bench", "Failed to fetch block {next_block}: {e}");
82                        let _ = error_sender.send(e);
83                        break;
84                    }
85                };
86
87                let rlp = if rlp_blocks {
88                    let Ok(rlp) = block_provider.debug_get_raw_block(next_block.into()).await
89                    else {
90                        tracing::error!(target: "reth-bench", "Failed to fetch raw block {next_block}");
91                        let _ = error_sender
92                            .send(eyre::eyre!("Failed to fetch raw block {next_block}"));
93                        break;
94                    };
95                    Some(rlp)
96                } else {
97                    None
98                };
99
100                next_block += 1;
101                if let Err(e) = sender.send((block, rlp)).await {
102                    tracing::error!(target: "reth-bench", "Failed to send block data: {e}");
103                    break;
104                }
105            }
106        });
107
108        let mut results = Vec::new();
109        let mut blocks_processed = 0u64;
110        let total_benchmark_duration = Instant::now();
111        let mut total_wait_time = Duration::ZERO;
112
113        while let Some((block, rlp)) = {
114            let wait_start = Instant::now();
115            let result = receiver.recv().await;
116            total_wait_time += wait_start.elapsed();
117            result
118        } {
119            let block_number = block.header.number;
120            let transaction_count = block.transactions.len() as u64;
121            let gas_used = block.header.gas_used;
122
123            debug!(target: "reth-bench", number=?block.header.number, "Sending payload to engine");
124
125            let (version, params) =
126                block_to_new_payload(block, is_optimism, rlp, use_reth_namespace)?;
127
128            let start = Instant::now();
129            let server_timings =
130                call_new_payload_with_reth(&auth_provider, version, params).await?;
131
132            let latency =
133                server_timings.as_ref().map(|t| t.latency).unwrap_or_else(|| start.elapsed());
134            let new_payload_result = NewPayloadResult {
135                gas_used,
136                latency,
137                persistence_wait: server_timings.as_ref().and_then(|t| t.persistence_wait),
138                execution_cache_wait: server_timings
139                    .as_ref()
140                    .map(|t| t.execution_cache_wait)
141                    .unwrap_or_default(),
142                sparse_trie_wait: server_timings
143                    .as_ref()
144                    .map(|t| t.sparse_trie_wait)
145                    .unwrap_or_default(),
146            };
147            blocks_processed += 1;
148            let progress = match total_blocks {
149                Some(total) => format!("{blocks_processed}/{total}"),
150                None => format!("{blocks_processed}"),
151            };
152            info!(target: "reth-bench", progress, %new_payload_result);
153
154            // current duration since the start of the benchmark minus the time
155            // waiting for blocks
156            let current_duration = total_benchmark_duration.elapsed() - total_wait_time;
157
158            // record the current result
159            let row =
160                TotalGasRow { block_number, transaction_count, gas_used, time: current_duration };
161            results.push((row, new_payload_result));
162
163            if let Some(scraper) = metrics_scraper.as_mut() &&
164                let Err(err) = scraper.scrape_after_block(block_number).await
165            {
166                tracing::warn!(target: "reth-bench", %err, block_number, "Failed to scrape metrics");
167            }
168        }
169
170        // Check if the spawned task encountered an error
171        if let Ok(error) = error_receiver.try_recv() {
172            return Err(error);
173        }
174
175        let (gas_output_results, new_payload_results): (_, Vec<NewPayloadResult>) =
176            results.into_iter().unzip();
177
178        // write the csv output to files
179        if let Some(path) = self.benchmark.output {
180            // first write the new payload results to a file
181            let output_path = path.join(NEW_PAYLOAD_OUTPUT_SUFFIX);
182            info!(target: "reth-bench", "Writing newPayload call latency output to file: {:?}", output_path);
183            let mut writer = Writer::from_path(output_path)?;
184            for result in new_payload_results {
185                writer.serialize(result)?;
186            }
187            writer.flush()?;
188
189            // now write the gas output to a file
190            let output_path = path.join(GAS_OUTPUT_SUFFIX);
191            info!(target: "reth-bench", "Writing total gas output to file: {:?}", output_path);
192            let mut writer = Writer::from_path(output_path)?;
193            for row in &gas_output_results {
194                writer.serialize(row)?;
195            }
196            writer.flush()?;
197
198            if let Some(scraper) = &metrics_scraper {
199                scraper.write_csv(&path)?;
200            }
201
202            info!(target: "reth-bench", "Finished writing benchmark output files to {:?}.", path);
203        }
204
205        // accumulate the results and calculate the overall Ggas/s
206        let gas_output = TotalGasOutput::new(gas_output_results)?;
207        info!(
208            target: "reth-bench",
209            total_duration=?gas_output.total_duration,
210            total_gas_used=?gas_output.total_gas_used,
211            blocks_processed=?gas_output.blocks_processed,
212            "Total Ggas/s: {:.4}",
213            gas_output.total_gigagas_per_second()
214        );
215
216        Ok(())
217    }
218}