Skip to content

Commit 877b52b

Browse files
rymncxgreenx
andauthored
fix(db_lookup_times): rework core logic of benchmark (#2159)
>[!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]>
1 parent 9487bf2 commit 877b52b

File tree

17 files changed

+554
-365
lines changed

17 files changed

+554
-365
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ criterion = { version = "0.5", features = [
1616
] }
1717
ctrlc = "3.2.3"
1818
ed25519-dalek = { version = "2.0", features = ["rand_core"] }
19+
enum-iterator = { workspace = true }
1920
ethnum = "1.3"
2021
fuel-core = { path = "../crates/fuel-core", default-features = false, features = [
2122
"smt",
@@ -30,6 +31,8 @@ fuel-core-sync = { path = "./../crates/services/sync", features = [
3031
] }
3132
fuel-core-types = { path = "./../crates/types", features = ["test-helpers"] }
3233
futures = { workspace = true }
34+
itertools = { workspace = true }
35+
num_enum = { workspace = true }
3336
p256 = { version = "0.13", default-features = false, features = [
3437
"digest",
3538
"ecdsa",
@@ -41,6 +44,8 @@ rand = { workspace = true }
4144
serde = { workspace = true, features = ["derive"] }
4245
serde_json = { workspace = true }
4346
serde_yaml = "0.9.13"
47+
strum = { workspace = true }
48+
strum_macros = { workspace = true }
4449
test-helpers = { path = "../tests/test-helpers" }
4550
tikv-jemallocator = { workspace = true }
4651
tokio = { workspace = true, features = ["full"] }

benches/benches/db_lookup_times.rs

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,114 @@
1-
use crate::db_lookup_times_utils::{
2-
matrix::matrix,
3-
utils::{
4-
get_full_block,
5-
get_random_block_height,
6-
multi_get_block,
7-
open_db,
8-
open_raw_rocksdb,
9-
},
10-
};
111
use criterion::{
122
criterion_group,
133
criterion_main,
144
Criterion,
155
};
16-
use db_lookup_times_utils::seed::{
17-
seed_compressed_blocks_and_transactions_matrix,
18-
seed_full_block_matrix,
6+
use fuel_core_benches::db_lookup_times_utils::{
7+
matrix::matrix,
8+
seed::{
9+
seed_compressed_blocks_and_transactions_matrix,
10+
seed_full_block_matrix,
11+
},
12+
utils::{
13+
get_random_block_height,
14+
open_rocks_db,
15+
LookupMethod,
16+
Result as DbLookupBenchResult,
17+
},
1918
};
20-
use fuel_core_storage::transactional::AtomicView;
21-
use rand::thread_rng;
2219

23-
mod db_lookup_times_utils;
20+
use fuel_core_benches::utils::ShallowTempDir;
21+
use rand::thread_rng;
2422

25-
pub fn header_and_tx_lookup(c: &mut Criterion) {
26-
let method = "header_and_tx";
23+
pub fn header_and_tx_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
24+
let method = LookupMethod::HeaderAndTx;
2725
let mut rng = thread_rng();
2826

29-
seed_compressed_blocks_and_transactions_matrix(method);
30-
let mut group = c.benchmark_group(method);
27+
let mut group = c.benchmark_group(method.as_ref());
3128

3229
for (block_count, tx_count) in matrix() {
33-
let database = open_db(block_count, tx_count, method);
34-
let view = database.latest_view().unwrap();
30+
let db_path = ShallowTempDir::new();
31+
let mut database = open_rocks_db(db_path.path())?;
32+
seed_compressed_blocks_and_transactions_matrix(
33+
&mut database,
34+
block_count,
35+
tx_count,
36+
)?;
37+
3538
group.bench_function(format!("{block_count}/{tx_count}"), |b| {
3639
b.iter(|| {
3740
let height = get_random_block_height(&mut rng, block_count);
38-
let block = view.get_full_block(&height);
41+
let block = method.get_block(&database, height);
3942
assert!(block.is_ok());
40-
assert!(block.unwrap().is_some());
4143
});
4244
});
4345
}
4446

4547
group.finish();
48+
Ok(())
4649
}
4750

48-
pub fn multi_get_lookup(c: &mut Criterion) {
49-
let method = "multi_get";
51+
pub fn multi_get_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
52+
let method = LookupMethod::MultiGet;
5053
let mut rng = thread_rng();
5154

52-
seed_compressed_blocks_and_transactions_matrix(method);
53-
let mut group = c.benchmark_group(method);
55+
let mut group = c.benchmark_group(method.as_ref());
5456

5557
for (block_count, tx_count) in matrix() {
56-
let database = open_raw_rocksdb(block_count, tx_count, method);
58+
let db_path = ShallowTempDir::new();
59+
let mut database = open_rocks_db(db_path.path())?;
60+
seed_compressed_blocks_and_transactions_matrix(
61+
&mut database,
62+
block_count,
63+
tx_count,
64+
)?;
65+
5766
group.bench_function(format!("{block_count}/{tx_count}"), |b| {
5867
b.iter(|| {
5968
let height = get_random_block_height(&mut rng, block_count);
60-
assert!(multi_get_block(&database, height).is_ok());
69+
let block = method.get_block(&database, height);
70+
assert!(block.is_ok());
6171
});
6272
});
6373
}
6474

6575
group.finish();
76+
Ok(())
6677
}
6778

68-
pub fn full_block_lookup(c: &mut Criterion) {
69-
let method = "full_block";
79+
pub fn full_block_lookup(c: &mut Criterion) -> DbLookupBenchResult<()> {
80+
let method = LookupMethod::FullBlock;
7081
let mut rng = thread_rng();
7182

72-
seed_full_block_matrix();
73-
let mut group = c.benchmark_group(method);
83+
let mut group = c.benchmark_group(method.as_ref());
7484

7585
for (block_count, tx_count) in matrix() {
76-
let database = open_db(block_count, tx_count, method);
77-
let view = database.latest_view().unwrap();
86+
let db_path = ShallowTempDir::new();
87+
let mut database = open_rocks_db(db_path.path())?;
88+
seed_full_block_matrix(&mut database, block_count, tx_count)?;
89+
7890
group.bench_function(format!("{block_count}/{tx_count}"), |b| {
7991
b.iter(|| {
8092
let height = get_random_block_height(&mut rng, block_count);
81-
let full_block = get_full_block(&view, &height);
93+
let full_block = method.get_block(&database, height);
8294
assert!(full_block.is_ok());
83-
assert!(full_block.unwrap().is_some());
8495
});
8596
});
8697
}
8798

8899
group.finish();
100+
Ok(())
101+
}
102+
103+
fn construct_and_run_benchmarks(c: &mut Criterion) {
104+
header_and_tx_lookup(c).unwrap();
105+
multi_get_lookup(c).unwrap();
106+
full_block_lookup(c).unwrap();
89107
}
90108

91109
criterion_group! {
92110
name = benches;
93-
config = Criterion::default().sample_size(100_000).measurement_time(std::time::Duration::from_secs(100));
94-
targets = header_and_tx_lookup, multi_get_lookup, full_block_lookup
111+
config = Criterion::default().sample_size(10).measurement_time(std::time::Duration::from_secs(10));
112+
targets = construct_and_run_benchmarks
95113
}
96114
criterion_main!(benches);

benches/benches/db_lookup_times_utils/matrix.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

benches/benches/db_lookup_times_utils/mod.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

benches/benches/db_lookup_times_utils/seed.rs

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)