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
use cairo_lang_runner::StarknetState;
use cairo_native::{
context::NativeContext,
executor::{AotNativeExecutor, JitNativeExecutor},
OptLevel,
};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use util::{create_vm_runner, prepare_programs};
mod util;
pub fn bench_libfuncs(c: &mut Criterion) {
let programs = prepare_programs("tests/cases");
{
let mut c = c.benchmark_group("Libfunc Execution Time");
for (program, filename) in &programs {
let entry = program
.funcs
.iter()
.find(|f| {
if let Some(name) = &f.id.debug_name {
name.ends_with("main")
} else {
false
}
})
.expect("failed to find entry point");
let vm_runner = create_vm_runner(program);
c.bench_with_input(
BenchmarkId::new(filename, "SierraCasmRunner"),
&program,
|b, _program| {
b.iter(|| {
let res = vm_runner
.run_function_with_starknet_context(
entry,
vec![],
Some(usize::MAX),
StarknetState::default(),
)
.expect("should run correctly");
black_box(res)
})
},
);
c.bench_with_input(
BenchmarkId::new(filename, "jit-cold"),
&program,
|b, program| {
let native_context = NativeContext::new();
b.iter(|| {
let module = native_context
.compile(program, false, Some(Default::default()))
.unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
JitNativeExecutor::from_native_module(module, OptLevel::Aggressive)
.unwrap();
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
},
);
c.bench_with_input(
BenchmarkId::new(filename, "jit-hot"),
program,
|b, program| {
let native_context = NativeContext::new();
let module = native_context
.compile(program, false, Some(Default::default()))
.unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
JitNativeExecutor::from_native_module(module, OptLevel::Aggressive)
.unwrap();
// warmup
for _ in 0..5 {
native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
}
b.iter(|| {
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
},
);
c.bench_with_input(
BenchmarkId::new(filename, "aot-with-compile"),
&program,
|b, program| {
let native_context = NativeContext::new();
b.iter(|| {
let module = native_context
.compile(program, false, Some(Default::default()))
.unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
AotNativeExecutor::from_native_module(module, OptLevel::Aggressive)
.unwrap();
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
},
);
c.bench_with_input(
BenchmarkId::new(filename, "aot-without-compile"),
program,
|b, program| {
let native_context = NativeContext::new();
let module = native_context
.compile(program, false, Some(Default::default()))
.unwrap();
// pass manager internally verifies the MLIR output is correct.
let native_executor =
AotNativeExecutor::from_native_module(module, OptLevel::Aggressive)
.unwrap();
// warmup
for _ in 0..5 {
native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
}
b.iter(|| {
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
},
);
}
}
}
criterion_group!(benches, bench_libfuncs);
criterion_main!(benches);