@@ -7,16 +7,22 @@ mod tests;
7
7
8
8
pub use frame_benchmarking:: {
9
9
benchmarking, whitelisted_caller, BenchmarkBatch , BenchmarkConfig , BenchmarkParameter , BenchmarkResults ,
10
- Benchmarking , BenchmarkingSetup , TrackedStorageKey ,
10
+ Benchmarking , BenchmarkingSetup ,
11
11
} ;
12
12
#[ cfg( feature = "std" ) ]
13
13
pub use frame_benchmarking:: { Analysis , BenchmarkSelector } ;
14
+ #[ doc( hidden) ]
14
15
pub use frame_support;
16
+ #[ doc( hidden) ]
15
17
pub use paste;
16
18
#[ doc( hidden) ]
17
19
pub use sp_io:: storage:: root as storage_root;
20
+ #[ doc( hidden) ]
18
21
pub use sp_runtime:: traits:: Zero ;
19
-
22
+ #[ doc( hidden) ]
23
+ pub use sp_std:: { self , boxed:: Box , prelude:: Vec , vec} ;
24
+ #[ doc( hidden) ]
25
+ pub use sp_storage:: TrackedStorageKey ;
20
26
/// Construct pallet benchmarks for weighing dispatchables.
21
27
///
22
28
/// Works around the idea of complexity parameters, named by a single letter
@@ -551,8 +557,8 @@ macro_rules! benchmark_backend {
551
557
struct $name;
552
558
#[ allow( unused_variables) ]
553
559
impl $crate:: BenchmarkingSetup <$runtime $( , $instance) ?> for $name {
554
- fn components( & self ) -> Vec <( $crate:: BenchmarkParameter , u32 , u32 ) > {
555
- vec! [
560
+ fn components( & self ) -> $crate :: Vec <( $crate:: BenchmarkParameter , u32 , u32 ) > {
561
+ $crate :: vec! [
556
562
$(
557
563
( $crate:: BenchmarkParameter :: $param, $param_from, $param_to)
558
564
) ,*
@@ -563,7 +569,7 @@ macro_rules! benchmark_backend {
563
569
& self ,
564
570
components: & [ ( $crate:: BenchmarkParameter , u32 ) ] ,
565
571
verify: bool
566
- ) -> Result <Box <dyn FnOnce ( ) -> Result <( ) , & ' static str >>, & ' static str > {
572
+ ) -> Result <$crate :: Box <dyn FnOnce ( ) -> Result <( ) , & ' static str >>, & ' static str > {
567
573
$(
568
574
// Prepare instance
569
575
let $param = components. iter( )
@@ -577,7 +583,7 @@ macro_rules! benchmark_backend {
577
583
$( $param_instancer ; ) *
578
584
$( $post ) *
579
585
580
- Ok ( Box :: new( move || -> Result <( ) , & ' static str > {
586
+ Ok ( $crate :: Box :: new( move || -> Result <( ) , & ' static str > {
581
587
$eval;
582
588
if verify {
583
589
$postcode;
@@ -620,7 +626,7 @@ macro_rules! selected_benchmark {
620
626
621
627
// Allow us to select a benchmark from the list of available benchmarks.
622
628
impl $crate:: BenchmarkingSetup <$runtime $( , $instance) ?> for SelectedBenchmark {
623
- fn components( & self ) -> Vec <( $crate:: BenchmarkParameter , u32 , u32 ) > {
629
+ fn components( & self ) -> $crate :: Vec <( $crate:: BenchmarkParameter , u32 , u32 ) > {
624
630
match self {
625
631
$(
626
632
Self :: $bench => <
@@ -634,9 +640,7 @@ macro_rules! selected_benchmark {
634
640
& self ,
635
641
components: & [ ( $crate:: BenchmarkParameter , u32 ) ] ,
636
642
verify: bool
637
- )
638
- -> Result <Box <dyn FnOnce ( ) -> Result <( ) , & ' static str >>, & ' static str >
639
- {
643
+ ) -> Result <$crate:: Box <dyn FnOnce ( ) -> Result <( ) , & ' static str >>, & ' static str > {
640
644
match self {
641
645
$(
642
646
Self :: $bench => <
@@ -662,8 +666,8 @@ macro_rules! impl_benchmark {
662
666
pub struct Benchmark ;
663
667
664
668
impl $crate:: Benchmarking <$crate:: BenchmarkResults > for Benchmark {
665
- fn benchmarks( extra: bool ) -> Vec <& ' static [ u8 ] > {
666
- let mut all = vec![ $( stringify!( $name) . as_ref( ) ) ,* ] ;
669
+ fn benchmarks( extra: bool ) -> $crate :: Vec <& ' static [ u8 ] > {
670
+ let mut all = $crate :: vec![ $( stringify!( $name) . as_ref( ) ) ,* ] ;
667
671
if !extra {
668
672
let extra = [ $( stringify!( $name_extra) . as_ref( ) ) ,* ] ;
669
673
all. retain( |x| !extra. contains( x) ) ;
@@ -679,23 +683,23 @@ macro_rules! impl_benchmark {
679
683
repeat: u32 ,
680
684
whitelist: & [ $crate:: TrackedStorageKey ] ,
681
685
verify: bool ,
682
- ) -> Result <Vec <$crate:: BenchmarkResults >, & ' static str > {
686
+ ) -> Result <$crate :: Vec <$crate:: BenchmarkResults >, & ' static str > {
683
687
// Map the input to the selected benchmark.
684
- let extrinsic = sp_std:: str :: from_utf8( extrinsic)
688
+ let extrinsic = $crate :: sp_std:: str :: from_utf8( extrinsic)
685
689
. map_err( |_| "`extrinsic` is not a valid utf8 string!" ) ?;
686
690
let selected_benchmark = match extrinsic {
687
691
$( stringify!( $name) => SelectedBenchmark :: $name, ) *
688
692
_ => return Err ( "Could not find extrinsic." ) ,
689
693
} ;
690
- let mut results: Vec <$crate:: BenchmarkResults > = Vec :: new( ) ;
694
+ let mut results: $crate :: Vec <$crate:: BenchmarkResults > = $crate :: Vec :: new( ) ;
691
695
if repeat == 0 {
692
696
return Ok ( results) ;
693
697
}
694
698
695
699
// Add whitelist to DB including whitelisted caller
696
700
let mut whitelist = whitelist. to_vec( ) ;
697
701
let whitelisted_caller_key =
698
- <frame_system:: Account :: <$runtime> as frame_support:: storage:: StorageMap <_, _>>:: hashed_key_for(
702
+ <frame_system:: Account :: <$runtime> as $crate :: frame_support:: storage:: StorageMap <_, _>>:: hashed_key_for(
699
703
$crate:: whitelisted_caller:: <<$runtime as frame_system:: Config >:: AccountId >( )
700
704
) ;
701
705
whitelist. push( whitelisted_caller_key. into( ) ) ;
@@ -715,7 +719,7 @@ macro_rules! impl_benchmark {
715
719
let repeat_benchmark = |
716
720
repeat: u32 ,
717
721
c: & [ ( $crate:: BenchmarkParameter , u32 ) ] ,
718
- results: & mut Vec <$crate:: BenchmarkResults >,
722
+ results: & mut $crate :: Vec <$crate:: BenchmarkResults >,
719
723
verify: bool ,
720
724
| -> Result <( ) , & ' static str > {
721
725
// Run the benchmark `repeat` times.
@@ -742,7 +746,7 @@ macro_rules! impl_benchmark {
742
746
closure_to_benchmark( ) ?;
743
747
} else {
744
748
// Time the extrinsic logic.
745
- frame_support:: log:: trace!(
749
+ $crate :: frame_support:: log:: trace!(
746
750
target: "benchmark" ,
747
751
"Start Benchmark: {:?}" , c
748
752
) ;
@@ -764,12 +768,12 @@ macro_rules! impl_benchmark {
764
768
765
769
// Commit the changes to get proper write count
766
770
$crate:: benchmarking:: commit_db( ) ;
767
- frame_support:: log:: trace!(
771
+ $crate :: frame_support:: log:: trace!(
768
772
target: "benchmark" ,
769
773
"End Benchmark: {} ns" , elapsed_extrinsic
770
774
) ;
771
775
let read_write_count = $crate:: benchmarking:: read_write_count( ) ;
772
- frame_support:: log:: trace!(
776
+ $crate :: frame_support:: log:: trace!(
773
777
target: "benchmark" ,
774
778
"Read/Write Count {:?}" , read_write_count
775
779
) ;
@@ -802,7 +806,7 @@ macro_rules! impl_benchmark {
802
806
if components. is_empty( ) {
803
807
if verify {
804
808
// If `--verify` is used, run the benchmark once to verify it would complete.
805
- repeat_benchmark( 1 , Default :: default ( ) , & mut Vec :: new( ) , true ) ?;
809
+ repeat_benchmark( 1 , Default :: default ( ) , & mut $crate :: Vec :: new( ) , true ) ?;
806
810
}
807
811
repeat_benchmark( repeat, Default :: default ( ) , & mut results, false ) ?;
808
812
} else {
@@ -829,7 +833,7 @@ macro_rules! impl_benchmark {
829
833
let component_value = lowest + step_size * s;
830
834
831
835
// Select the max value for all the other components.
832
- let c: Vec <( $crate:: BenchmarkParameter , u32 ) > = components. iter( )
836
+ let c: $crate :: Vec <( $crate:: BenchmarkParameter , u32 ) > = components. iter( )
833
837
. enumerate( )
834
838
. map( |( idx, ( n, _, h) ) |
835
839
if n == name {
@@ -842,7 +846,7 @@ macro_rules! impl_benchmark {
842
846
843
847
if verify {
844
848
// If `--verify` is used, run the benchmark once to verify it would complete.
845
- repeat_benchmark( 1 , & c, & mut Vec :: new( ) , true ) ?;
849
+ repeat_benchmark( 1 , & c, & mut $crate :: Vec :: new( ) , true ) ?;
846
850
}
847
851
repeat_benchmark( repeat, & c, & mut results, false ) ?;
848
852
}
@@ -875,7 +879,7 @@ macro_rules! impl_benchmark_test {
875
879
>:: components( & selected_benchmark) ;
876
880
877
881
let execute_benchmark = |
878
- c: Vec <( $crate:: BenchmarkParameter , u32 ) >
882
+ c: $crate :: Vec <( $crate:: BenchmarkParameter , u32 ) >
879
883
| -> Result <( ) , & ' static str > {
880
884
// Set up the benchmark, return execution + verification function.
881
885
let closure_to_verify = <
@@ -901,9 +905,9 @@ macro_rules! impl_benchmark_test {
901
905
} else {
902
906
for ( _, ( name, low, high) ) in components. iter( ) . enumerate( ) {
903
907
// Test only the low and high value, assuming values in the middle won't break
904
- for component_value in vec![ low, high] {
908
+ for component_value in $crate :: vec![ low, high] {
905
909
// Select the max value for all the other components.
906
- let c: Vec <( $crate:: BenchmarkParameter , u32 ) > = components. iter( )
910
+ let c: $crate :: Vec <( $crate:: BenchmarkParameter , u32 ) > = components. iter( )
907
911
. enumerate( )
908
912
. map( |( _, ( n, _, h) ) |
909
913
if n == name {
@@ -924,6 +928,39 @@ macro_rules! impl_benchmark_test {
924
928
} ;
925
929
}
926
930
931
+ /// show error message and debugging info for the case of an error happening
932
+ /// during a benchmark
933
+ #[ allow( clippy:: too_many_arguments) ]
934
+ pub fn show_benchmark_debug_info (
935
+ instance_string : & [ u8 ] ,
936
+ benchmark : & [ u8 ] ,
937
+ lowest_range_values : & [ u32 ] ,
938
+ highest_range_values : & [ u32 ] ,
939
+ steps : & [ u32 ] ,
940
+ repeat : & u32 ,
941
+ verify : & bool ,
942
+ error_message : & str ,
943
+ ) -> sp_runtime:: RuntimeString {
944
+ sp_runtime:: format_runtime_string!(
945
+ "\n * Pallet: {}\n \
946
+ * Benchmark: {}\n \
947
+ * Lowest_range_values: {:?}\n \
948
+ * Highest_range_values: {:?}\n \
949
+ * Steps: {:?}\n \
950
+ * Repeat: {:?}\n \
951
+ * Verify: {:?}\n \
952
+ * Error message: {}",
953
+ sp_std:: str :: from_utf8( instance_string) . expect( "it's all just strings ran through the wasm interface. qed" ) ,
954
+ sp_std:: str :: from_utf8( benchmark) . expect( "it's all just strings ran through the wasm interface. qed" ) ,
955
+ lowest_range_values,
956
+ highest_range_values,
957
+ steps,
958
+ repeat,
959
+ verify,
960
+ error_message,
961
+ )
962
+ }
963
+
927
964
/// This macro adds pallet benchmarks to a `Vec<BenchmarkBatch>` object.
928
965
///
929
966
/// First create an object that holds in the input parameters for the benchmark:
@@ -989,7 +1026,9 @@ macro_rules! add_benchmark {
989
1026
if & pallet[ ..] == & b"*" [ ..] || & benchmark[ ..] == & b"*" [ ..] {
990
1027
for benchmark in $( $location ) * :: Benchmark :: benchmarks( * extra) . into_iter( ) {
991
1028
$batches. push( $crate:: BenchmarkBatch {
1029
+ pallet: name_string. to_vec( ) ,
992
1030
instance: instance_string. to_vec( ) ,
1031
+ benchmark: benchmark. to_vec( ) ,
993
1032
results: $( $location ) * :: Benchmark :: run_benchmark(
994
1033
benchmark,
995
1034
& lowest_range_values[ ..] ,
@@ -998,14 +1037,25 @@ macro_rules! add_benchmark {
998
1037
* repeat,
999
1038
whitelist,
1000
1039
* verify,
1001
- ) ?,
1002
- pallet: name_string. to_vec( ) ,
1003
- benchmark: benchmark. to_vec( ) ,
1004
- } ) ;
1040
+ ) . map_err( |e| {
1041
+ $crate:: show_benchmark_debug_info(
1042
+ instance_string,
1043
+ benchmark,
1044
+ lowest_range_values,
1045
+ highest_range_values,
1046
+ steps,
1047
+ repeat,
1048
+ verify,
1049
+ e,
1050
+ )
1051
+ } ) ?,
1052
+ } ) ;
1005
1053
}
1006
1054
} else {
1007
1055
$batches. push( $crate:: BenchmarkBatch {
1056
+ pallet: name_string. to_vec( ) ,
1008
1057
instance: instance_string. to_vec( ) ,
1058
+ benchmark: benchmark. clone( ) ,
1009
1059
results: $( $location ) * :: Benchmark :: run_benchmark(
1010
1060
& benchmark[ ..] ,
1011
1061
& lowest_range_values[ ..] ,
@@ -1014,9 +1064,18 @@ macro_rules! add_benchmark {
1014
1064
* repeat,
1015
1065
whitelist,
1016
1066
* verify,
1017
- ) ?,
1018
- pallet: name_string. to_vec( ) ,
1019
- benchmark: benchmark. clone( ) ,
1067
+ ) . map_err( |e| {
1068
+ $crate:: show_benchmark_debug_info(
1069
+ instance_string,
1070
+ benchmark,
1071
+ lowest_range_values,
1072
+ highest_range_values,
1073
+ steps,
1074
+ repeat,
1075
+ verify,
1076
+ e,
1077
+ )
1078
+ } ) ?,
1020
1079
} ) ;
1021
1080
}
1022
1081
}
0 commit comments