-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathgas_tracing.rs
83 lines (73 loc) · 2.82 KB
/
gas_tracing.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
use std::time::Duration;
use criterion::{
black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput,
};
use fvm::call_manager::ExecutionStats;
use fvm::gas::tracer::{Consumption, Context, Event, GasTrace, Point};
use rand::{thread_rng, Rng};
pub fn benchmark_tracing(c: &mut Criterion) {
let mut group = c.benchmark_group("tracing");
for size in [32, 64, 128, 256].iter() {
group.throughput(Throughput::Elements(*size as u64));
let cid = cid::Cid::default();
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
// generate a random number to perform math with so that the compiler has no chance to optimize
let r: u64 = thread_rng().gen();
b.iter_batched_ref(
|| GasTrace::start(),
move |gt| {
let ctx = Context {
code_cid: cid, // copy, include the Cid copy cost
method_num: size,
};
let point = Point {
event: Event::Started,
label: "foo".to_string(), // include the string allocation cost
};
let consumption = Consumption {
fuel_consumed: Some(r),
gas_consumed: Some(r as i64),
};
gt.record(ctx, point, consumption);
},
BatchSize::SmallInput,
);
});
}
}
pub fn benchmark_accumulator(c: &mut Criterion) {
let mut group = c.benchmark_group("accumulator");
group.bench_function("exec stats accumulator", |b| {
// generate a random number to perform math with so that the compiler has no chance to optimize
let r: u64 = thread_rng().gen();
b.iter_batched_ref(
|| ExecutionStats::default(),
move |exec_stats| {
let now = minstant::Instant::now();
let call_duration = now.elapsed();
exec_stats.fuel_used += r;
exec_stats.call_count += 1;
exec_stats.call_overhead += call_duration;
exec_stats.wasm_duration +=
(call_duration + call_duration).max(Duration::default());
},
BatchSize::SmallInput,
)
});
}
pub fn benchmark_time(c: &mut Criterion) {
let mut group = c.benchmark_group("time");
group.bench_function("std::time::Instant::now()", |b| {
b.iter(|| black_box(std::time::Instant::now()))
});
group.bench_function("minstant::Instant::now()", |b| {
b.iter(|| black_box(minstant::Instant::now()))
});
}
criterion_group!(
benches,
benchmark_tracing,
benchmark_accumulator,
benchmark_time
);
criterion_main!(benches);