Skip to content

Commit c94ca25

Browse files
authored
feat(rbuilder): add more relay submission metrics, smol optimizations (#824)
## 📝 Summary - Adds histograms for SSZ encoding and GZIP compression times, for normal (non optimistic V3) payloads - Some small optimizations: - Consolidate loops in hot path - Pre-allocate GZIP compression buffer ## 💡 Motivation and Context We would like to understand how long these things take, and specifically if it wouldn't be better to disable gzip compression. ## ✅ I have completed the following steps: * [x] Run `make lint` * [x] Run `make test` * [x] Added tests (if applicable)
1 parent ce67ef8 commit c94ca25

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

crates/rbuilder/src/live_builder/block_output/relay_submit.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,30 +178,33 @@ async fn run_submit_to_relays_job(
178178

179179
let builder_name = block.builder_name.clone();
180180

181-
let bundles = block
182-
.trace
183-
.included_orders
184-
.iter()
185-
.filter(|o| !o.order.is_tx())
186-
.count();
181+
let mut bundles = 0;
182+
let mut order_ids = Vec::with_capacity(block.trace.included_orders.len());
183+
let mut bundle_hashes = Vec::with_capacity(block.trace.included_orders.len());
184+
185+
for exec_res in &block.trace.included_orders {
186+
if !exec_res.order.is_tx() {
187+
bundles += 1;
188+
}
189+
190+
for order in exec_res.order.original_orders() {
191+
order_ids.push(order.id());
192+
if let Some(bundle_hash) = order.external_bundle_hash() {
193+
bundle_hashes.push(bundle_hash);
194+
}
195+
}
196+
}
187197

188198
// SAFETY: UNIX timestamp in nanos won't exceed u64::MAX until year 2554
189199
let sequence = OffsetDateTime::now_utc().unix_timestamp_nanos() as u64;
190-
let executed_orders = block
191-
.trace
192-
.included_orders
193-
.iter()
194-
.flat_map(|exec_res| exec_res.order.original_orders());
195200
let bid_metadata = BidMetadata {
196201
sequence,
197202
value: BidValueMetadata {
198203
coinbase_reward: block.trace.coinbase_reward,
199204
top_competitor_bid: block.trace.seen_competition_bid,
200205
},
201-
order_ids: executed_orders.clone().map(|o| o.id()).collect(),
202-
bundle_hashes: executed_orders
203-
.filter_map(|o| o.external_bundle_hash())
204-
.collect(),
206+
order_ids,
207+
bundle_hashes,
205208
};
206209

207210
let latency = block.trace.orders_sealed_at - block.trace.orders_closed_at;

crates/rbuilder/src/mev_boost/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::telemetry::{add_gzip_compression_time, add_ssz_encoding_time};
2+
13
use super::utils::u256decimal_serde_helper;
24
use alloy_primitives::{utils::parse_ether, Address, BlockHash, U256};
35
use alloy_rpc_types_beacon::BlsPublicKey;
@@ -729,7 +731,11 @@ impl RelayClient {
729731
let mut builder = self.client.post(url.clone());
730732
// SSZ vs JSON
731733
let (mut body_data, content_type) = if ssz {
732-
(submission.as_ssz_bytes(), SSZ_CONTENT_TYPE)
734+
let ssz_start = std::time::Instant::now();
735+
let encoded = submission.as_ssz_bytes();
736+
737+
add_ssz_encoding_time(ssz_start.elapsed());
738+
(encoded, SSZ_CONTENT_TYPE)
733739
} else {
734740
let json_result = if fake_relay {
735741
// For the fake relay we remove the blobs
@@ -751,13 +757,19 @@ impl RelayClient {
751757
CONTENT_ENCODING,
752758
HeaderValue::from_static(GZIP_CONTENT_ENCODING),
753759
);
754-
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
760+
let gzip_start = std::time::Instant::now();
761+
// NOTE: Pre-allocate the buffer to avoid reallocations during compression.
762+
// This should be more efficient even if we over-allocate (uncompressed > compressed)
763+
let mut encoder =
764+
GzEncoder::new(Vec::with_capacity(body_data.len()), Compression::default());
755765
encoder
756766
.write_all(&body_data)
757767
.map_err(|e| SubmitBlockErr::RPCSerializationError(e.to_string()))?;
758768
body_data = encoder
759769
.finish()
760770
.map_err(|e| SubmitBlockErr::RPCSerializationError(e.to_string()))?;
771+
772+
add_gzip_compression_time(gzip_start.elapsed());
761773
}
762774

763775
// Set bloxroute specific headers.

crates/rbuilder/src/telemetry/metrics/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use metrics_macros::register_metrics;
2121
use parking_lot::Mutex;
2222
use prometheus::{
2323
core::{Atomic, AtomicF64, AtomicI64, GenericGauge},
24-
Counter, Gauge, HistogramOpts, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec,
25-
Opts, Registry,
24+
Counter, Gauge, Histogram, HistogramOpts, HistogramVec, IntCounter, IntCounterVec, IntGauge,
25+
IntGaugeVec, Opts, Registry,
2626
};
2727
use rbuilder_primitives::mev_boost::MevBoostRelayID;
2828
use std::time::Duration;
@@ -233,6 +233,20 @@ register_metrics! {
233233
)
234234
.unwrap();
235235

236+
pub static SSZ_ENCODING_TIME: Histogram = Histogram::with_opts(
237+
HistogramOpts::new("payload_ssz_encoding_time", "Time to encode a full payload in SSZ (ms)")
238+
// Range: 100us - 100ms
239+
.buckets(exponential_buckets_range(0.1, 100.0, 20)),
240+
)
241+
.unwrap();
242+
243+
pub static GZIP_COMPRESSION_TIME: Histogram = Histogram::with_opts(
244+
HistogramOpts::new("payload_gzip_compression_time", "Time to compress a full payload in GZIP (ms)")
245+
// Range: 100us - 200ms
246+
.buckets(exponential_buckets_range(0.1, 200.0, 50)),
247+
)
248+
.unwrap();
249+
236250
pub static RPC_PROCESSING_TIME: HistogramVec = HistogramVec::new(
237251
HistogramOpts::new("rpc_processing_time", "Time spend in RPC handlers (us)")
238252
.buckets(exponential_buckets_range(10.0, 50000.0, 100)),
@@ -582,6 +596,14 @@ pub fn add_relay_submit_time(relay: &MevBoostRelayID, duration: Duration) {
582596
.observe(duration_ms(duration));
583597
}
584598

599+
pub fn add_ssz_encoding_time(duration: Duration) {
600+
SSZ_ENCODING_TIME.observe(duration_ms(duration));
601+
}
602+
603+
pub fn add_gzip_compression_time(duration: Duration) {
604+
GZIP_COMPRESSION_TIME.observe(duration_ms(duration));
605+
}
606+
585607
const BIG_RPC_DATA_THRESHOLD: usize = 50000;
586608
const BIG_RPC_DATA_TEXT: &str = ">50K";
587609
const SMALL_RPC_DATA_TEXT: &str = "<=50K";

0 commit comments

Comments
 (0)