Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve benchmarking #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 98 additions & 80 deletions zenlink-protocol/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ use frame_system::RawOrigin;

const UNIT: u128 = 1_000_000_000_000;

const ASSET_0: AssetId = AssetId { chain_id: 2001, asset_type: 2, asset_index: 515 };
fn asset0<T: Config>()->AssetId{
AssetId{
chain_id: T::SelfParaId::get(),
asset_type: 2,
asset_index: 2,
}
}

const ASSET_1: AssetId = AssetId { chain_id: 2001, asset_type: 2, asset_index: 516 };
fn asset1<T: Config>()->AssetId{
AssetId{
chain_id: T::SelfParaId::get(),
asset_type: 2,
asset_index: 3,
}
}

const ASSET_2: AssetId = AssetId { chain_id: 2001, asset_type: 2, asset_index: 518 };
fn asset2<T: Config>()->AssetId{
AssetId{
chain_id: T::SelfParaId::get(),
asset_type: 2,
asset_index: 4,
}
}

pub fn lookup_of_account<T: Config>(
who: T::AccountId,
Expand Down Expand Up @@ -49,28 +67,28 @@ benchmarks! {
create_pair {
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

} : _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into())
} : _(RawOrigin::Root, asset0::<T>().into(), asset1::<T>().into())

bootstrap_create {
let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
}: _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts)
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];
}: _(RawOrigin::Root, asset0::<T>().into(), asset1::<T>().into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts)

bootstrap_contribute{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];
assert_ok!(ZenlinkPallet::<T>::bootstrap_create(
(RawOrigin::Root).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
1000,
1000,
1000_000_000,
Expand All @@ -80,21 +98,21 @@ benchmarks! {
reward_amounts,
));

}: _(RawOrigin::Signed(caller.clone()), ASSET_0.into(), ASSET_1.into(), UNIT, UNIT, 100u128.saturated_into())
}: _(RawOrigin::Signed(caller.clone()), asset0::<T>().into(), asset1::<T>().into(), UNIT, UNIT, 100u128.saturated_into())

bootstrap_claim{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];

assert_ok!(ZenlinkPallet::<T>::bootstrap_create(
(RawOrigin::Root).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
1000,
1000,
10*UNIT,
Expand All @@ -106,8 +124,8 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::bootstrap_contribute(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
10*UNIT,
10*UNIT,
99u128.saturated_into()
Expand All @@ -117,25 +135,25 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::bootstrap_end(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
));

}:_(RawOrigin::Signed(caller.clone()), lookup_of_account::<T>(caller.clone()), ASSET_0.into(), ASSET_1.into(), 120u128.saturated_into())
}:_(RawOrigin::Signed(caller.clone()), lookup_of_account::<T>(caller.clone()), asset0::<T>().into(), asset1::<T>().into(), 120u128.saturated_into())

bootstrap_end{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];

assert_ok!(ZenlinkPallet::<T>::bootstrap_create(
(RawOrigin::Root).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
1000,
1000,
10*UNIT,
Expand All @@ -147,26 +165,26 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::bootstrap_contribute(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
10*UNIT,
10*UNIT,
99u128.saturated_into()
));

run_to_block::<T>(100);
}:_(RawOrigin::Signed(caller.clone()), ASSET_0.into(), ASSET_1.into())
}:_(RawOrigin::Signed(caller.clone()), asset0::<T>().into(), asset1::<T>().into())

bootstrap_update{
let caller: T::AccountId = whitelisted_caller();

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];

assert_ok!(ZenlinkPallet::<T>::bootstrap_create(
(RawOrigin::Root).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
1000,
1000,
10*UNIT,
Expand All @@ -176,21 +194,21 @@ benchmarks! {
reward_amounts.clone(),
));

}:_(RawOrigin::Root, ASSET_0.into(), ASSET_1.into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts)
}:_(RawOrigin::Root, asset0::<T>().into(), asset1::<T>().into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts)

bootstrap_refund{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
let reward:Vec<T::AssetId> = vec![asset0::<T>().into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(asset1::<T>().into(), 0)];

assert_ok!(ZenlinkPallet::<T>::bootstrap_create(
(RawOrigin::Root).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
2*UNIT,
2*UNIT,
10*UNIT,
Expand All @@ -202,60 +220,60 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::bootstrap_contribute(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
1*UNIT,
1*UNIT,
99u128.saturated_into()
));
run_to_block::<T>(100);
}:_(RawOrigin::Signed(caller.clone()), ASSET_0.into(), ASSET_1.into())
}:_(RawOrigin::Signed(caller.clone()), asset0::<T>().into(), asset1::<T>().into())

add_liquidity{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset0::<T>().into(), asset1::<T>().into()));

assert_ok!(ZenlinkPallet::<T>::set_fee_receiver((RawOrigin::Root).into(), lookup_of_account::<T>(caller.clone()).into()));

}:_(RawOrigin::Signed(caller.clone()), ASSET_0.into(), ASSET_1.into(), 10 * UNIT, 10* UNIT, 0,0, 100u32.saturated_into())
}:_(RawOrigin::Signed(caller.clone()), asset0::<T>().into(), asset1::<T>().into(), 10 * UNIT, 10* UNIT, 0,0, 100u32.saturated_into())

remove_liquidity{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));

assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset0::<T>().into(), asset1::<T>().into()));

assert_ok!(ZenlinkPallet::<T>::set_fee_receiver((RawOrigin::Root).into(), lookup_of_account::<T>(caller.clone()).into()));

assert_ok!(ZenlinkPallet::<T>::add_liquidity(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
10 * UNIT,
10* UNIT,
0,
0,
100u32.saturated_into()));

}:_(RawOrigin::Signed(caller.clone()), ASSET_0.into(), ASSET_1.into(), 1 * UNIT, 0, 0, lookup_of_account::<T>(caller.clone()).into(), 100u32.saturated_into())
}:_(RawOrigin::Signed(caller.clone()), asset0::<T>().into(), asset1::<T>().into(), 1 * UNIT, 0, 0, lookup_of_account::<T>(caller.clone()).into(), 100u32.saturated_into())

swap_exact_assets_for_assets{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset2::<T>().into(), &caller, 1000 * UNIT));

assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset0::<T>().into(), asset1::<T>().into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset1::<T>().into(), asset2::<T>().into()));

assert_ok!(ZenlinkPallet::<T>::add_liquidity(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
10 * UNIT,
10* UNIT,
0,
Expand All @@ -264,31 +282,31 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::add_liquidity(
RawOrigin::Signed(caller.clone()).into(),
ASSET_1.into(),
ASSET_2.into(),
asset1::<T>().into(),
asset2::<T>().into(),
10 * UNIT,
10* UNIT,
0,
0,
100u32.saturated_into()));

let path: Vec<T::AssetId> = vec![ASSET_0.into(), ASSET_1.into(), ASSET_2.into()];
let path: Vec<T::AssetId> = vec![asset0::<T>().into(), asset1::<T>().into(), asset2::<T>().into()];

}:_(RawOrigin::Signed(caller.clone()), 1* UNIT, 0,path, lookup_of_account::<T>(caller.clone()).into(), 100u32.saturated_into())

swap_assets_for_exact_assets{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset0::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset1::<T>().into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(asset2::<T>().into(), &caller, 1000 * UNIT));

assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset1::<T>().into(), asset2::<T>().into()));
assert_ok!(ZenlinkPallet::<T>::create_pair((RawOrigin::Root).into(), asset0::<T>().into(), asset1::<T>().into()));

assert_ok!(ZenlinkPallet::<T>::add_liquidity(
RawOrigin::Signed(caller.clone()).into(),
ASSET_1.into(),
ASSET_2.into(),
asset1::<T>().into(),
asset2::<T>().into(),
10 * UNIT,
10* UNIT,
0,
Expand All @@ -297,14 +315,14 @@ benchmarks! {

assert_ok!(ZenlinkPallet::<T>::add_liquidity(
RawOrigin::Signed(caller.clone()).into(),
ASSET_0.into(),
ASSET_1.into(),
asset0::<T>().into(),
asset1::<T>().into(),
10 * UNIT,
10* UNIT,
0,
0,
100u32.saturated_into()));

let path: Vec<T::AssetId> = vec![ASSET_0.into(), ASSET_1.into(), ASSET_2.into()];
let path: Vec<T::AssetId> = vec![asset0::<T>().into(), asset1::<T>().into(), asset2::<T>().into()];
}:_(RawOrigin::Signed(caller.clone()), 1* UNIT, 10*UNIT,path, lookup_of_account::<T>(caller.clone()).into(), 100u32.saturated_into())
}