|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Polkadot. |
| 3 | + |
| 4 | +// Polkadot is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Polkadot is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Benchmarks for preparation through the host. We use a real PVF to get realistic results. |
| 18 | +
|
| 19 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion, SamplingMode}; |
| 20 | +use parity_scale_codec::Encode; |
| 21 | +use polkadot_node_core_pvf::{ |
| 22 | + start, testing, Config, Metrics, PrepareError, PrepareJobKind, PrepareStats, PvfPrepData, |
| 23 | + ValidationError, ValidationHost, |
| 24 | +}; |
| 25 | +use polkadot_parachain_primitives::primitives::{BlockData, ValidationParams, ValidationResult}; |
| 26 | +use polkadot_primitives::ExecutorParams; |
| 27 | +use rococo_runtime::WASM_BINARY; |
| 28 | +use std::time::Duration; |
| 29 | +use tokio::{runtime::Handle, sync::Mutex}; |
| 30 | + |
| 31 | +const TEST_EXECUTION_TIMEOUT: Duration = Duration::from_secs(3); |
| 32 | +const TEST_PREPARATION_TIMEOUT: Duration = Duration::from_secs(30); |
| 33 | + |
| 34 | +struct TestHost { |
| 35 | + host: Mutex<ValidationHost>, |
| 36 | +} |
| 37 | + |
| 38 | +impl TestHost { |
| 39 | + fn new_with_config<F>(handle: &Handle, f: F) -> Self |
| 40 | + where |
| 41 | + F: FnOnce(&mut Config), |
| 42 | + { |
| 43 | + let (prepare_worker_path, execute_worker_path) = testing::get_and_check_worker_paths(); |
| 44 | + |
| 45 | + let cache_dir = tempfile::tempdir().unwrap(); |
| 46 | + let mut config = Config::new( |
| 47 | + cache_dir.path().to_owned(), |
| 48 | + None, |
| 49 | + prepare_worker_path, |
| 50 | + execute_worker_path, |
| 51 | + ); |
| 52 | + f(&mut config); |
| 53 | + let (host, task) = start(config, Metrics::default()); |
| 54 | + let _ = handle.spawn(task); |
| 55 | + Self { host: Mutex::new(host) } |
| 56 | + } |
| 57 | + |
| 58 | + async fn precheck_pvf( |
| 59 | + &self, |
| 60 | + code: &[u8], |
| 61 | + executor_params: ExecutorParams, |
| 62 | + ) -> Result<PrepareStats, PrepareError> { |
| 63 | + let (result_tx, result_rx) = futures::channel::oneshot::channel(); |
| 64 | + |
| 65 | + let code = sp_maybe_compressed_blob::decompress(code, 16 * 1024 * 1024) |
| 66 | + .expect("Compression works"); |
| 67 | + |
| 68 | + self.host |
| 69 | + .lock() |
| 70 | + .await |
| 71 | + .precheck_pvf( |
| 72 | + PvfPrepData::from_code( |
| 73 | + code.into(), |
| 74 | + executor_params, |
| 75 | + TEST_PREPARATION_TIMEOUT, |
| 76 | + PrepareJobKind::Prechecking, |
| 77 | + ), |
| 78 | + result_tx, |
| 79 | + ) |
| 80 | + .await |
| 81 | + .unwrap(); |
| 82 | + result_rx.await.unwrap() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn host_prepare_rococo_runtime(c: &mut Criterion) { |
| 87 | + polkadot_node_core_pvf_common::sp_tracing::try_init_simple(); |
| 88 | + |
| 89 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 90 | + |
| 91 | + let blob = WASM_BINARY.expect("You need to build the WASM binaries to run the tests!"); |
| 92 | + let pvf = match sp_maybe_compressed_blob::decompress(&blob, 64 * 1024 * 1024) { |
| 93 | + Ok(code) => PvfPrepData::from_code( |
| 94 | + code.into_owned(), |
| 95 | + ExecutorParams::default(), |
| 96 | + Duration::from_secs(360), |
| 97 | + PrepareJobKind::Compilation, |
| 98 | + ), |
| 99 | + Err(e) => { |
| 100 | + panic!("Cannot decompress blob: {:?}", e); |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + let mut group = c.benchmark_group("prepare rococo"); |
| 105 | + group.sampling_mode(SamplingMode::Flat); |
| 106 | + group.sample_size(20); |
| 107 | + group.measurement_time(Duration::from_secs(240)); |
| 108 | + group.bench_function("host: prepare Rococo runtime", |b| { |
| 109 | + b.to_async(&rt).iter_batched( |
| 110 | + || { |
| 111 | + ( |
| 112 | + TestHost::new_with_config(rt.handle(), |cfg| { |
| 113 | + cfg.prepare_workers_hard_max_num = 1; |
| 114 | + }), |
| 115 | + pvf.clone().code(), |
| 116 | + ) |
| 117 | + }, |
| 118 | + |(host, pvf_code)| async move { |
| 119 | + // `PvfPrepData` is designed to be cheap to clone, so cloning shouldn't affect the |
| 120 | + // benchmark accuracy. |
| 121 | + let _stats = host.precheck_pvf(&pvf_code, Default::default()).await.unwrap(); |
| 122 | + }, |
| 123 | + BatchSize::SmallInput, |
| 124 | + ) |
| 125 | + }); |
| 126 | + group.finish(); |
| 127 | +} |
| 128 | + |
| 129 | +criterion_group!(prepare, host_prepare_rococo_runtime); |
| 130 | +criterion_main!(prepare); |
0 commit comments