-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simple bench target for measuring transaction batch lookup times
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use fuel_core::{ | ||
combined_database::CombinedDatabase, | ||
fuel_core_graphql_api::database::ReadDatabase, | ||
}; | ||
|
||
use fuel_core_storage::{ | ||
tables::{ | ||
FuelBlocks, | ||
Transactions, | ||
}, | ||
transactional::WriteTransaction, | ||
StorageAsMut, | ||
}; | ||
use fuel_core_types::{ | ||
blockchain::block::CompressedBlock, | ||
fuel_tx::{ | ||
TxId, | ||
UniqueIdentifier, | ||
}, | ||
fuel_types::{ | ||
BlockHeight, | ||
ChainId, | ||
}, | ||
}; | ||
use rand::{ | ||
rngs::StdRng, | ||
SeedableRng, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
println!("Setting up bench harness."); | ||
let mut harness = Harness::new(StdRng::seed_from_u64(2322)).await?; | ||
|
||
println!("Populating storage with transactions."); | ||
let tx_ids = harness.populate_database().await?; | ||
|
||
println!("Querying transactions from storage."); | ||
harness.query_database_many_times(&tx_ids).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
struct Harness<Rng> { | ||
rng: Rng, | ||
params: Parameters, | ||
db: CombinedDatabase, | ||
} | ||
|
||
impl<Rng: rand::RngCore + rand::CryptoRng + Send> Harness<Rng> { | ||
async fn new(rng: Rng) -> anyhow::Result<Self> { | ||
let params = Parameters::hard_coded(); | ||
let db = CombinedDatabase::default(); | ||
|
||
Ok(Self { rng, params, db }) | ||
} | ||
|
||
async fn populate_database(&mut self) -> anyhow::Result<Vec<TxId>> { | ||
let mut tx_ids = Vec::new(); | ||
|
||
for block_height in 0..self.params.num_blocks { | ||
let block_height = BlockHeight::from(block_height as u32); | ||
let mut compressed_block = CompressedBlock::default(); | ||
compressed_block.header_mut().set_block_height(block_height); | ||
|
||
let mut transaction = self.db.on_chain_mut().write_transaction(); | ||
|
||
transaction | ||
.storage::<FuelBlocks>() | ||
.insert(&block_height, &compressed_block) | ||
.unwrap(); | ||
|
||
for tx in (0..self.params.tx_count_per_block).map(|i| { | ||
let script_gas_limit = 26; // Cost of OP_RET * 2 | ||
test_helpers::make_tx(&mut self.rng, i, script_gas_limit) | ||
}) { | ||
let tx_id = tx.id(&ChainId::default()); | ||
transaction | ||
.storage::<Transactions>() | ||
.insert(&tx_id, &tx) | ||
.unwrap(); | ||
|
||
tx_ids.push(tx_id); | ||
} | ||
|
||
transaction.commit().unwrap(); | ||
} | ||
|
||
Ok(tx_ids) | ||
} | ||
|
||
async fn query_database_many_times(&mut self, tx_ids: &[TxId]) -> anyhow::Result<()> { | ||
let read_database = ReadDatabase::new( | ||
0, | ||
BlockHeight::new(0), | ||
self.db.on_chain().clone(), | ||
self.db.off_chain().clone(), | ||
); | ||
|
||
let read_view = read_database.view()?; | ||
|
||
for _ in 0..self.params.num_queries { | ||
let _ = read_view.transactions(tx_ids.to_vec()).await; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct Parameters { | ||
num_queries: usize, | ||
num_blocks: usize, | ||
tx_count_per_block: u64, | ||
} | ||
|
||
impl Parameters { | ||
fn hard_coded() -> Self { | ||
Self { | ||
num_queries: 100, | ||
num_blocks: 100, | ||
tx_count_per_block: 1000, | ||
} | ||
} | ||
} |