reth_bench/bench/
output.rs

1//! Contains various benchmark output formats, either for logging or for
2//! serialization to / from files.
3
4use reth_primitives_traits::constants::GIGAGAS;
5use serde::{ser::SerializeStruct, Serialize};
6use std::time::Duration;
7
8/// This is the suffix for gas output csv files.
9pub(crate) const GAS_OUTPUT_SUFFIX: &str = "total_gas.csv";
10
11/// This is the suffix for combined output csv files.
12pub(crate) const COMBINED_OUTPUT_SUFFIX: &str = "combined_latency.csv";
13
14/// This is the suffix for new payload output csv files.
15pub(crate) const NEW_PAYLOAD_OUTPUT_SUFFIX: &str = "new_payload_latency.csv";
16
17/// This represents the results of a single `newPayload` call in the benchmark, containing the gas
18/// used and the `newPayload` latency.
19#[derive(Debug)]
20pub(crate) struct NewPayloadResult {
21    /// The gas used in the `newPayload` call.
22    pub(crate) gas_used: u64,
23    /// The latency of the `newPayload` call.
24    pub(crate) latency: Duration,
25}
26
27impl NewPayloadResult {
28    /// Returns the gas per second processed in the `newPayload` call.
29    pub(crate) fn gas_per_second(&self) -> f64 {
30        self.gas_used as f64 / self.latency.as_secs_f64()
31    }
32}
33
34impl std::fmt::Display for NewPayloadResult {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(
37            f,
38            "New payload processed at {:.4} Ggas/s, used {} total gas. Latency: {:?}",
39            self.gas_per_second() / GIGAGAS as f64,
40            self.gas_used,
41            self.latency
42        )
43    }
44}
45
46/// This is another [`Serialize`] implementation for the [`NewPayloadResult`] struct, serializing
47/// the duration as microseconds because the csv writer would fail otherwise.
48impl Serialize for NewPayloadResult {
49    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50    where
51        S: serde::ser::Serializer,
52    {
53        // convert the time to microseconds
54        let time = self.latency.as_micros();
55        let mut state = serializer.serialize_struct("NewPayloadResult", 3)?;
56        state.serialize_field("gas_used", &self.gas_used)?;
57        state.serialize_field("latency", &time)?;
58        state.end()
59    }
60}
61
62/// This represents the combined results of a `newPayload` call and a `forkchoiceUpdated` call in
63/// the benchmark, containing the gas used, the `newPayload` latency, and the `forkchoiceUpdated`
64/// latency.
65#[derive(Debug)]
66pub(crate) struct CombinedResult {
67    /// The block number of the block being processed.
68    pub(crate) block_number: u64,
69    /// The `newPayload` result.
70    pub(crate) new_payload_result: NewPayloadResult,
71    /// The latency of the `forkchoiceUpdated` call.
72    pub(crate) fcu_latency: Duration,
73    /// The latency of both calls combined.
74    pub(crate) total_latency: Duration,
75}
76
77impl CombinedResult {
78    /// Returns the gas per second, including the `newPayload` _and_ `forkchoiceUpdated` duration.
79    pub(crate) fn combined_gas_per_second(&self) -> f64 {
80        self.new_payload_result.gas_used as f64 / self.total_latency.as_secs_f64()
81    }
82}
83
84impl std::fmt::Display for CombinedResult {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        write!(
87            f,
88            "Payload {} processed at {:.4} Ggas/s, used {} total gas. Combined gas per second: {:.4} Ggas/s. fcu latency: {:?}, newPayload latency: {:?}",
89            self.block_number,
90            self.new_payload_result.gas_per_second() / GIGAGAS as f64,
91            self.new_payload_result.gas_used,
92            self.combined_gas_per_second() / GIGAGAS as f64,
93            self.fcu_latency,
94            self.new_payload_result.latency
95        )
96    }
97}
98
99/// This is a [`Serialize`] implementation for the [`CombinedResult`] struct, serializing the
100/// durations as microseconds because the csv writer would fail otherwise.
101impl Serialize for CombinedResult {
102    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
103    where
104        S: serde::ser::Serializer,
105    {
106        // convert the time to microseconds
107        let fcu_latency = self.fcu_latency.as_micros();
108        let new_payload_latency = self.new_payload_result.latency.as_micros();
109        let total_latency = self.total_latency.as_micros();
110        let mut state = serializer.serialize_struct("CombinedResult", 5)?;
111
112        // flatten the new payload result because this is meant for CSV writing
113        state.serialize_field("block_number", &self.block_number)?;
114        state.serialize_field("gas_used", &self.new_payload_result.gas_used)?;
115        state.serialize_field("new_payload_latency", &new_payload_latency)?;
116        state.serialize_field("fcu_latency", &fcu_latency)?;
117        state.serialize_field("total_latency", &total_latency)?;
118        state.end()
119    }
120}
121
122/// This represents a row of total gas data in the benchmark.
123#[derive(Debug)]
124pub(crate) struct TotalGasRow {
125    /// The block number of the block being processed.
126    #[allow(dead_code)]
127    pub(crate) block_number: u64,
128    /// The total gas used in the block.
129    pub(crate) gas_used: u64,
130    /// Time since the start of the benchmark.
131    pub(crate) time: Duration,
132}
133
134/// This represents the aggregated output, meant to show gas per second metrics, of a benchmark run.
135#[derive(Debug)]
136pub(crate) struct TotalGasOutput {
137    /// The total gas used in the benchmark.
138    pub(crate) total_gas_used: u64,
139    /// The total duration of the benchmark.
140    pub(crate) total_duration: Duration,
141    /// The total gas used per second.
142    pub(crate) total_gas_per_second: f64,
143    /// The number of blocks processed.
144    pub(crate) blocks_processed: u64,
145}
146
147impl TotalGasOutput {
148    /// Create a new [`TotalGasOutput`] from a list of [`TotalGasRow`].
149    pub(crate) fn new(rows: Vec<TotalGasRow>) -> Self {
150        // the duration is obtained from the last row
151        let total_duration =
152            rows.last().map(|row| row.time).expect("the row has at least one element");
153        let blocks_processed = rows.len() as u64;
154        let total_gas_used: u64 = rows.into_iter().map(|row| row.gas_used).sum();
155        let total_gas_per_second = total_gas_used as f64 / total_duration.as_secs_f64();
156
157        Self { total_gas_used, total_duration, total_gas_per_second, blocks_processed }
158    }
159
160    /// Return the total gigagas per second.
161    pub(crate) fn total_gigagas_per_second(&self) -> f64 {
162        self.total_gas_per_second / GIGAGAS as f64
163    }
164}
165
166/// This serializes the `time` field of the [`TotalGasRow`] to microseconds.
167///
168/// This is essentially just for the csv writer, which would have headers
169impl Serialize for TotalGasRow {
170    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
171    where
172        S: serde::ser::Serializer,
173    {
174        // convert the time to microseconds
175        let time = self.time.as_micros();
176        let mut state = serializer.serialize_struct("TotalGasRow", 3)?;
177        state.serialize_field("block_number", &self.block_number)?;
178        state.serialize_field("gas_used", &self.gas_used)?;
179        state.serialize_field("time", &time)?;
180        state.end()
181    }
182}
183
184#[cfg(test)]
185mod tests {
186    use super::*;
187    use csv::Writer;
188    use std::io::BufRead;
189
190    #[test]
191    fn test_write_total_gas_row_csv() {
192        let row = TotalGasRow { block_number: 1, gas_used: 1_000, time: Duration::from_secs(1) };
193
194        let mut writer = Writer::from_writer(vec![]);
195        writer.serialize(row).unwrap();
196        let result = writer.into_inner().unwrap();
197
198        // parse into Lines
199        let mut result = result.as_slice().lines();
200
201        // assert header
202        let expected_first_line = "block_number,gas_used,time";
203        let first_line = result.next().unwrap().unwrap();
204        assert_eq!(first_line, expected_first_line);
205
206        let expected_second_line = "1,1000,1000000";
207        let second_line = result.next().unwrap().unwrap();
208        assert_eq!(second_line, expected_second_line);
209    }
210}