mlua/benches/benchmark.rs

211 lines
6.2 KiB
Rust
Raw Normal View History

2019-10-17 11:04:43 -04:00
#![cfg_attr(
all(feature = "luajit", target_os = "macos", target_arch = "x86_64"),
feature(link_args)
)]
2019-10-17 11:04:43 -04:00
#[cfg_attr(
all(feature = "luajit", target_os = "macos", target_arch = "x86_64"),
link_args = "-pagezero_size 10000 -image_base 100000000"
)]
extern "system" {}
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use mlua::prelude::*;
2018-03-11 14:26:26 -04:00
fn create_table(c: &mut Criterion) {
c.bench_function("create table", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| Lua::new(),
2019-10-17 11:04:43 -04:00
|lua| {
2018-03-11 14:26:26 -04:00
lua.create_table().unwrap();
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
}
2018-03-11 14:26:26 -04:00
fn create_array(c: &mut Criterion) {
c.bench_function("create array 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| Lua::new(),
2019-10-17 11:04:43 -04:00
|lua| {
let table = lua.create_table().unwrap();
for i in 1..11 {
table.set(i, i).unwrap();
2018-03-11 14:26:26 -04:00
}
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
}
2018-03-11 14:26:26 -04:00
fn create_string_table(c: &mut Criterion) {
c.bench_function("create string table 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| Lua::new(),
2019-10-17 11:04:43 -04:00
|lua| {
let table = lua.create_table().unwrap();
for &s in &["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] {
let s = lua.create_string(s).unwrap();
table.set(s.clone(), s).unwrap();
2018-03-11 14:26:26 -04:00
}
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
}
2018-03-11 14:26:26 -04:00
fn call_add_function(c: &mut Criterion) {
c.bench_function("call add function 3 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| {
let lua = Lua::new();
let f = {
2018-09-24 22:13:42 -04:00
let f: LuaFunction = lua
2019-10-17 11:04:43 -04:00
.load(
2018-08-05 09:51:39 -04:00
r#"
2019-10-17 11:04:43 -04:00
function(a, b, c)
return a + b + c
end
"#,
2019-09-27 12:38:24 -04:00
)
2019-10-17 11:04:43 -04:00
.eval()
2019-09-27 12:38:24 -04:00
.unwrap();
2018-03-11 14:26:26 -04:00
lua.create_registry_value(f).unwrap()
};
(lua, f)
},
2019-10-17 11:04:43 -04:00
|(lua, f)| {
let add_function: LuaFunction = lua.registry_value(f).unwrap();
for i in 0..10 {
let _result: i64 = add_function.call((i, i + 1, i + 2)).unwrap();
2018-03-11 14:26:26 -04:00
}
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
2018-03-11 14:26:26 -04:00
}
fn call_add_callback(c: &mut Criterion) {
c.bench_function("call callback add 2 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| {
let lua = Lua::new();
let f = {
2018-08-05 09:51:39 -04:00
let c: LuaFunction = lua
.create_function(|_, (a, b, c): (i64, i64, i64)| Ok(a + b + c))
.unwrap();
2018-03-11 14:26:26 -04:00
lua.globals().set("callback", c).unwrap();
2018-09-24 22:13:42 -04:00
let f: LuaFunction = lua
2019-10-17 11:04:43 -04:00
.load(
2018-08-05 09:51:39 -04:00
r#"
2018-03-11 14:26:26 -04:00
function()
for i = 1,10 do
callback(i, i, i)
end
end
"#,
2019-09-27 12:38:24 -04:00
)
2019-10-17 11:04:43 -04:00
.eval()
2019-09-27 12:38:24 -04:00
.unwrap();
2018-03-11 14:26:26 -04:00
lua.create_registry_value(f).unwrap()
};
(lua, f)
},
2019-10-17 11:04:43 -04:00
|(lua, f)| {
let entry_function: LuaFunction = lua.registry_value(f).unwrap();
entry_function.call::<_, ()>(()).unwrap();
2018-03-11 14:26:26 -04:00
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
2018-03-11 14:26:26 -04:00
}
fn call_append_callback(c: &mut Criterion) {
c.bench_function("call callback append 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 14:26:26 -04:00
|| {
let lua = Lua::new();
let f = {
2018-09-24 22:13:42 -04:00
let c: LuaFunction = lua
.create_function(|_, (a, b): (LuaString, LuaString)| {
2018-03-11 14:26:26 -04:00
Ok(format!("{}{}", a.to_str()?, b.to_str()?))
2019-09-27 12:38:24 -04:00
})
.unwrap();
2018-03-11 14:26:26 -04:00
lua.globals().set("callback", c).unwrap();
2018-09-24 22:13:42 -04:00
let f: LuaFunction = lua
2019-10-17 11:04:43 -04:00
.load(
2018-08-05 09:51:39 -04:00
r#"
2018-03-11 14:26:26 -04:00
function()
for _ = 1,10 do
callback("a", "b")
end
end
"#,
2019-09-27 12:38:24 -04:00
)
2019-10-17 11:04:43 -04:00
.eval()
2019-09-27 12:38:24 -04:00
.unwrap();
2018-03-11 14:26:26 -04:00
lua.create_registry_value(f).unwrap()
};
(lua, f)
},
2019-10-17 11:04:43 -04:00
|(lua, f)| {
let entry_function: LuaFunction = lua.registry_value(f).unwrap();
entry_function.call::<_, ()>(()).unwrap();
2018-03-11 14:26:26 -04:00
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 14:26:26 -04:00
);
});
}
2018-03-11 17:50:17 -04:00
fn create_registry_values(c: &mut Criterion) {
c.bench_function("create registry 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-11 17:50:17 -04:00
|| Lua::new(),
2019-10-17 11:04:43 -04:00
|lua| {
2018-03-11 17:50:17 -04:00
for _ in 0..10 {
lua.create_registry_value(lua.pack(true).unwrap()).unwrap();
}
lua.expire_registry_values();
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-11 17:50:17 -04:00
);
});
}
2018-03-12 12:48:20 -04:00
fn create_userdata(c: &mut Criterion) {
struct UserData(i64);
2018-03-12 12:48:20 -04:00
impl LuaUserData for UserData {}
c.bench_function("create userdata 10", |b| {
2019-10-17 11:04:43 -04:00
b.iter_batched_ref(
2018-03-12 12:48:20 -04:00
|| Lua::new(),
2019-10-17 11:04:43 -04:00
|lua| {
let table: LuaTable = lua.create_table().unwrap();
for i in 1..11 {
table.set(i, UserData(i)).unwrap();
2018-03-12 12:48:20 -04:00
}
},
2019-10-17 11:04:43 -04:00
BatchSize::SmallInput,
2018-03-12 12:48:20 -04:00
);
});
}
2018-03-11 14:26:26 -04:00
criterion_group! {
name = benches;
config = Criterion::default()
.sample_size(200)
.noise_threshold(0.02);
targets =
create_table,
create_array,
create_string_table,
call_add_function,
call_add_callback,
2018-03-11 17:50:17 -04:00
call_append_callback,
2018-03-12 12:48:20 -04:00
create_registry_values,
2019-10-17 11:04:43 -04:00
create_userdata,
2018-03-11 14:26:26 -04:00
}
criterion_main!(benches);