Skip to content

Commit

Permalink
feat: introduce uv.metrics_info api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozg committed May 20, 2023
1 parent 278a6ce commit fc9c04f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ static const luaL_Reg luv_functions[] = {
#if LUV_UV_VERSION_GEQ(1, 39, 0)
{"metrics_idle_time", luv_metrics_idle_time},
#endif
#if LUV_UV_VERSION_GEQ(1, 45, 0)
{"metrics_info", luv_metrics_info},
#endif

{NULL, NULL}
};
Expand Down
24 changes: 24 additions & 0 deletions src/metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ static int luv_metrics_idle_time(lua_State* L) {
return 1;
}
#endif

#if LUV_UV_VERSION_GEQ(1, 45, 0)
static int luv_metrics_info(lua_State *L) {
uv_metrics_t metrics;
int ret = uv_metrics_info(luv_loop(L), &metrics);
if (ret < 0) return luv_error(L, ret);

lua_newtable(L);

lua_pushliteral(L, "loop_count");
lua_pushinteger(L, metrics.loop_count);
lua_rawset(L, -3);

lua_pushliteral(L, "events");
lua_pushinteger(L, metrics.events);
lua_rawset(L, -3);

lua_pushliteral(L, "events_waiting");
lua_pushinteger(L, metrics.events_waiting);
lua_rawset(L, -3);

return 1;
}
#endif
31 changes: 31 additions & 0 deletions tests/test-metrics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

return require('lib/tap')(function (test)

test("idle time", function (print, p, expect, uv)
local NS_TO_MS = 1000000
local timeout = 1000
local timer = uv.new_timer()
local counter = 0
timer:start(timeout, 0, function()
counter = counter + 1
local t = uv.hrtime()

-- Spin for 500 ms to spin loop time out of the delta check.
while uv.hrtime() - t < 600 * NS_TO_MS do end
timer:close()

local metrics = uv.metrics_info()
p(metrics)
assert(metrics.loop_count > 0)
assert(metrics.events >= 0)
assert(metrics.events_waiting >= 0)
end)

local metrics = assert(uv.metrics_info())
p(metrics)
assert(metrics.loop_count >= 0)
assert(metrics.events >= 0)
assert(metrics.events_waiting >= 0)
end, "1.45.0")

end)

0 comments on commit fc9c04f

Please sign in to comment.