Skip to content

Commit 6a77b5f

Browse files
committed
Update benchmarks:
- Refactor - Add async benchmarks
1 parent aeb6611 commit 6a77b5f

File tree

2 files changed

+172
-103
lines changed

2 files changed

+172
-103
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ luajit-src = { version = ">= 210.1.2, < 220.0.0", optional = true }
5858

5959
[dev-dependencies]
6060
rustyline = "7.0"
61-
criterion = "0.3"
61+
criterion = { version = "0.3.4", features = ["html_reports", "async_tokio"] }
6262
trybuild = "1.0"
6363
futures = "0.3.5"
6464
hyper = { version = "0.14", features = ["client", "server"] }
@@ -70,6 +70,7 @@ serde_json = "1.0"
7070
[[bench]]
7171
name = "benchmark"
7272
harness = false
73+
required-features = ["async"]
7374

7475
[[example]]
7576
name = "async_http_client"

benches/benchmark.rs

Lines changed: 170 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
)]
1111
extern "system" {}
1212

13+
use std::time::Duration;
1314
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
15+
use tokio::runtime::Runtime;
16+
1417
use mlua::prelude::*;
1518

19+
fn collect_gc_twice(lua: &Lua) {
20+
lua.gc_collect().unwrap();
21+
lua.gc_collect().unwrap();
22+
}
23+
1624
fn create_table(c: &mut Criterion) {
17-
c.bench_function("create table", |b| {
18-
b.iter_batched_ref(
19-
|| Lua::new(),
20-
|lua| {
25+
let lua = Lua::new();
26+
27+
c.bench_function("create [table empty]", |b| {
28+
b.iter_batched(
29+
|| collect_gc_twice(&lua),
30+
|_| {
2131
lua.create_table().unwrap();
2232
},
2333
BatchSize::SmallInput,
@@ -26,12 +36,14 @@ fn create_table(c: &mut Criterion) {
2636
}
2737

2838
fn create_array(c: &mut Criterion) {
29-
c.bench_function("create array 10", |b| {
30-
b.iter_batched_ref(
31-
|| Lua::new(),
32-
|lua| {
39+
let lua = Lua::new();
40+
41+
c.bench_function("create [array] 10", |b| {
42+
b.iter_batched(
43+
|| collect_gc_twice(&lua),
44+
|_| {
3345
let table = lua.create_table().unwrap();
34-
for i in 1..11 {
46+
for i in 1..=10 {
3547
table.set(i, i).unwrap();
3648
}
3749
},
@@ -41,10 +53,12 @@ fn create_array(c: &mut Criterion) {
4153
}
4254

4355
fn create_string_table(c: &mut Criterion) {
44-
c.bench_function("create string table 10", |b| {
45-
b.iter_batched_ref(
46-
|| Lua::new(),
47-
|lua| {
56+
let lua = Lua::new();
57+
58+
c.bench_function("create [table string] 10", |b| {
59+
b.iter_batched(
60+
|| collect_gc_twice(&lua),
61+
|_| {
4862
let table = lua.create_table().unwrap();
4963
for &s in &["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] {
5064
let s = lua.create_string(s).unwrap();
@@ -56,114 +70,106 @@ fn create_string_table(c: &mut Criterion) {
5670
});
5771
}
5872

59-
fn call_add_function(c: &mut Criterion) {
60-
c.bench_function("call add function 3 10", |b| {
73+
fn call_lua_function(c: &mut Criterion) {
74+
let lua = Lua::new();
75+
76+
c.bench_function("call Lua function [sum] 3 10", |b| {
6177
b.iter_batched_ref(
6278
|| {
63-
let lua = Lua::new();
64-
let f = {
65-
let f: LuaFunction = lua
66-
.load(
67-
r#"
68-
function(a, b, c)
69-
return a + b + c
70-
end
71-
"#,
72-
)
73-
.eval()
74-
.unwrap();
75-
lua.create_registry_value(f).unwrap()
76-
};
77-
(lua, f)
78-
},
79-
|(lua, f)| {
80-
let add_function: LuaFunction = lua.registry_value(f).unwrap();
79+
collect_gc_twice(&lua);
80+
lua.load("function(a, b, c) return a + b + c end")
81+
.eval::<LuaFunction>()
82+
.unwrap()
83+
},
84+
|function| {
8185
for i in 0..10 {
82-
let _result: i64 = add_function.call((i, i + 1, i + 2)).unwrap();
86+
let _result: i64 = function.call((i, i + 1, i + 2)).unwrap();
8387
}
8488
},
8589
BatchSize::SmallInput,
8690
);
8791
});
8892
}
8993

90-
fn call_add_callback(c: &mut Criterion) {
91-
c.bench_function("call callback add 2 10", |b| {
94+
fn call_sum_callback(c: &mut Criterion) {
95+
let lua = Lua::new();
96+
let callback = lua
97+
.create_function(|_, (a, b, c): (i64, i64, i64)| Ok(a + b + c))
98+
.unwrap();
99+
lua.globals().set("callback", callback).unwrap();
100+
101+
c.bench_function("call Rust callback [sum] 3 10", |b| {
92102
b.iter_batched_ref(
93103
|| {
94-
let lua = Lua::new();
95-
let f = {
96-
let c: LuaFunction = lua
97-
.create_function(|_, (a, b, c): (i64, i64, i64)| Ok(a + b + c))
98-
.unwrap();
99-
lua.globals().set("callback", c).unwrap();
100-
let f: LuaFunction = lua
101-
.load(
102-
r#"
103-
function()
104-
for i = 1,10 do
105-
callback(i, i, i)
106-
end
107-
end
108-
"#,
109-
)
110-
.eval()
111-
.unwrap();
112-
lua.create_registry_value(f).unwrap()
113-
};
114-
(lua, f)
115-
},
116-
|(lua, f)| {
117-
let entry_function: LuaFunction = lua.registry_value(f).unwrap();
118-
entry_function.call::<_, ()>(()).unwrap();
104+
collect_gc_twice(&lua);
105+
lua.load("function() for i = 1,10 do callback(i, i+1, i+2) end end")
106+
.eval::<LuaFunction>()
107+
.unwrap()
108+
},
109+
|function| {
110+
function.call::<_, ()>(()).unwrap();
119111
},
120112
BatchSize::SmallInput,
121113
);
122114
});
123115
}
124116

125-
fn call_append_callback(c: &mut Criterion) {
126-
c.bench_function("call callback append 10", |b| {
117+
fn call_async_sum_callback(c: &mut Criterion) {
118+
let lua = Lua::new();
119+
let callback = lua
120+
.create_async_function(|_, (a, b, c): (i64, i64, i64)| async move { Ok(a + b + c) })
121+
.unwrap();
122+
lua.globals().set("callback", callback).unwrap();
123+
124+
c.bench_function("call async Rust callback [sum] 3 10", |b| {
125+
let rt = Runtime::new().unwrap();
126+
b.to_async(rt).iter_batched(
127+
|| {
128+
collect_gc_twice(&lua);
129+
lua.load("function() for i = 1,10 do callback(i, i+1, i+2) end end")
130+
.eval::<LuaFunction>()
131+
.unwrap()
132+
},
133+
|function| async move {
134+
function.call_async::<_, ()>(()).await.unwrap();
135+
},
136+
BatchSize::SmallInput,
137+
);
138+
});
139+
}
140+
141+
fn call_concat_callback(c: &mut Criterion) {
142+
let lua = Lua::new();
143+
let callback = lua
144+
.create_function(|_, (a, b): (LuaString, LuaString)| {
145+
Ok(format!("{}{}", a.to_str()?, b.to_str()?))
146+
})
147+
.unwrap();
148+
lua.globals().set("callback", callback).unwrap();
149+
150+
c.bench_function("call Rust callback [concat string] 10", |b| {
127151
b.iter_batched_ref(
128152
|| {
129-
let lua = Lua::new();
130-
let f = {
131-
let c: LuaFunction = lua
132-
.create_function(|_, (a, b): (LuaString, LuaString)| {
133-
Ok(format!("{}{}", a.to_str()?, b.to_str()?))
134-
})
135-
.unwrap();
136-
lua.globals().set("callback", c).unwrap();
137-
let f: LuaFunction = lua
138-
.load(
139-
r#"
140-
function()
141-
for _ = 1,10 do
142-
callback("a", "b")
143-
end
144-
end
145-
"#,
146-
)
147-
.eval()
148-
.unwrap();
149-
lua.create_registry_value(f).unwrap()
150-
};
151-
(lua, f)
152-
},
153-
|(lua, f)| {
154-
let entry_function: LuaFunction = lua.registry_value(f).unwrap();
155-
entry_function.call::<_, ()>(()).unwrap();
153+
collect_gc_twice(&lua);
154+
lua.load("function() for i = 1,10 do callback('a', tostring(i)) end end")
155+
.eval::<LuaFunction>()
156+
.unwrap()
157+
},
158+
|function| {
159+
function.call::<_, ()>(()).unwrap();
156160
},
157161
BatchSize::SmallInput,
158162
);
159163
});
160164
}
161165

162166
fn create_registry_values(c: &mut Criterion) {
163-
c.bench_function("create registry 10", |b| {
164-
b.iter_batched_ref(
165-
|| Lua::new(),
166-
|lua| {
167+
let lua = Lua::new();
168+
169+
c.bench_function("create [registry value] 10", |b| {
170+
b.iter_batched(
171+
|| collect_gc_twice(&lua),
172+
|_| {
167173
for _ in 0..10 {
168174
lua.create_registry_value(lua.pack(true).unwrap()).unwrap();
169175
}
@@ -178,10 +184,12 @@ fn create_userdata(c: &mut Criterion) {
178184
struct UserData(i64);
179185
impl LuaUserData for UserData {}
180186

181-
c.bench_function("create userdata 10", |b| {
182-
b.iter_batched_ref(
183-
|| Lua::new(),
184-
|lua| {
187+
let lua = Lua::new();
188+
189+
c.bench_function("create [table userdata] 10", |b| {
190+
b.iter_batched(
191+
|| collect_gc_twice(&lua),
192+
|_| {
185193
let table: LuaTable = lua.create_table().unwrap();
186194
for i in 1..11 {
187195
table.set(i, UserData(i)).unwrap();
@@ -192,20 +200,80 @@ fn create_userdata(c: &mut Criterion) {
192200
});
193201
}
194202

203+
fn call_userdata_method(c: &mut Criterion) {
204+
struct UserData(i64);
205+
impl LuaUserData for UserData {
206+
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
207+
methods.add_method("method", |_, this, ()| Ok(this.0));
208+
}
209+
}
210+
211+
let lua = Lua::new();
212+
lua.globals().set("userdata", UserData(10)).unwrap();
213+
214+
c.bench_function("call [userdata method] 10", |b| {
215+
b.iter_batched_ref(
216+
|| {
217+
collect_gc_twice(&lua);
218+
lua.load("function() for i = 1,10 do userdata:method() end end")
219+
.eval::<LuaFunction>()
220+
.unwrap()
221+
},
222+
|function| {
223+
function.call::<_, ()>(()).unwrap();
224+
},
225+
BatchSize::SmallInput,
226+
);
227+
});
228+
}
229+
230+
fn call_async_userdata_method(c: &mut Criterion) {
231+
#[derive(Clone, Copy)]
232+
struct UserData(i64);
233+
impl LuaUserData for UserData {
234+
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
235+
methods.add_async_method("method", |_, this, ()| async move { Ok(this.0) });
236+
}
237+
}
238+
239+
let lua = Lua::new();
240+
lua.globals().set("userdata", UserData(10)).unwrap();
241+
242+
c.bench_function("call async [userdata method] 10", |b| {
243+
let rt = Runtime::new().unwrap();
244+
b.to_async(rt).iter_batched(
245+
|| {
246+
collect_gc_twice(&lua);
247+
lua.load("function() for i = 1,10 do userdata:method() end end")
248+
.eval::<LuaFunction>()
249+
.unwrap()
250+
},
251+
|function| async move {
252+
function.call_async::<_, ()>(()).await.unwrap();
253+
},
254+
BatchSize::SmallInput,
255+
);
256+
});
257+
}
258+
195259
criterion_group! {
196260
name = benches;
197261
config = Criterion::default()
198-
.sample_size(200)
262+
.sample_size(300)
263+
.measurement_time(Duration::from_secs(10))
199264
.noise_threshold(0.02);
200265
targets =
201266
create_table,
202267
create_array,
203268
create_string_table,
204-
call_add_function,
205-
call_add_callback,
206-
call_append_callback,
269+
call_lua_function,
270+
call_sum_callback,
271+
call_async_sum_callback,
272+
call_concat_callback,
207273
create_registry_values,
208274
create_userdata,
275+
call_userdata_method,
276+
call_async_userdata_method,
209277
}
210278

211279
criterion_main!(benches);

0 commit comments

Comments
 (0)