|
| 1 | +//! Benchmarks for the derivation pipeline. |
| 2 | +
|
| 3 | +#![allow(missing_docs)] |
| 4 | + |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use alloy_primitives::{address, b256, bytes, U256}; |
| 8 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 9 | +use futures::StreamExt; |
| 10 | +use rollup_node_primitives::{BatchCommitData, BatchInfo, L1MessageEnvelope}; |
| 11 | +use rollup_node_providers::{test_utils::NoBlobProvider, DatabaseL1MessageProvider}; |
| 12 | +use scroll_alloy_consensus::TxL1Message; |
| 13 | +use scroll_codec::decoding::test_utils::read_to_bytes; |
| 14 | +use scroll_db::{test_utils::setup_test_db, Database, DatabaseOperations}; |
| 15 | +use scroll_derivation_pipeline::DerivationPipeline; |
| 16 | +use tokio::runtime::{Handle, Runtime}; |
| 17 | + |
| 18 | +async fn setup_pipeline( |
| 19 | +) -> DerivationPipeline<NoBlobProvider<DatabaseL1MessageProvider<Arc<Database>>>> { |
| 20 | + // load batch data in the db. |
| 21 | + let db = Arc::new(setup_test_db().await); |
| 22 | + let raw_calldata = read_to_bytes("./testdata/calldata_v0.bin").unwrap(); |
| 23 | + let batch_data = BatchCommitData { |
| 24 | + hash: b256!("7f26edf8e3decbc1620b4d2ba5f010a6bdd10d6bb16430c4f458134e36ab3961"), |
| 25 | + index: 12, |
| 26 | + block_number: 18319648, |
| 27 | + block_timestamp: 1696935971, |
| 28 | + calldata: Arc::new(raw_calldata), |
| 29 | + blob_versioned_hash: None, |
| 30 | + finalized_block_number: None, |
| 31 | + }; |
| 32 | + db.insert_batch(batch_data).await.unwrap(); |
| 33 | + |
| 34 | + // load messages in db. |
| 35 | + let l1_messages = vec![ |
| 36 | + L1MessageEnvelope { |
| 37 | + l1_block_number: 717, |
| 38 | + l2_block_number: None, |
| 39 | + queue_hash: None, |
| 40 | + transaction: TxL1Message { |
| 41 | + queue_index: 33, |
| 42 | + gas_limit: 168000, |
| 43 | + to: address!("781e90f1c8Fc4611c9b7497C3B47F99Ef6969CbC"), |
| 44 | + value: U256::ZERO, |
| 45 | + sender: address!("7885BcBd5CeCEf1336b5300fb5186A12DDD8c478"), |
| 46 | + input: bytes!("8ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf0000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000000000000000000000000000006a94d74f4300000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), |
| 47 | + }, |
| 48 | + }, |
| 49 | + L1MessageEnvelope { |
| 50 | + l1_block_number: 717, |
| 51 | + l2_block_number: None, |
| 52 | + queue_hash: None, |
| 53 | + transaction: TxL1Message { |
| 54 | + queue_index: 34, |
| 55 | + gas_limit: 168000, |
| 56 | + to: address!("781e90f1c8fc4611c9b7497c3b47f99ef6969cbc"), |
| 57 | + value: U256::ZERO, |
| 58 | + sender: address!("7885BcBd5CeCEf1336b5300fb5186A12DDD8c478"), |
| 59 | + input: bytes!("8ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf000000000000000000000000000000000000000000000000000470de4df820000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f00000000000000000000000000000000000000000000000000470de4df8200000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), |
| 60 | + }, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + for message in l1_messages { |
| 64 | + db.insert_l1_message(message).await.unwrap(); |
| 65 | + } |
| 66 | + |
| 67 | + // construct the pipeline. |
| 68 | + let l1_messages_provider = DatabaseL1MessageProvider::new(db.clone(), 0); |
| 69 | + let mock_l1_provider = NoBlobProvider { l1_messages_provider }; |
| 70 | + DerivationPipeline::new(mock_l1_provider, db) |
| 71 | +} |
| 72 | + |
| 73 | +fn benchmark_pipeline_derivation(c: &mut Criterion) { |
| 74 | + let rt = Runtime::new().unwrap(); |
| 75 | + |
| 76 | + c.bench_function("pipeline_derive_1000_batches", |b| { |
| 77 | + b.to_async(&rt).iter_batched( |
| 78 | + || { |
| 79 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 80 | + Handle::current().spawn(async move { |
| 81 | + // setup (not measured): create fresh pipeline with 1000 committed batches |
| 82 | + let mut pipeline = setup_pipeline().await; |
| 83 | + let batch_info = BatchInfo { index: 12, hash: Default::default() }; |
| 84 | + |
| 85 | + // commit 1000 batches. |
| 86 | + for _ in 0..1000 { |
| 87 | + pipeline.handle_batch_commit(batch_info, 0); |
| 88 | + } |
| 89 | + |
| 90 | + tx.send(pipeline).unwrap(); |
| 91 | + }); |
| 92 | + rx.recv().unwrap() |
| 93 | + }, |
| 94 | + |mut pipeline| async move { |
| 95 | + // measured work: derive 1000 batches. |
| 96 | + for _ in 0..1000 { |
| 97 | + let _ = pipeline.next().await.unwrap(); |
| 98 | + } |
| 99 | + }, |
| 100 | + criterion::BatchSize::SmallInput, |
| 101 | + ) |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +criterion_group!(benches, benchmark_pipeline_derivation); |
| 106 | +criterion_main!(benches); |
0 commit comments