Skip to content

Commit

Permalink
Add a skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiEres committed Apr 11, 2024
1 parent 4208c5e commit 42f2a55
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
11 changes: 9 additions & 2 deletions polkadot/node/subsystem-bench/src/cli/subsystem-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use clap::Parser;
use color_eyre::eyre;
use colored::Colorize;
use polkadot_subsystem_bench::{approval, availability, configuration};
use polkadot_subsystem_bench::{approval, availability, configuration, statement};
use pyroscope::PyroscopeAgent;
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
use serde::{Deserialize, Serialize};
Expand All @@ -40,6 +40,7 @@ pub enum TestObjective {
DataAvailabilityWrite,
/// Benchmark the approval-voting and approval-distribution subsystems.
ApprovalVoting(approval::ApprovalsOptions),
// TODO
StatementDistribution,
}

Expand Down Expand Up @@ -173,7 +174,13 @@ impl BenchCli {
))
},
TestObjective::StatementDistribution => {
todo!();
let state = statement::TestState::new(&test_config);
let mut env = statement::prepare_test(&state, true);
env.runtime().block_on(statement::benchmark_statement_distribution(
&benchmark_name,
&mut env,
&state,
))
},
};
println!("{}", usage);
Expand Down
1 change: 1 addition & 0 deletions polkadot/node/subsystem-bench/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ pub(crate) mod environment;
pub(crate) mod keyring;
pub(crate) mod mock;
pub(crate) mod network;
pub mod statement;
pub mod usage;
pub mod utils;
97 changes: 97 additions & 0 deletions polkadot/node/subsystem-bench/src/lib/statement/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use colored::Colorize;
use polkadot_overseer::{
Handle as OverseerHandle, MetricsTrait, Overseer, OverseerConnector, OverseerMetrics, SpawnGlue,
};
use sc_service::SpawnTaskHandle;
use std::time::Instant;
pub use test_state::TestState;

const LOG_TARGET: &str = "subsystem-bench::availability";

use crate::{
dummy_builder,
environment::{TestEnvironment, TestEnvironmentDependencies},
mock::AlwaysSupportsParachains,
network::new_network,
usage::BenchmarkUsage,
};

mod test_state;

/// Helper function to build an overseer with the real implementation for `ApprovalDistribution` and
/// `ApprovalVoting` subsystems and mock subsystems for all others.
fn build_overseer(
dependencies: &TestEnvironmentDependencies,
) -> (Overseer<SpawnGlue<SpawnTaskHandle>, AlwaysSupportsParachains>, OverseerHandle) {
let overseer_connector = OverseerConnector::with_event_capacity(64000);
let overseer_metrics = OverseerMetrics::try_register(&dependencies.registry).unwrap();
let spawn_task_handle = dependencies.task_manager.spawn_handle();
let dummy = dummy_builder!(spawn_task_handle, overseer_metrics);
let (overseer, raw_handle) =
dummy.build_with_connector(overseer_connector).expect("Should not fail");
let overseer_handle = OverseerHandle::new(raw_handle);

(overseer, overseer_handle)
}

pub fn prepare_test(state: &TestState, with_prometheus_endpoint: bool) -> TestEnvironment {
let dependencies = TestEnvironmentDependencies::default();
let (network, _network_interface, _network_receiver) =
new_network(&state.config, &dependencies, &state.test_authorities, vec![]);
let (overseer, overseer_handle) = build_overseer(&dependencies);

TestEnvironment::new(
dependencies,
state.config.clone(),
network,
overseer,
overseer_handle,
state.test_authorities.clone(),
with_prometheus_endpoint,
)
}

pub async fn benchmark_statement_distribution(
benchmark_name: &str,
env: &mut TestEnvironment,
state: &TestState,
) -> BenchmarkUsage {
let config = env.config().clone();

env.metrics().set_n_validators(config.n_validators);
env.metrics().set_n_cores(config.n_cores);

let test_start = Instant::now();
for block_info in state.block_infos.iter() {
let block_num = block_info.number as usize;
gum::info!(target: LOG_TARGET, "Current block {}/{}", block_num, config.num_blocks);
env.metrics().set_current_block(block_num);
env.import_block(block_info.clone()).await;
}

let duration: u128 = test_start.elapsed().as_millis();
gum::info!(target: LOG_TARGET, "All blocks processed in {}", format!("{:?}ms", duration).cyan());
gum::info!(target: LOG_TARGET,
"Avg block time: {}",
format!("{} ms", test_start.elapsed().as_millis() / env.config().num_blocks as u128).red()
);

env.stop().await;
env.collect_resource_usage(benchmark_name, &["statement-distribution"])
}
45 changes: 45 additions & 0 deletions polkadot/node/subsystem-bench/src/lib/statement/test_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use polkadot_node_subsystem_test_helpers::mock::new_block_import_info;
use polkadot_overseer::BlockInfo;
use polkadot_primitives::{BlockNumber, Hash};

use crate::configuration::{TestAuthorities, TestConfiguration};

pub struct TestState {
// Full test config
pub config: TestConfiguration,
// Authority keys for the network emulation.
pub test_authorities: TestAuthorities,
// Relay chain block infos
pub block_infos: Vec<BlockInfo>,
}

impl TestState {
pub fn new(config: &TestConfiguration) -> Self {
Self {
config: config.clone(),
test_authorities: config.generate_authorities(),
block_infos: (1..=config.num_blocks)
.map(|block_num| {
let relay_block_hash = Hash::repeat_byte(block_num as u8);
new_block_import_info(relay_block_hash, block_num as BlockNumber)
})
.collect(),
}
}
}

0 comments on commit 42f2a55

Please sign in to comment.