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

cw1-whitelist-ng: Benchmarking #534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,061 changes: 1,000 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[workspace]
members = ["packages/*", "contracts/*"]

[patch.crates-io]
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm", branch = "lazy-deserialized-ep" }

[profile.release.package.cw1-subkeys]
codegen-units = 1
incremental = false
Expand Down
1 change: 1 addition & 0 deletions contracts/cw1-whitelist-ng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ anyhow = { version = "1", optional = true }
anyhow = "1"
assert_matches = "1"
cosmwasm-schema = { version = "1.0.0-beta" }
cosmwasm-vm = "1.0.0-beta"
cw-multi-test = { path = "../../packages/multi-test", version = "0.10.2" }
derivative = "2"
8 changes: 4 additions & 4 deletions contracts/cw1-whitelist-ng/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mod tests {
fn instantiate_and_modify_config() {
let contract = Cw1WhitelistContract::native();

let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down Expand Up @@ -292,7 +292,7 @@ mod tests {
#[test]
fn execute_messages_has_proper_permissions() {
let contract = Cw1WhitelistContract::native();
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down Expand Up @@ -342,7 +342,7 @@ mod tests {
#[test]
fn execute_custom_messages_works() {
let contract = Cw1WhitelistContract::<String>::new();
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();
let alice = "alice";

let admins = vec![alice.to_owned()];
Expand Down Expand Up @@ -371,7 +371,7 @@ mod tests {
#[test]
fn can_execute_query_works() {
let contract = Cw1WhitelistContract::native();
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down
16 changes: 9 additions & 7 deletions contracts/cw1-whitelist-ng/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ pub mod state;
mod entry_points {
use crate::error::ContractError;
use crate::state::Cw1WhitelistContract;
use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
use cosmwasm_std::{
entry_point_lazy, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response,
};

const CONTRACT: Cw1WhitelistContract<Empty> = Cw1WhitelistContract::native();

#[entry_point]
#[entry_point_lazy]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: Binary,
msg: Vec<u8>,
) -> Result<Response, ContractError> {
CONTRACT.entry_instantiate(deps, env, info, &msg)
}

#[entry_point]
#[entry_point_lazy]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: Binary,
msg: Vec<u8>,
) -> Result<Response, ContractError> {
CONTRACT.entry_execute(deps, env, info, &msg)
}

#[entry_point]
pub fn query(deps: Deps, env: Env, msg: Binary) -> Result<Binary, ContractError> {
#[entry_point_lazy]
pub fn query(deps: Deps, env: Env, msg: Vec<u8>) -> Result<Binary, ContractError> {
CONTRACT.entry_query(deps, env, &msg)
}
}
Expand Down
63 changes: 63 additions & 0 deletions contracts/cw1-whitelist-ng/tests/gas_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use cosmwasm_std::Empty;
use cosmwasm_std::{coins, to_binary, BankMsg, Response, WasmMsg};
use cosmwasm_vm::testing::{
execute, instantiate, mock_env, mock_info, mock_instance_with_gas_limit,
};
use cw1_whitelist_ng::msg::{Cw1ExecMsg, InstantiateMsg, WhitelistExecMsg};

static WASM: &[u8] = include_bytes!("../../../artifacts/cw1_whitelist_ng.wasm");

#[test]
fn execute_bench() {
let mut deps = mock_instance_with_gas_limit(WASM, 100_000_000_000_000);

let alice = "alice";
let bob = "bob";
let carl = "carl";

// instantiate the contract
let instantiate_msg = InstantiateMsg {
admins: vec![alice.to_string(), carl.to_string()],
mutable: false,
};
let info = mock_info(&bob, &[]);

let init_gas = deps.get_gas_left();

let _: Response = instantiate(&mut deps, mock_env(), info, instantiate_msg).unwrap();

let gas_after_instantiation = deps.get_gas_left();

let freeze = WhitelistExecMsg::Freeze {};
let msgs = vec![
BankMsg::Send {
to_address: bob.to_string(),
amount: coins(10000, "DAI"),
}
.into(),
WasmMsg::Execute {
contract_addr: "some contract".into(),
msg: to_binary(&freeze).unwrap(),
funds: vec![],
}
.into(),
];

// make some nice message
let execute_msg = Cw1ExecMsg::<Empty>::Execute { msgs: msgs.clone() };

// but carl can
let info = mock_info(&carl, &[]);
let _: Response = execute(&mut deps, mock_env(), info, execute_msg).unwrap();

let gas_after_execution = deps.get_gas_left();

println!(
"Instantiation gas usage: {}",
init_gas - gas_after_instantiation
);
println!(
"Execution gas usage: {}",
gas_after_instantiation - gas_after_execution
);
}
3 changes: 2 additions & 1 deletion contracts/cw1-whitelist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test-utils = []
cw0 = { path = "../../packages/cw0", version = "0.10.2" }
cw1 = { path = "../../packages/cw1", version = "0.10.2" }
cw2 = { path = "../../packages/cw2", version = "0.10.2" }
cosmwasm-std = { version = "1.0.0-beta", features = ["staking"] }
cosmwasm-std = { version = "1.0.0-beta", features = ["staking"] }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.10.2" }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
Expand All @@ -32,5 +32,6 @@ thiserror = { version = "1.0.23" }
anyhow = "1"
assert_matches = "1"
cosmwasm-schema = { version = "1.0.0-beta" }
cosmwasm-vm = "1.0.0-beta"
cw-multi-test = { path = "../../packages/multi-test", version = "0.10.2" }
derivative = "2"
6 changes: 3 additions & 3 deletions contracts/cw1-whitelist/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {

#[test]
fn instantiate_and_modify_config() {
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down Expand Up @@ -219,7 +219,7 @@ mod tests {

#[test]
fn execute_messages_has_proper_permissions() {
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down Expand Up @@ -268,7 +268,7 @@ mod tests {

#[test]
fn can_execute_query_works() {
let mut deps = mock_dependencies(&[]);
let mut deps = mock_dependencies();

let alice = "alice";
let bob = "bob";
Expand Down
63 changes: 63 additions & 0 deletions contracts/cw1-whitelist/tests/gas_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use cosmwasm_std::Empty;
use cosmwasm_std::{coins, to_binary, BankMsg, Response, WasmMsg};
use cosmwasm_vm::testing::{
execute, instantiate, mock_env, mock_info, mock_instance_with_gas_limit,
};
use cw1_whitelist::msg::{ExecuteMsg, InstantiateMsg};

static WASM: &[u8] = include_bytes!("../../../artifacts/cw1_whitelist.wasm");

#[test]
fn execute_bench() {
let mut deps = mock_instance_with_gas_limit(WASM, 100_000_000_000_000);

let alice = "alice";
let bob = "bob";
let carl = "carl";

// instantiate the contract
let instantiate_msg = InstantiateMsg {
admins: vec![alice.to_string(), carl.to_string()],
mutable: false,
};
let info = mock_info(&bob, &[]);

let init_gas = deps.get_gas_left();

let _: Response = instantiate(&mut deps, mock_env(), info, instantiate_msg).unwrap();

let gas_after_instantiation = deps.get_gas_left();

let freeze: ExecuteMsg<Empty> = ExecuteMsg::Freeze {};
let msgs = vec![
BankMsg::Send {
to_address: bob.to_string(),
amount: coins(10000, "DAI"),
}
.into(),
WasmMsg::Execute {
contract_addr: "some contract".into(),
msg: to_binary(&freeze).unwrap(),
funds: vec![],
}
.into(),
];

// make some nice message
let execute_msg = ExecuteMsg::<Empty>::Execute { msgs: msgs.clone() };

// but carl can
let info = mock_info(&carl, &[]);
let _: Response = execute(&mut deps, mock_env(), info, execute_msg).unwrap();

let gas_after_execution = deps.get_gas_left();

println!(
"Instantiation gas usage: {}",
init_gas - gas_after_instantiation
);
println!(
"Execution gas usage: {}",
gas_after_instantiation - gas_after_execution
);
}