Add basic timing library

This commit is contained in:
lumi 2025-06-29 18:06:56 +10:00
parent fcdee34b42
commit 7768c5ec56
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
6 changed files with 57 additions and 13 deletions

View File

@ -35,8 +35,9 @@ name = "main"
harness = false
[features]
default = ["task", "fs", "net"]
default = ["task", "time", "fs", "net"]
task = ["lb/task"]
time = ["lb/time"]
fs = ["lb/fs"]
net = ["lb/net"]
tokio-console = ["dep:console-subscriber"]

View File

@ -10,6 +10,7 @@ repository.workspace = true
[features]
runtime = ["tokio/rt"]
task = ["tokio/rt", "tokio/time"]
time = []
fs = ["tokio/fs", "dep:walkdir", "dep:globset", "dep:tempfile"]
net = ["tokio/net", "tokio/io-util"]

View File

@ -9,3 +9,5 @@ pub mod net;
pub mod runtime;
#[cfg(feature = "task")]
pub mod task;
#[cfg(feature = "time")]
pub mod time;

30
crates/lb/src/time.rs Normal file
View File

@ -0,0 +1,30 @@
use luaffi::{cdef, metatype};
#[cdef(module = "lb:time")]
pub struct lb_timelib;
#[metatype]
impl lb_timelib {
#[new]
extern "Lua-C" fn new() -> Self {
Self
}
pub extern "Lua-C" fn instant() -> lb_instant {
lb_instant::new(std::time::Instant::now())
}
}
#[cdef]
pub struct lb_instant(#[opaque] std::time::Instant);
#[metatype]
impl lb_instant {
fn new(instant: std::time::Instant) -> Self {
Self(instant)
}
pub extern "Lua-C" fn elapsed(&self) -> f64 {
self.0.elapsed().as_secs_f64()
}
}

View File

@ -6,6 +6,8 @@ pub use lb::fs;
pub use lb::net;
#[cfg(feature = "task")]
pub use lb::task;
#[cfg(feature = "time")]
pub use lb::time;
#[doc(hidden)]
pub fn open(#[allow(unused)] rt: &mut lb::runtime::Builder) {
@ -13,6 +15,8 @@ pub fn open(#[allow(unused)] rt: &mut lb::runtime::Builder) {
rt.module::<task::lb_tasklib>();
#[cfg(feature = "task")]
rt.module::<chan::lb_chanlib>();
#[cfg(feature = "time")]
rt.module::<time::lb_timelib>();
#[cfg(feature = "fs")]
rt.module::<fs::lb_fslib>();
#[cfg(feature = "net")]

View File

@ -1,25 +1,27 @@
if (...) ~= nil and (...).type == "group" then return end -- prevent recursive harness call
local ok = pcall(require, "lb:task")
if not ok then error("lua test harness requires lb:task module") end
if not ok then error("lua test harness requires 'lb:task'") end
local ok, time = pcall(require, "lb:time")
if not ok then error("lua test harness requires 'lb:time'") end
local ok, fs = pcall(require, "lb:fs")
if not ok then error("lua test harness requires lb:fs module") end
if not ok then error("lua test harness requires 'lb:fs'") end
local global = _G
local colors = {
local color = {
reset = "\x1b[0m",
pass = "\x1b[32;1m", -- green
fail = "\x1b[31;1m", -- red
}
local icons = {
local icon = {
check = "\u{2713}",
cross = "\u{00d7}",
chevron = "\u{203a}",
}
local function style(name, s)
return ("%s%s%s"):format(colors[name], s, colors.reset)
return ("%s%s%s"):format(color[name], s, color.reset)
end
local function create_test(name, f, group)
@ -54,7 +56,7 @@ local function name_test(test)
local name = test.name
local group = test.group
while group ~= nil do
if group.name ~= "" then name = ("%s %s %s"):format(group.name, icons.chevron, name) end
if group.name ~= "" then name = ("%s %s %s"):format(group.name, icon.chevron, name) end
group = group.parent
end
return name
@ -106,7 +108,7 @@ end
local function main(item)
local cx = { tasks = {} }
local time, pass, fail = os.clock(), 0, 0
local time, pass, fail = time.instant(), 0, 0
start(cx, item)
for _, task in ipairs(cx.tasks) do
if task:await().state == "pass" then
@ -115,21 +117,25 @@ local function main(item)
fail = fail + 1
end
end
local time = (os.clock() - time) * 1000
local elapsed = time:elapsed()
local code = 1
if fail == 0 then
print("", style("pass", ("%s %d tests passed"):format(icons.check, pass)))
print("", style("pass", ("%s %d tests passed"):format(icon.check, pass)))
code = 0
else
print(
"",
("%s, %s"):format(
style("pass", ("%s %d tests passed"):format(icons.check, pass)),
style("fail", ("%s %d tests failed"):format(icons.cross, fail))
style("pass", ("%s %d tests passed"):format(icon.check, pass)),
style("fail", ("%s %d tests failed"):format(icon.cross, fail))
)
)
end
print("", ("%s completed in %d ms"):format(icons.chevron, time))
if elapsed < 1000 then
print("", ("%s completed in %.2f ms"):format(icon.chevron, elapsed * 1000))
else
print("", ("%s completed in %.2f s"):format(icon.chevron, elapsed))
end
cx = nil
collectgarbage()
check_refs()