Skip to content

Commit

Permalink
test: Introduce read_big_state test on benchmark tests (#3113)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx authored Sep 7, 2023
1 parent fa2dfd8 commit 9971633
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ jobs:
cargo build -p gear-cli --release --features=runtime-benchmarks,runtime-benchmarks-checkers
# check that perf benchmarks works. `--steps=20` need to test, that benchmarks works for different input number.
./target/release/gear benchmark pallet --chain=dev --pallet=pallet_gear --steps=20 --extrinsic="*" --execution=wasm --wasm-execution=compiled --heap-pages=4096
# check that read_big_state benchmarks works
./target/release/gear benchmark pallet --chain=dev --pallet=pallet_gear --extrinsic="read_big_state" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --extra
# check that check/test benchmarks works
./target/release/gear benchmark pallet --chain=dev --pallet=pallet_gear --extrinsic="check_all" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --extra
# check also lazy-pages benchmarks tests for native runtime
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ demo-program-generator = { path = "examples/program-generator" }
demo-proxy = { path = "examples/proxy", default-features = false }
demo-proxy-relay = { path = "examples/proxy-relay" }
demo-proxy-reservation-with-gas = { path = "examples/proxy-reservation-with-gas" }
demo-read-big-state = { path = "examples/read-big-state" }
demo-read-big-state = { path = "examples/read-big-state", default-features = false }
demo-reservation-manager = { path = "examples/reservation-manager" }
demo-reserve-gas = { path = "examples/reserve-gas" }
demo-rwlock = { path = "examples/rwlock" }
Expand Down
3 changes: 2 additions & 1 deletion examples/read-big-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
wasm-wrapper = []
std = ["wasm-wrapper", "parity-scale-codec/std"]
default = ["std"]
std = []
6 changes: 5 additions & 1 deletion examples/read-big-state/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use gear_wasm_builder::WasmBuilder;

fn main() {
gear_wasm_builder::build();
WasmBuilder::new()
.exclude_features(vec!["std", "wasm-wrapper"])
.build();
}
8 changes: 4 additions & 4 deletions examples/read-big-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::{collections::BTreeMap, string::String, vec, vec::Vec};
use parity_scale_codec::{Decode, Encode};

#[cfg(feature = "std")]
#[cfg(feature = "wasm-wrapper")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
#[cfg(feature = "wasm-wrapper")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[derive(Encode, Decode, Default, Debug, Clone)]
Expand Down Expand Up @@ -59,7 +59,7 @@ impl State {
}
}

#[cfg(not(feature = "std"))]
#[cfg(not(feature = "wasm-wrapper"))]
mod wasm {
use super::*;
use gstd::{msg, prelude::*};
Expand Down
7 changes: 3 additions & 4 deletions examples/sys-calls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@

extern crate alloc;

use alloc::{string::String, vec::Vec};
use parity_scale_codec::{Decode, Encode};

#[cfg(feature = "wasm-wrapper")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

type MessageId = [u8; 32];
type ActorId = [u8; 32];

#[cfg(feature = "wasm-wrapper")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

use alloc::{string::String, vec::Vec};
type MessageId = [u8; 32];
type ActorId = [u8; 32];

// Instead of proper gstd primitives we use their raw versions to make this contract
// compilable as a dependency for the build of the `gear` with `runtime-benchmarking` feature.
Expand Down
3 changes: 3 additions & 0 deletions pallets/gear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rand = { workspace = true, optional = true }
rand_pcg = { workspace = true, optional = true }
sp-consensus-slots = { workspace = true, optional = true }
test-syscalls = { workspace = true, optional = true }
demo-read-big-state = { workspace = true, optional = true }
demo-proxy = { workspace = true, optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -151,6 +152,7 @@ std = [
"gear-lazy-pages-common?/std",
"sp-consensus-babe/std",
"test-syscalls?/std",
"demo-read-big-state?/std",
"demo-proxy?/std",
"gear-runtime-interface/std",
]
Expand All @@ -167,6 +169,7 @@ runtime-benchmarks = [
"rand",
"rand_pcg",
"test-syscalls/wasm-wrapper",
"demo-read-big-state/wasm-wrapper",
"demo-proxy/wasm-wrapper",
"gsys",
]
Expand Down
5 changes: 5 additions & 0 deletions pallets/gear/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ benchmarks! {
T: pallet_gear_voucher::Config,
}

#[extra]
read_big_state {
syscalls_integrity::read_big_state::<T>();
} : {}

#[extra]
check_all {
syscalls_integrity::main_test::<T>();
Expand Down
81 changes: 80 additions & 1 deletion pallets/gear/src/benchmarking/tests/syscalls_integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
use super::*;

use crate::{CurrencyOf, Event, RentCostPerBlockOf, WaitlistOf};
use crate::{BlockGasLimitOf, CurrencyOf, Event, RentCostPerBlockOf, String, WaitlistOf};
use common::event::DispatchStatus;
use frame_support::traits::Randomness;
use gear_core::ids::{CodeId, ReservationId};
use gear_core_errors::{ReplyCode, SuccessReplyReason};
Expand All @@ -39,6 +40,84 @@ use parity_scale_codec::Decode;
use sp_runtime::SaturatedConversion;
use test_syscalls::{Kind, WASM_BINARY as SYSCALLS_TEST_WASM_BINARY};

pub fn read_big_state<T>()
where
T: Config,
T::AccountId: Origin,
{
use demo_read_big_state::{State, Strings, WASM_BINARY};

#[cfg(feature = "std")]
utils::init_logger();

let origin = benchmarking::account::<T::AccountId>("origin", 0, 0);
CurrencyOf::<T>::deposit_creating(
&origin,
100_000_000_000_000_000_u128.unique_saturated_into(),
);

let salt = b"read_big_state salt";

Gear::<T>::upload_program(
RawOrigin::Signed(origin.clone()).into(),
WASM_BINARY.to_vec(),
salt.to_vec(),
Default::default(),
BlockGasLimitOf::<T>::get(),
Zero::zero(),
)
.expect("Failed to upload read_big_state binary");

let pid = ProgramId::generate(CodeId::generate(WASM_BINARY), salt);
utils::run_to_next_block::<T>(None);

let string = String::from("hi").repeat(4095);
let string_size = 8 * 1024;
assert_eq!(string.encoded_size(), string_size);

let strings = Strings::new(string);
let strings_size = (string_size * Strings::LEN) + 1;
assert_eq!(strings.encoded_size(), strings_size);

let approx_size =
|size: usize, iteration: usize| -> usize { size - 17 - 144 * (iteration + 1) };

// with initial data step is ~2 MiB
let expected_size =
|iteration: usize| -> usize { Strings::LEN * State::LEN * string_size * (iteration + 1) };

// go to 6 MiB due to approximate calculations and 8MiB reply restrictions
for i in 0..3 {
let next_user_mid = utils::get_next_message_id::<T>(origin.clone());

Gear::<T>::send_message(
RawOrigin::Signed(origin.clone()).into(),
pid,
strings.encode(),
BlockGasLimitOf::<T>::get(),
Zero::zero(),
false,
)
.expect("Failed to send read_big_state append command");

utils::run_to_next_block::<T>(None);

assert!(SystemPallet::<T>::events().into_iter().any(|e| {
let bytes = e.event.encode();
let Ok(gear_event): Result<Event<T>, _> = Event::decode(&mut bytes[1..].as_ref()) else { return false };
let Event::MessagesDispatched { statuses, .. } = gear_event else { return false };

log::debug!("{statuses:?}");
log::debug!("{next_user_mid:?}");
matches!(statuses.get(&next_user_mid), Some(DispatchStatus::Success))
}), "No message with expected id had succeeded");

let state =
Gear::<T>::read_state_impl(pid, Default::default()).expect("Failed to read state");
assert_eq!(approx_size(state.len(), i), expected_size(i));
}
}

pub fn main_test<T>()
where
T: Config,
Expand Down

0 comments on commit 9971633

Please sign in to comment.