Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![cfg_attr(not(feature = "std"), no_std)]
//! 1. Add macro attribute on method you want to benchmark.
//! ```ignore
//! #[orml_weight_meter::weight(0)]
//! fn inner_do_something(something: u32) {
//! // Update storage.
//! Something::<T>::put(something);
//! }
//! ```
//! Start with `0` and after the weights is generated then it can be replaced
//! with generated weight. Macro will inject callable methods that wraps inner
//! methods. Generated call will start with prefix `method_` followed by method
//! name. This only works for methods with `orml_weight_meter::weight` attribute
//! and only when running benchmarks.
//!
//! 2. Create benchmarks using orml_bencher and generate the weights with
//! orml_weight_gen
//! After running the benchmarks and the weights have been generated then we can
//! replace
//! ```ignore
//! #[orml_weight_meter::weight(0)]
//! ```
//! with
//!```ignore
//! #[orml_weight_meter::weight(T::WeightInfo::method_inner_do_something())]
//! ```
//!
//! 3. Use WeightMeter on your calls by adding macro
//! `#[orml_weight_meter::start]` and at the end use
//! `orml_weight_meter::used_weight()` to get used weight.
//!```ignore
//! #[pallet::call]
//! impl<T: Config> Pallet<T> {
//! #[pallet::weight(T::WeightInfo::do_something())]
//! #[orml_weight_meter::start]
//! pub fn do_something(origin: OriginFor<T>, something: u32) ->
//! DispatchResultWithPostInfo {
//! let who = ensure_signed(origin)?;
//! Self::inner_do_something(something);
//! // Emit an event.
//! Self::deposit_event(Event::SomethingStored(something, who));
//! Ok(PostDispatchInfo::from(Some(orml_weight_meter::used_weight())))
//! }
//! }
//! ```
use frame_support::weights::Weight;
struct Meter {
used_weight: Weight,
// Depth gets incremented when entering call or a sub-call
// This is used to avoid miscalculation during sub-calls
depth: u8,
}
mod meter_no_std;
mod meter_std;
// For use in mock file
#[cfg(test)]
extern crate self as orml_weight_meter;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "std")]
pub use meter_std::*;
#[cfg(not(feature = "std"))]
pub use meter_no_std::*;
/// Start weight meter
pub use weight_meter_procedural::start;
/// Measure each methods weight
pub use weight_meter_procedural::weight;