-
Notifications
You must be signed in to change notification settings - Fork 346
/
main.rs
500 lines (432 loc) · 18.2 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use std::{fs, thread};
use tempfile::TempDir;
use cosmwasm_std::{coins, Checksum, Empty};
use cosmwasm_vm::testing::{
mock_backend, mock_env, mock_info, mock_instance_options, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_vm::{
call_execute, call_instantiate, capabilities_from_csv, Cache, CacheOptions, Instance,
InstanceOptions, Size,
};
// Instance
const DEFAULT_MEMORY_LIMIT: Size = Size::mebi(64);
const DEFAULT_GAS_LIMIT: u64 = 1_000_000_000; // ~1ms
const DEFAULT_INSTANCE_OPTIONS: InstanceOptions = InstanceOptions {
gas_limit: DEFAULT_GAS_LIMIT,
};
const HIGH_GAS_LIMIT: u64 = 20_000_000_000_000; // ~20s, allows many calls on one instance
// Cache
const MEMORY_CACHE_SIZE: Size = Size::mebi(200);
// Multi-threaded get_instance benchmark
const INSTANTIATION_THREADS: usize = 128;
const CONTRACTS: u64 = 10;
const DEFAULT_CAPABILITIES: &str = "cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,iterator,staking";
static HACKATOM: &[u8] = include_bytes!("../testdata/hackatom.wasm");
static CYBERPUNK: &[u8] = include_bytes!("../testdata/cyberpunk.wasm");
static BENCH_CONTRACTS: &[&str] = &[
"cyberpunk_rust170.wasm",
"cyberpunk.wasm",
"floaty_1.0.wasm",
"floaty_1.2.wasm",
"floaty_2.0.wasm",
"hackatom_1.0.wasm",
"hackatom_1.2.wasm",
"hackatom.wasm",
];
fn bench_instance(c: &mut Criterion) {
let mut group = c.benchmark_group("Instance");
group.bench_function("compile and instantiate", |b| {
b.iter(|| {
let backend = mock_backend(&[]);
let (instance_options, memory_limit) = mock_instance_options();
let _instance =
Instance::from_code(HACKATOM, backend, instance_options, memory_limit).unwrap();
});
});
group.bench_function("execute init", |b| {
let backend = mock_backend(&[]);
let much_gas: InstanceOptions = InstanceOptions {
gas_limit: HIGH_GAS_LIMIT,
};
let mut instance =
Instance::from_code(HACKATOM, backend, much_gas, Some(DEFAULT_MEMORY_LIMIT)).unwrap();
b.iter(|| {
let info = mock_info(&instance.api().addr_make("creator"), &coins(1000, "earth"));
let verifier = instance.api().addr_make("verifies");
let beneficiary = instance.api().addr_make("benefits");
let msg = format!(r#"{{"verifier": "{verifier}", "beneficiary": "{beneficiary}"}}"#);
let contract_result = call_instantiate::<_, _, _, Empty>(
&mut instance,
&mock_env(),
&info,
msg.as_bytes(),
)
.unwrap();
assert!(contract_result.into_result().is_ok());
});
});
group.bench_function("execute execute (release)", |b| {
let backend = mock_backend(&[]);
let much_gas: InstanceOptions = InstanceOptions {
gas_limit: HIGH_GAS_LIMIT,
};
let mut instance =
Instance::from_code(HACKATOM, backend, much_gas, Some(DEFAULT_MEMORY_LIMIT)).unwrap();
let info = mock_info(&instance.api().addr_make("creator"), &coins(1000, "earth"));
let verifier = instance.api().addr_make("verifies");
let beneficiary = instance.api().addr_make("benefits");
let msg = format!(r#"{{"verifier": "{verifier}", "beneficiary": "{beneficiary}"}}"#);
let contract_result =
call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg.as_bytes())
.unwrap();
assert!(contract_result.into_result().is_ok());
b.iter(|| {
let info = mock_info(&verifier, &coins(15, "earth"));
let msg = br#"{"release":{}}"#;
let contract_result =
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap();
assert!(contract_result.into_result().is_ok());
});
});
group.bench_function("execute execute (argon2)", |b| {
let backend = mock_backend(&[]);
let much_gas: InstanceOptions = InstanceOptions {
gas_limit: HIGH_GAS_LIMIT,
};
let mut instance =
Instance::from_code(CYBERPUNK, backend, much_gas, Some(DEFAULT_MEMORY_LIMIT)).unwrap();
let info = mock_info("creator", &coins(1000, "earth"));
let contract_result =
call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, b"{}").unwrap();
assert!(contract_result.into_result().is_ok());
let mut gas_used = 0;
b.iter(|| {
let gas_before = instance.get_gas_left();
let info = mock_info("hasher", &[]);
let msg = br#"{"argon2":{"mem_cost":256,"time_cost":3}}"#;
let contract_result =
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap();
assert!(contract_result.into_result().is_ok());
gas_used = gas_before - instance.get_gas_left();
});
println!("Gas used: {gas_used}");
});
group.finish();
}
fn bench_cache(c: &mut Criterion) {
let mut group = c.benchmark_group("Cache");
let options = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv(DEFAULT_CAPABILITIES),
MEMORY_CACHE_SIZE,
DEFAULT_MEMORY_LIMIT,
);
group.bench_function("save wasm", |b| {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
b.iter(|| {
let result = cache.store_code(HACKATOM, true, true);
assert!(result.is_ok());
});
});
group.bench_function("load wasm", |b| {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
let checksum = cache.store_code(HACKATOM, true, true).unwrap();
b.iter(|| {
let result = cache.load_wasm(&checksum);
assert!(result.is_ok());
});
});
group.bench_function("load wasm unchecked", |b| {
let options = options.clone();
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
cache.set_module_unchecked(true);
let checksum = cache.store_code(HACKATOM, true, true).unwrap();
b.iter(|| {
let result = cache.load_wasm(&checksum);
assert!(result.is_ok());
});
});
for contract_name in BENCH_CONTRACTS {
let contract_wasm = fs::read(format!("testdata/{contract_name}")).unwrap();
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
let checksum = cache.store_code(&contract_wasm, true, true).unwrap();
group.bench_function(format!("analyze_{contract_name}"), |b| {
b.iter(|| {
let result = cache.analyze(&checksum);
assert!(result.is_ok());
});
});
}
group.bench_function("instantiate from fs", |b| {
let non_memcache = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv(DEFAULT_CAPABILITIES),
Size::new(0),
DEFAULT_MEMORY_LIMIT,
);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
let checksum = cache.store_code(HACKATOM, true, true).unwrap();
b.iter(|| {
let _ = cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_fs_cache >= 1);
assert_eq!(cache.stats().misses, 0);
});
});
group.bench_function("instantiate from fs unchecked", |b| {
let non_memcache = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv(DEFAULT_CAPABILITIES),
Size::new(0),
DEFAULT_MEMORY_LIMIT,
);
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
cache.set_module_unchecked(true);
let checksum = cache.store_code(HACKATOM, true, true).unwrap();
b.iter(|| {
let _ = cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_fs_cache >= 1);
assert_eq!(cache.stats().misses, 0);
});
});
group.bench_function("instantiate from memory", |b| {
let checksum = Checksum::generate(HACKATOM);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
// Load into memory
cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
b.iter(|| {
let backend = mock_backend(&[]);
let _ = cache
.get_instance(&checksum, backend, DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert!(cache.stats().hits_memory_cache >= 1);
assert_eq!(cache.stats().hits_fs_cache, 1);
assert_eq!(cache.stats().misses, 0);
});
});
group.bench_function("instantiate from pinned memory", |b| {
let checksum = Checksum::generate(HACKATOM);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
// Load into pinned memory
cache.pin(&checksum).unwrap();
b.iter(|| {
let backend = mock_backend(&[]);
let _ = cache
.get_instance(&checksum, backend, DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_pinned_memory_cache >= 1);
assert_eq!(cache.stats().hits_fs_cache, 1);
assert_eq!(cache.stats().misses, 0);
});
});
group.finish();
}
fn bench_instance_threads(c: &mut Criterion) {
c.bench_function("multi-threaded get_instance", |b| {
let options = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv(DEFAULT_CAPABILITIES),
MEMORY_CACHE_SIZE,
DEFAULT_MEMORY_LIMIT,
);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
let cache = Arc::new(cache);
// Find sub-sequence helper
fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack
.windows(needle.len())
.position(|window| window == needle)
}
// Offset to the i32.const (0x41) 15731626 (0xf00baa) (unsigned leb128 encoded) instruction
// data we want to replace
let query_int_data = b"\x41\xaa\x97\xc0\x07";
let offset = find_subsequence(HACKATOM, query_int_data).unwrap() + 1;
let mut leb128_buf = [0; 4];
let mut contract = HACKATOM.to_vec();
let mut random_checksum = || {
let mut writable = &mut leb128_buf[..];
// Generates a random number in the range of a 4-byte unsigned leb128 encoded number
let r = rand::thread_rng().gen_range(2097152..2097152 + CONTRACTS);
leb128::write::unsigned(&mut writable, r).expect("Should write number");
// Splice data in contract
contract.splice(offset..offset + leb128_buf.len(), leb128_buf);
cache.store_code(contract.as_slice(), true, true).unwrap()
// let checksum = cache.store_code(contract.as_slice(), true, true).unwrap();
// Preload into memory
// cache
// .get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
// .unwrap();
// checksum
};
b.iter_custom(|iters| {
let mut res = Duration::from_secs(0);
for _ in 0..iters {
let mut durations: Vec<_> = (0..INSTANTIATION_THREADS)
.map(|_id| {
let cache = Arc::clone(&cache);
let checksum = random_checksum();
thread::spawn(move || {
// Perform measurement internally
let t = SystemTime::now();
black_box(
cache
.get_instance(
&checksum,
mock_backend(&[]),
DEFAULT_INSTANCE_OPTIONS,
)
.unwrap(),
);
t.elapsed().unwrap()
})
})
.collect::<Vec<_>>()
.into_iter()
.map(|handle| handle.join().unwrap())
.collect(); // join threads, collect durations
// Calculate median thread duration
durations.sort_unstable();
res += durations[durations.len() / 2];
}
res
});
});
}
fn bench_combined(c: &mut Criterion) {
let mut group = c.benchmark_group("Combined");
let options = CacheOptions::new(
TempDir::new().unwrap().into_path(),
capabilities_from_csv("cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,iterator,staking"),
MEMORY_CACHE_SIZE,
DEFAULT_MEMORY_LIMIT,
);
// Store contracts for all benchmarks in this group
let checksum: Checksum = {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
cache.store_code(CYBERPUNK, true, true).unwrap()
};
group.bench_function("get instance from fs cache and execute", |b| {
let mut non_memcache = options.clone();
non_memcache.memory_cache_size_bytes = Size::kibi(0);
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
b.iter(|| {
let mut instance = cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_fs_cache >= 1);
assert_eq!(cache.stats().misses, 0);
let info = mock_info("guest", &[]);
let msg = br#"{"noop":{}}"#;
let contract_result =
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap();
contract_result.into_result().unwrap();
});
});
group.bench_function("get instance from memory cache and execute", |b| {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
// Load into memory
cache
.get_instance(&checksum, mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS)
.unwrap();
b.iter(|| {
let backend = mock_backend(&[]);
let mut instance = cache
.get_instance(&checksum, backend, DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_pinned_memory_cache, 0);
assert!(cache.stats().hits_memory_cache >= 1);
assert_eq!(cache.stats().hits_fs_cache, 1);
assert_eq!(cache.stats().misses, 0);
let info = mock_info("guest", &[]);
let msg = br#"{"noop":{}}"#;
let contract_result =
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap();
contract_result.into_result().unwrap();
});
});
group.bench_function("get instance from pinned memory and execute", |b| {
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options.clone()).unwrap() };
// Load into pinned memory
cache.pin(&checksum).unwrap();
b.iter(|| {
let backend = mock_backend(&[]);
let mut instance = cache
.get_instance(&checksum, backend, DEFAULT_INSTANCE_OPTIONS)
.unwrap();
assert_eq!(cache.stats().hits_memory_cache, 0);
assert!(cache.stats().hits_pinned_memory_cache >= 1);
assert_eq!(cache.stats().hits_fs_cache, 1);
assert_eq!(cache.stats().misses, 0);
let info = mock_info("guest", &[]);
let msg = br#"{"noop":{}}"#;
let contract_result =
call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap();
contract_result.into_result().unwrap();
});
});
group.finish();
}
fn make_config(measurement_time_s: u64) -> Criterion {
Criterion::default()
.without_plots()
.measurement_time(Duration::new(measurement_time_s, 0))
.sample_size(12)
.configure_from_args()
}
criterion_group!(
name = instance;
config = make_config(8);
targets = bench_instance
);
criterion_group!(
name = cache;
config = make_config(8);
targets = bench_cache
);
// Combines loading module from cache, instantiating it and executing the instance.
// This is what every call in libwasmvm does.
criterion_group!(
name = combined;
config = make_config(5);
targets = bench_combined
);
criterion_group!(
name = multi_threaded_instance;
config = Criterion::default()
.without_plots()
.measurement_time(Duration::new(16, 0))
.sample_size(10)
.configure_from_args();
targets = bench_instance_threads
);
criterion_main!(instance, cache, combined, multi_threaded_instance);