Skip to content
Snippets Groups Projects
lib.rs 36.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 							let start_storage_root = $crate::benchmarking::current_time();
    							$crate::storage_root();
    							let finish_storage_root = $crate::benchmarking::current_time();
    							let elapsed_storage_root = finish_storage_root - start_storage_root;
    
    
    							results.push($crate::BenchmarkResults {
    								components: c.clone(),
    								extrinsic_time: elapsed_extrinsic,
    								storage_root_time: elapsed_storage_root,
    								reads: read_write_count.0,
    								repeat_reads: read_write_count.1,
    								writes: read_write_count.2,
    								repeat_writes: read_write_count.3,
    							});
    
    
    							// Wipe the DB back to the genesis state.
    							$crate::benchmarking::wipe_db();
    						}
    					}
    				}
    				return Ok(results);
    			}
    		}
    	}
    }
    
    
    // This creates a unit test for one benchmark of the main benchmark macro.
    // It runs the benchmark using the `high` and `low` value for each component
    
    // and ensure that everything completes successfully.
    #[macro_export]
    
    #[doc(hidden)]
    macro_rules! impl_benchmark_test {
    
    	(
    		NO_INSTANCE
    		$runtime:ident
    		$pallet:ident
    
    		$name:ident
    
    		$crate::paste::item! {
    			fn [<test_benchmark_ $name>] () -> Result<(), &'static str>
    			{
    				let selected_benchmark = SelectedBenchmark::$name;
    				let components = <
    					SelectedBenchmark as $crate::BenchmarkingSetup<$runtime>
    				>::components(&selected_benchmark);
    
    
    				// assert!(
    				// 	components.len() != 0,
    				// 	"You need to add components to your benchmark!",
    				// );
    
    				for (_, (name, low, high)) in components.iter().enumerate() {
    					// Test only the low and high value, assuming values in the middle won't break
    					for component_value in vec![low, high] {
    						// Select the max value for all the other components.
    						let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
    							.enumerate()
    							.map(|(_, (n, _, h))|
    								if n == name {
    									(*n, *component_value)
    								} else {
    									(*n, *h)
    								}
    							)
    							.collect();
    
    						// Set up the verification state
    						let closure_to_verify = <
    							SelectedBenchmark as $crate::BenchmarkingSetup<$runtime>
    						>::verify(&selected_benchmark, &c)?;
    
    						// Set the block number to at least 1 so events are deposited.
    						if $crate::Zero::is_zero(&frame_system::Module::<$runtime>::block_number()) {
    							frame_system::Module::<$runtime>::set_block_number($crate::One::one());
    
    
    						// Run verification
    						closure_to_verify()?;
    
    						// Reset the state
    						$crate::benchmarking::wipe_db();
    
    		$runtime:ident
    		$pallet:ident
    
    		$name:ident
    
    		$crate::paste::item! {
    			fn [<test_benchmark_ $name>] () -> Result<(), &'static str>
    			{
    				let selected_benchmark = SelectedBenchmark::$name;
    				let components = <
    					SelectedBenchmark as $crate::BenchmarkingSetupInstance<$runtime, $instance>
    				>::components(&selected_benchmark);
    
    				for (_, (name, low, high)) in components.iter().enumerate() {
    					// Test only the low and high value, assuming values in the middle won't break
    					for component_value in vec![low, high] {
    						// Select the max value for all the other components.
    						let c: Vec<($crate::BenchmarkParameter, u32)> = components.iter()
    							.enumerate()
    							.map(|(_, (n, _, h))|
    								if n == name {
    									(*n, *component_value)
    								} else {
    									(*n, *h)
    								}
    							)
    							.collect();
    
    						// Set up the verification state
    						let closure_to_verify = <
    							SelectedBenchmark as $crate::BenchmarkingSetupInstance<$runtime, $instance>
    						>::verify(&selected_benchmark, &c)?;
    
    						// Set the block number to at least 1 so events are deposited.
    						if $crate::Zero::is_zero(&frame_system::Module::<$runtime>::block_number()) {
    							frame_system::Module::<$runtime>::set_block_number($crate::One::one());
    
    
    						// Run verification
    						closure_to_verify()?;
    
    						// Reset the state
    						$crate::benchmarking::wipe_db();
    
    	};
    }
    
    /// This macro adds pallet benchmarks to a `Vec<BenchmarkBatch>` object.
    ///
    /// First create an object that holds in the input parameters for the benchmark:
    ///
    /// ```ignore
    /// let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat);
    /// ```
    ///
    /// Then define a mutable local variable to hold your `BenchmarkBatch` object:
    ///
    /// ```ignore
    /// let mut batches = Vec::<BenchmarkBatch>::new();
    /// ````
    ///
    
    /// Then add the pallets you want to benchmark to this object, including the
    /// string you want to use target a particular pallet:
    
    ///
    /// ```ignore
    /// add_benchmark!(params, batches, b"balances", Balances);
    /// add_benchmark!(params, batches, b"identity", Identity);
    /// add_benchmark!(params, batches, b"session", SessionBench::<Runtime>);
    /// ...
    /// ```
    ///
    /// At the end of `dispatch_benchmark`, you should return this batches object.
    #[macro_export]
    macro_rules! add_benchmark {
    	( $params:ident, $batches:ident, $name:literal, $( $location:tt )* ) => (
    
    		let (pallet, benchmark, lowest_range_values, highest_range_values, steps, repeat, whitelist) = $params;
    
    		if &pallet[..] == &$name[..] || &pallet[..] == &b"*"[..] {
    			if &pallet[..] == &b"*"[..] || &benchmark[..] == &b"*"[..] {
    				for benchmark in $( $location )*::Benchmark::benchmarks().into_iter() {
    					$batches.push($crate::BenchmarkBatch {
    						results: $( $location )*::Benchmark::run_benchmark(
    							benchmark,
    							&lowest_range_values[..],
    							&highest_range_values[..],
    							&steps[..],
    							repeat,
    
    							whitelist,
    
    						)?,
    						pallet: $name.to_vec(),
    						benchmark: benchmark.to_vec(),
    					});
    				}
    			} else {
    				$batches.push($crate::BenchmarkBatch {
    					results: $( $location )*::Benchmark::run_benchmark(
    						&benchmark[..],
    						&lowest_range_values[..],
    						&highest_range_values[..],
    						&steps[..],
    						repeat,
    
    						whitelist,
    
    					)?,
    					pallet: $name.to_vec(),
    					benchmark: benchmark.clone(),
    				});
    			}
    		}
    	)
    }