|
| 1 | +// Copyright 2017-2021 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 | +use log::info; |
| 18 | +use polkadot_node_core_pvf::sp_maybe_compressed_blob; |
| 19 | +use polkadot_performance_test::{ |
| 20 | + measure_erasure_coding, measure_pvf_prepare, PerfCheckError, ERASURE_CODING_N_VALIDATORS, |
| 21 | + ERASURE_CODING_TIME_LIMIT, PVF_PREPARE_TIME_LIMIT, VALIDATION_CODE_BOMB_LIMIT, |
| 22 | +}; |
| 23 | +use std::time::Duration; |
| 24 | + |
| 25 | +pub fn host_perf_check() -> Result<(), PerfCheckError> { |
| 26 | + let wasm_code = |
| 27 | + polkadot_performance_test::WASM_BINARY.ok_or(PerfCheckError::WasmBinaryMissing)?; |
| 28 | + |
| 29 | + // Decompress the code before running checks. |
| 30 | + let code = sp_maybe_compressed_blob::decompress(wasm_code, VALIDATION_CODE_BOMB_LIMIT) |
| 31 | + .or(Err(PerfCheckError::CodeDecompressionFailed))?; |
| 32 | + |
| 33 | + info!("Running the performance checks..."); |
| 34 | + |
| 35 | + perf_check("PVF-prepare", PVF_PREPARE_TIME_LIMIT, || measure_pvf_prepare(code.as_ref()))?; |
| 36 | + |
| 37 | + perf_check("Erasure-coding", ERASURE_CODING_TIME_LIMIT, || { |
| 38 | + measure_erasure_coding(ERASURE_CODING_N_VALIDATORS, code.as_ref()) |
| 39 | + })?; |
| 40 | + |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +fn green_threshold(duration: Duration) -> Duration { |
| 45 | + duration * 4 / 5 |
| 46 | +} |
| 47 | + |
| 48 | +fn perf_check( |
| 49 | + test_name: &str, |
| 50 | + time_limit: Duration, |
| 51 | + test: impl Fn() -> Result<Duration, PerfCheckError>, |
| 52 | +) -> Result<(), PerfCheckError> { |
| 53 | + let elapsed = test()?; |
| 54 | + |
| 55 | + if elapsed < green_threshold(time_limit) { |
| 56 | + info!("🟢 {} performance check passed, elapsed: {:?}", test_name, elapsed); |
| 57 | + Ok(()) |
| 58 | + } else if elapsed <= time_limit { |
| 59 | + info!( |
| 60 | + "🟡 {} performance check passed, {:?} limit almost exceeded, elapsed: {:?}", |
| 61 | + test_name, time_limit, elapsed |
| 62 | + ); |
| 63 | + Ok(()) |
| 64 | + } else { |
| 65 | + Err(PerfCheckError::TimeOut { elapsed, limit: time_limit }) |
| 66 | + } |
| 67 | +} |
0 commit comments