Skip to content

Commit

Permalink
test(pallets): add instantiate_module_with_data_section_per_kb bench
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteNacked committed Apr 29, 2024
1 parent 8ba3c69 commit 2137fa9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
34 changes: 34 additions & 0 deletions pallets/gear/src/benchmarking/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,40 @@ where
module.into()
}

/// Creates a WebAssembly module with a data section of size `data_section_bytes`.
/// The generated module contains `data_segment_num` data segments with an overall size of `data_section_bytes`.
/// If `data_segment_num` is 0, no data segments are added.
/// If the result of dividing `data_section_bytes` by `data_segment_num` is 0, zero-length data segments are added.
pub fn sized_data_section(data_section_bytes: u32, data_segment_num: u32) -> Self {
let mut module = ModuleDefinition {
memory: Some(ImportedMemory::max::<T>()),
..Default::default()
};

if data_segment_num != 0 {
let (data_segment_size, residual_bytes) = (
data_section_bytes / data_segment_num,
data_section_bytes % data_segment_num,
);

for seg_idx in 0..data_segment_num {
module.data_segments.push(DataSegment {
offset: seg_idx * data_segment_size,
value: vec![0xA5; data_segment_size as usize],
});
}

// Add residual bytes to the last data segment
if residual_bytes != 0 {
if let Some(last) = module.data_segments.last_mut() {
last.value
.resize(data_segment_size as usize + residual_bytes as usize, 0xA5)
}
}
}
module.into()
}

/// Creates a memory instance for use in a sandbox with dimensions declared in this module
/// and adds it to `env`. A reference to that memory is returned so that it can be used to
/// access the memory contents from the supervisor.
Expand Down
15 changes: 13 additions & 2 deletions pallets/gear/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const MAX_PAYLOAD_LEN: u32 = 32 * 64 * 1024;
const MAX_PAYLOAD_LEN_KB: u32 = MAX_PAYLOAD_LEN / 1024;
const MAX_PAGES: u32 = 512;
const MAX_SALT_SIZE_BYTES: u32 = 4 * 1024 * 1024;
const MAX_NUMBER_OF_DATA_SEGMENTS: u32 = 1024;

/// How many batches we do per API benchmark.
const API_BENCHMARK_BATCHES: u32 = 20;
Expand Down Expand Up @@ -372,8 +373,8 @@ benchmarks! {
BenchmarkStorage::<T>::get(c).expect("Infallible: Key not found in storage");
}

// `c`: Size of the code in kilobytes.
instantiate_module_per_kb {
// `c`: Size of the code section in kilobytes.
instantiate_module_with_code_section_per_kb {
let c in 0 .. T::Schedule::get().limits.code_len / 1024;

let WasmModule { code, .. } = WasmModule::<T>::sized(c * 1024, Location::Init);
Expand All @@ -382,6 +383,16 @@ benchmarks! {
Environment::new(ext, &code, DispatchKind::Init, Default::default(), max_pages::<T>().into()).unwrap();
}

// `d`: Size of the data section in kilobytes.
instantiate_module_with_data_section_per_kb {
let d in 0 .. T::Schedule::get().limits.code_len / 1024;

let WasmModule { code, .. } = WasmModule::<T>::sized_data_section(d * 1024, MAX_NUMBER_OF_DATA_SEGMENTS);
}: {
let ext = Externalities::new(ProcessorContext::new_mock());
Environment::new(ext, &code, DispatchKind::Init, Default::default(), max_pages::<T>().into()).unwrap();
}

claim_value {
let caller = benchmarking::account("caller", 0, 0);
let _ = CurrencyOf::<T>::deposit_creating(&caller, 100_000_000_000_000_u128.unique_saturated_into());
Expand Down

0 comments on commit 2137fa9

Please sign in to comment.