Skip to content

Commit

Permalink
fix(db_lookup_times): rework core logic of benchmark (#2159)
Browse files Browse the repository at this point in the history
>[!NOTE]
> This PR follows up #2142 with comments after merge

## Linked Issues/PRs
<!-- List of related issues/PRs -->
- #2142 
- #2023 

## Description
<!-- List of detailed changes -->
- refactors the test database to use a custom set of columns that will
not create any new tables/columns in `fuel-core`
- refactors the functions to return `Result`s instead of unwrapping
everywhere
- automated cleaning up of benchmark databases
- `KeyValueMutate` impl of `RocksDb` is now behind the `test-helpers`
feature flag
- moved the `db_lookup_times_utils` to the `src` directory in the
benches crate so we can test our insertion/fetching logic

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [ ] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?

---------

Co-authored-by: Green Baneling <[email protected]>
  • Loading branch information
rymnc and xgreenx authored Sep 9, 2024
1 parent 9487bf2 commit 877b52b
Show file tree
Hide file tree
Showing 17 changed files with 554 additions and 365 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ criterion = { version = "0.5", features = [
] }
ctrlc = "3.2.3"
ed25519-dalek = { version = "2.0", features = ["rand_core"] }
enum-iterator = { workspace = true }
ethnum = "1.3"
fuel-core = { path = "../crates/fuel-core", default-features = false, features = [
"smt",
Expand All @@ -30,6 +31,8 @@ fuel-core-sync = { path = "./../crates/services/sync", features = [
] }
fuel-core-types = { path = "./../crates/types", features = ["test-helpers"] }
futures = { workspace = true }
itertools = { workspace = true }
num_enum = { workspace = true }
p256 = { version = "0.13", default-features = false, features = [
"digest",
"ecdsa",
Expand All @@ -41,6 +44,8 @@ rand = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_yaml = "0.9.13"
strum = { workspace = true }
strum_macros = { workspace = true }
test-helpers = { path = "../tests/test-helpers" }
tikv-jemallocator = { workspace = true }
tokio = { workspace = true, features = ["full"] }
Expand Down
98 changes: 58 additions & 40 deletions benches/benches/db_lookup_times.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,114 @@
use crate::db_lookup_times_utils::{
matrix::matrix,
utils::{
get_full_block,
get_random_block_height,
multi_get_block,
open_db,
open_raw_rocksdb,
},
};
use criterion::{
criterion_group,
criterion_main,
Criterion,
};
use db_lookup_times_utils::seed::{
seed_compressed_blocks_and_transactions_matrix,
seed_full_block_matrix,
use fuel_core_benches::db_lookup_times_utils::{
matrix::matrix,
seed::{
seed_compressed_blocks_and_transactions_matrix,
seed_full_block_matrix,
},
utils::{
get_random_block_height,
open_rocks_db,
LookupMethod,
Result as DbLookupBenchResult,
},
};
use fuel_core_storage::transactional::AtomicView;
use rand::thread_rng;

mod db_lookup_times_utils;
use fuel_core_benches::utils::ShallowTempDir;
use rand::thread_rng;

pub fn header_and_tx_lookup(c: &mut Criterion) {
let method = "header_and_tx";
pub fn header_and_tx_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
let method = LookupMethod::HeaderAndTx;
let mut rng = thread_rng();

seed_compressed_blocks_and_transactions_matrix(method);
let mut group = c.benchmark_group(method);
let mut group = c.benchmark_group(method.as_ref());

for (block_count, tx_count) in matrix() {
let database = open_db(block_count, tx_count, method);
let view = database.latest_view().unwrap();
let db_path = ShallowTempDir::new();
let mut database = open_rocks_db(db_path.path())?;
seed_compressed_blocks_and_transactions_matrix(
&mut database,
block_count,
tx_count,
)?;

group.bench_function(format!("{block_count}/{tx_count}"), |b| {
b.iter(|| {
let height = get_random_block_height(&mut rng, block_count);
let block = view.get_full_block(&height);
let block = method.get_block(&database, height);
assert!(block.is_ok());
assert!(block.unwrap().is_some());
});
});
}

group.finish();
Ok(())
}

pub fn multi_get_lookup(c: &mut Criterion) {
let method = "multi_get";
pub fn multi_get_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
let method = LookupMethod::MultiGet;
let mut rng = thread_rng();

seed_compressed_blocks_and_transactions_matrix(method);
let mut group = c.benchmark_group(method);
let mut group = c.benchmark_group(method.as_ref());

for (block_count, tx_count) in matrix() {
let database = open_raw_rocksdb(block_count, tx_count, method);
let db_path = ShallowTempDir::new();
let mut database = open_rocks_db(db_path.path())?;
seed_compressed_blocks_and_transactions_matrix(
&mut database,
block_count,
tx_count,
)?;

group.bench_function(format!("{block_count}/{tx_count}"), |b| {
b.iter(|| {
let height = get_random_block_height(&mut rng, block_count);
assert!(multi_get_block(&database, height).is_ok());
let block = method.get_block(&database, height);
assert!(block.is_ok());
});
});
}

group.finish();
Ok(())
}

pub fn full_block_lookup(c: &mut Criterion) {
let method = "full_block";
pub fn full_block_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
let method = LookupMethod::FullBlock;
let mut rng = thread_rng();

seed_full_block_matrix();
let mut group = c.benchmark_group(method);
let mut group = c.benchmark_group(method.as_ref());

for (block_count, tx_count) in matrix() {
let database = open_db(block_count, tx_count, method);
let view = database.latest_view().unwrap();
let db_path = ShallowTempDir::new();
let mut database = open_rocks_db(db_path.path())?;
seed_full_block_matrix(&mut database, block_count, tx_count)?;

group.bench_function(format!("{block_count}/{tx_count}"), |b| {
b.iter(|| {
let height = get_random_block_height(&mut rng, block_count);
let full_block = get_full_block(&view, &height);
let full_block = method.get_block(&database, height);
assert!(full_block.is_ok());
assert!(full_block.unwrap().is_some());
});
});
}

group.finish();
Ok(())
}

fn construct_and_run_benchmarks(c: &mut Criterion) {
header_and_tx_lookup(c).unwrap();
multi_get_lookup(c).unwrap();
full_block_lookup(c).unwrap();
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(100_000).measurement_time(std::time::Duration::from_secs(100));
targets = header_and_tx_lookup, multi_get_lookup, full_block_lookup
config = Criterion::default().sample_size(10).measurement_time(std::time::Duration::from_secs(10));
targets = construct_and_run_benchmarks
}
criterion_main!(benches);
10 changes: 0 additions & 10 deletions benches/benches/db_lookup_times_utils/matrix.rs

This file was deleted.

3 changes: 0 additions & 3 deletions benches/benches/db_lookup_times_utils/mod.rs

This file was deleted.

151 changes: 0 additions & 151 deletions benches/benches/db_lookup_times_utils/seed.rs

This file was deleted.

Loading

0 comments on commit 877b52b

Please sign in to comment.