Skip to content

Commit bffd254

Browse files
committed
Add cpu loop benchmark
1 parent 7af2aa4 commit bffd254

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

packages/vm/benches/main.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
22

33
use rand::Rng;
44
use std::sync::Arc;
@@ -12,7 +12,7 @@ use cosmwasm_vm::testing::{
1212
};
1313
use cosmwasm_vm::{
1414
call_execute, call_instantiate, capabilities_from_csv, Cache, CacheOptions, Instance,
15-
InstanceOptions, Size,
15+
InstanceOptions, Size, VmError,
1616
};
1717

1818
// Instance
@@ -22,6 +22,7 @@ const DEFAULT_INSTANCE_OPTIONS: InstanceOptions = InstanceOptions {
2222
gas_limit: DEFAULT_GAS_LIMIT,
2323
};
2424
const HIGH_GAS_LIMIT: u64 = 20_000_000_000_000; // ~20s, allows many calls on one instance
25+
const MEDIUM_GAS_LIMIT: u64 = 1_000_000_000_000; // ~1s
2526

2627
// Cache
2728
const MEMORY_CACHE_SIZE: Size = Size::mebi(200);
@@ -133,6 +134,53 @@ fn bench_instance(c: &mut Criterion) {
133134
println!("Gas used: {gas_used}");
134135
});
135136

137+
group.bench_function("execute execute (infinite loop)", |b| {
138+
let backend = mock_backend(&[]);
139+
let medium_gas: InstanceOptions = InstanceOptions {
140+
gas_limit: MEDIUM_GAS_LIMIT,
141+
};
142+
let mut instance = Instance::from_code(
143+
CYBERPUNK,
144+
backend,
145+
medium_gas.clone(),
146+
Some(DEFAULT_MEMORY_LIMIT),
147+
)
148+
.unwrap();
149+
150+
let info = mock_info("creator", &coins(1000, "earth"));
151+
let contract_result =
152+
call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, b"{}").unwrap();
153+
assert!(contract_result.into_result().is_ok());
154+
155+
let mut gas_used = 0;
156+
b.iter_batched(
157+
|| {
158+
// setup new instance for each iteration because cpu loop will consume all gas
159+
let instance = Instance::from_code(
160+
CYBERPUNK,
161+
mock_backend(&[]),
162+
medium_gas.clone(),
163+
Some(DEFAULT_MEMORY_LIMIT),
164+
)
165+
.unwrap();
166+
instance
167+
},
168+
|mut instance| {
169+
let gas_before = instance.get_gas_left();
170+
let info = mock_info("hasher", &[]);
171+
let msg = br#"{"cpu_loop":{}}"#;
172+
173+
let vm_result =
174+
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg);
175+
176+
assert!(matches!(vm_result, Err(VmError::GasDepletion { .. })));
177+
gas_used = gas_before - instance.get_gas_left();
178+
},
179+
BatchSize::SmallInput,
180+
);
181+
println!("Gas used: {gas_used}");
182+
});
183+
136184
group.finish();
137185
}
138186

0 commit comments

Comments
 (0)