|
| 1 | +// This file is part of Cumulus. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
| 19 | + |
| 20 | +use sc_client_api::UsageProvider; |
| 21 | +use sp_api::{Core, ProvideRuntimeApi}; |
| 22 | +use sp_arithmetic::Perbill; |
| 23 | + |
| 24 | +use core::time::Duration; |
| 25 | +use cumulus_primitives_core::ParaId; |
| 26 | + |
| 27 | +use sc_block_builder::{BlockBuilderProvider, RecordProof}; |
| 28 | +use sp_keyring::Sr25519Keyring::Alice; |
| 29 | + |
| 30 | +use cumulus_test_service::bench_utils as utils; |
| 31 | + |
| 32 | +fn benchmark_block_import(c: &mut Criterion) { |
| 33 | + sp_tracing::try_init_simple(); |
| 34 | + |
| 35 | + let runtime = tokio::runtime::Runtime::new().expect("creating tokio runtime doesn't fail; qed"); |
| 36 | + let para_id = ParaId::from(100); |
| 37 | + let tokio_handle = runtime.handle(); |
| 38 | + |
| 39 | + let alice = runtime.block_on( |
| 40 | + cumulus_test_service::TestNodeBuilder::new(para_id, tokio_handle.clone(), Alice).build(), |
| 41 | + ); |
| 42 | + let client = alice.client; |
| 43 | + |
| 44 | + let mut group = c.benchmark_group("Block import"); |
| 45 | + group.sample_size(20); |
| 46 | + group.measurement_time(Duration::from_secs(120)); |
| 47 | + |
| 48 | + let mut initialize_glutton_pallet = true; |
| 49 | + for (compute_percent, storage_percent) in &[ |
| 50 | + (Perbill::from_percent(100), Perbill::from_percent(0)), |
| 51 | + (Perbill::from_percent(100), Perbill::from_percent(100)), |
| 52 | + ] { |
| 53 | + let block = utils::set_glutton_parameters( |
| 54 | + &client, |
| 55 | + initialize_glutton_pallet, |
| 56 | + compute_percent, |
| 57 | + storage_percent, |
| 58 | + ); |
| 59 | + initialize_glutton_pallet = false; |
| 60 | + |
| 61 | + runtime.block_on(utils::import_block(&client, &block, false)); |
| 62 | + |
| 63 | + // Build the block we will use for benchmarking |
| 64 | + let parent_hash = client.usage_info().chain.best_hash; |
| 65 | + let parent_header = client.header(parent_hash).expect("Just fetched this hash.").unwrap(); |
| 66 | + let mut block_builder = |
| 67 | + client.new_block_at(parent_hash, Default::default(), RecordProof::No).unwrap(); |
| 68 | + block_builder |
| 69 | + .push(utils::extrinsic_set_validation_data(parent_header.clone()).clone()) |
| 70 | + .unwrap(); |
| 71 | + block_builder.push(utils::extrinsic_set_time(&client)).unwrap(); |
| 72 | + let benchmark_block = block_builder.build().unwrap(); |
| 73 | + |
| 74 | + group.bench_function( |
| 75 | + format!( |
| 76 | + "(compute = {:?}, storage = {:?}) block import", |
| 77 | + compute_percent, storage_percent |
| 78 | + ), |
| 79 | + |b| { |
| 80 | + b.iter_batched( |
| 81 | + || benchmark_block.block.clone(), |
| 82 | + |block| { |
| 83 | + client.runtime_api().execute_block(parent_hash, block).unwrap(); |
| 84 | + }, |
| 85 | + BatchSize::SmallInput, |
| 86 | + ) |
| 87 | + }, |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +criterion_group!(benches, benchmark_block_import); |
| 93 | +criterion_main!(benches); |
0 commit comments