Skip to content

Commit c31f8ac

Browse files
ggwpezgrishasobol
authored andcommitted
Storage benchmarking (paritytech#10897)
* WIP Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * WIP: DB benchmarking Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * WIP Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * WIP Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Simplify code Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove old files Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove old files Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Minimize changes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add license Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove dependencies Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Extend template Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Linter Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Linter Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Beauty fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove default Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add feature Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove seed Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * CI wakeup Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fmt Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Review fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Adding doc Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Adding doc Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Improve template Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Do not expose columns Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix ColumnId Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Nicer template prints Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Cleanup Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix json path Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Simplify `bench_write` logic Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Invert tx before the second commit Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
1 parent 7e04164 commit c31f8ac

File tree

19 files changed

+917
-11
lines changed

19 files changed

+917
-11
lines changed

Cargo.lock

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

bin/node/cli/src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub enum Subcommand {
4242
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
4343
Benchmark(frame_benchmarking_cli::BenchmarkCmd),
4444

45+
/// Sub command for benchmarking the storage speed.
46+
#[clap(name = "benchmark-storage", about = "Benchmark storage speed.")]
47+
BenchmarkStorage(frame_benchmarking_cli::StorageCmd),
48+
4549
/// Try some command against runtime state.
4650
#[cfg(feature = "try-runtime")]
4751
TryRuntime(try_runtime_cli::TryRuntimeCmd),

bin/node/cli/src/command.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ pub fn run() -> Result<()> {
9595
You can enable it with `--features runtime-benchmarks`."
9696
.into())
9797
},
98+
Some(Subcommand::BenchmarkStorage(cmd)) => {
99+
if !cfg!(feature = "runtime-benchmarks") {
100+
return Err("Benchmarking wasn't enabled when building the node. \
101+
You can enable it with `--features runtime-benchmarks`."
102+
.into())
103+
}
104+
105+
let runner = cli.create_runner(cmd)?;
106+
runner.async_run(|config| {
107+
let PartialComponents { client, task_manager, backend, .. } = new_partial(&config)?;
108+
let db = backend.expose_db();
109+
let storage = backend.expose_storage();
110+
111+
Ok((cmd.run(config, client, db, storage), task_manager))
112+
})
113+
},
98114
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
99115
Some(Subcommand::Sign(cmd)) => cmd.run(),
100116
Some(Subcommand::Verify(cmd)) => cmd.run(),

client/cli/src/arg_enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl Into<sc_service::config::RpcMethods> for RpcMethods {
194194
}
195195

196196
/// Database backend
197-
#[derive(Debug, Clone, Copy)]
197+
#[derive(Debug, Clone, PartialEq, Copy)]
198198
pub enum Database {
199199
/// Facebooks RocksDB
200200
RocksDb,

client/cli/src/params/database_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use clap::Args;
2121
use sc_service::TransactionStorageMode;
2222

2323
/// Parameters for block import.
24-
#[derive(Debug, Clone, Args)]
24+
#[derive(Debug, Clone, PartialEq, Args)]
2525
pub struct DatabaseParams {
2626
/// Select database backend to use.
2727
#[clap(

client/cli/src/params/pruning_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use clap::Args;
2121
use sc_service::{KeepBlocks, PruningMode, Role};
2222

2323
/// Parameters to define the pruning mode
24-
#[derive(Debug, Clone, Args)]
24+
#[derive(Debug, Clone, PartialEq, Args)]
2525
pub struct PruningParams {
2626
/// Specify the state pruning mode, a number of blocks to keep or 'archive'.
2727
///

client/cli/src/params/shared_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use sc_service::config::BasePath;
2222
use std::path::PathBuf;
2323

2424
/// Shared parameters used by all `CoreParams`.
25-
#[derive(Debug, Clone, Args)]
25+
#[derive(Debug, Clone, PartialEq, Args)]
2626
pub struct SharedParams {
2727
/// Specify the chain specification.
2828
///

client/db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ tempfile = "3"
4545
[features]
4646
default = []
4747
test-helpers = []
48+
runtime-benchmarks = []
4849
with-kvdb-rocksdb = ["kvdb-rocksdb"]
4950
with-parity-db = ["parity-db"]

client/db/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const DEFAULT_CHILD_RATIO: (usize, usize) = (1, 10);
106106
pub type DbState<B> =
107107
sp_state_machine::TrieBackend<Arc<dyn sp_state_machine::Storage<HashFor<B>>>, HashFor<B>>;
108108

109-
const DB_HASH_LEN: usize = 32;
109+
/// Length of a [`DbHash`].
110+
pub const DB_HASH_LEN: usize = 32;
110111
/// Hash type that this backend uses for the database.
111112
pub type DbHash = sp_core::H256;
112113

@@ -1050,6 +1051,23 @@ impl<Block: BlockT> Backend<Block> {
10501051
Self::new(db_setting, canonicalization_delay).expect("failed to create test-db")
10511052
}
10521053

1054+
/// Expose the Database that is used by this backend.
1055+
/// The second argument is the Column that stores the State.
1056+
///
1057+
/// Should only be needed for benchmarking.
1058+
#[cfg(any(feature = "runtime-benchmarks"))]
1059+
pub fn expose_db(&self) -> (Arc<dyn sp_database::Database<DbHash>>, sp_database::ColumnId) {
1060+
(self.storage.db.clone(), columns::STATE)
1061+
}
1062+
1063+
/// Expose the Storage that is used by this backend.
1064+
///
1065+
/// Should only be needed for benchmarking.
1066+
#[cfg(any(feature = "runtime-benchmarks"))]
1067+
pub fn expose_storage(&self) -> Arc<dyn sp_state_machine::Storage<HashFor<Block>>> {
1068+
self.storage.clone()
1069+
}
1070+
10531071
fn from_database(
10541072
db: Arc<dyn Database<DbHash>>,
10551073
canonicalization_delay: u64,

utils/frame/benchmarking-cli/Cargo.toml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ frame-benchmarking = { version = "4.0.0-dev", path = "../../../frame/benchmarkin
1717
frame-support = { version = "4.0.0-dev", path = "../../../frame/support" }
1818
sp-core = { version = "5.0.0", path = "../../../primitives/core" }
1919
sc-service = { version = "0.10.0-dev", default-features = false, path = "../../../client/service" }
20+
sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" }
2021
sc-cli = { version = "0.10.0-dev", path = "../../../client/cli" }
2122
sc-client-db = { version = "0.10.0-dev", path = "../../../client/db" }
2223
sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
24+
25+
sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" }
2326
sp-externalities = { version = "0.11.0", path = "../../../primitives/externalities" }
27+
sp-database = { version = "4.0.0-dev", path = "../../../primitives/database" }
28+
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
2429
sp-keystore = { version = "0.11.0", path = "../../../primitives/keystore" }
30+
sp-storage = { version = "5.0.0", path = "../../../primitives/storage" }
2531
sp-runtime = { version = "5.0.0", path = "../../../primitives/runtime" }
32+
sp-std = { version = "4.0.0", default-features = false, path = "../../../primitives/std" }
2633
sp-state-machine = { version = "0.11.0", path = "../../../primitives/state-machine" }
34+
sp-trie = { version = "5.0.0", path = "../../../primitives/trie" }
2735
codec = { version = "3.0.0", package = "parity-scale-codec" }
2836
clap = { version = "3.0", features = ["derive"] }
2937
chrono = "0.4"
@@ -33,7 +41,14 @@ handlebars = "4.1.6"
3341
Inflector = "0.11.4"
3442
linked-hash-map = "0.5.4"
3543
log = "0.4.8"
44+
itertools = "0.10.3"
45+
serde_nanos = "0.1.2"
46+
kvdb = "0.11.0"
47+
hash-db = "0.15.2"
48+
hex = "0.4.3"
49+
memory-db = "0.29.0"
50+
rand = { version = "0.8.4", features = ["small_rng"] }
3651

3752
[features]
38-
default = ["db"]
53+
default = ["db", "sc-client-db/runtime-benchmarks"]
3954
db = ["sc-client-db/with-kvdb-rocksdb", "sc-client-db/with-parity-db"]

0 commit comments

Comments
 (0)