From fc9c04fbf79ca6aaed7f7458a274d2c936b8f30f Mon Sep 17 00:00:00 2001 From: George Zhao Date: Sat, 20 May 2023 12:25:34 +0800 Subject: [PATCH] feat: introduce uv.metrics_info api --- src/luv.c | 3 +++ src/metrics.c | 24 ++++++++++++++++++++++++ tests/test-metrics.lua | 31 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/test-metrics.lua diff --git a/src/luv.c b/src/luv.c index 1bbce26d..141ba4c3 100644 --- a/src/luv.c +++ b/src/luv.c @@ -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} }; diff --git a/src/metrics.c b/src/metrics.c index bbe59411..48253746 100644 --- a/src/metrics.c +++ b/src/metrics.c @@ -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 diff --git a/tests/test-metrics.lua b/tests/test-metrics.lua new file mode 100644 index 00000000..3991837d --- /dev/null +++ b/tests/test-metrics.lua @@ -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)