From: Artem S. Povalyukhin Date: Wed, 14 Nov 2018 15:14:49 +0000 (+0300) Subject: console.time() and console.timeEnd() methods. X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=5a62746510bc46dea96334e50435555e30de942b;p=njs.git console.time() and console.timeEnd() methods. This fixes #62 issue on Github. --- diff --git a/njs/njs_shell.c b/njs/njs_shell.c index 8445f330..6743d336 100644 --- a/njs/njs_shell.c +++ b/njs/njs_shell.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -63,6 +64,10 @@ static njs_ret_t njs_ext_console_dump(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused); static njs_ret_t njs_ext_console_help(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused); +static njs_ret_t njs_ext_console_time(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused); +static njs_ret_t njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused); static njs_external_t njs_ext_console[] = { @@ -102,6 +107,30 @@ static njs_external_t njs_ext_console[] = { NULL, njs_ext_console_help, 0 }, + + { nxt_string("time"), + NJS_EXTERN_METHOD, + NULL, + 0, + NULL, + NULL, + NULL, + NULL, + NULL, + njs_ext_console_time, + 0 }, + + { nxt_string("timeEnd"), + NJS_EXTERN_METHOD, + NULL, + 0, + NULL, + NULL, + NULL, + NULL, + NULL, + njs_ext_console_time_end, + 0 }, }; static njs_external_t njs_externals[] = { @@ -123,6 +152,9 @@ static njs_external_t njs_externals[] = { static njs_completion_t njs_completion; +static struct timeval njs_console_time; + + int main(int argc, char **argv) { @@ -727,3 +759,57 @@ njs_ext_console_help(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NJS_OK; } + + +static njs_ret_t +njs_ext_console_time(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + if (!njs_value_is_void(njs_arg(args, nargs, 1))) { + njs_vm_error(vm, "labels not implemented"); + return NJS_ERROR; + } + + vm->retval = njs_value_void; + + gettimeofday(&njs_console_time, NULL); + + return NJS_OK; +} + + +static njs_ret_t +njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + int64_t us, ms; + struct timeval tv; + + gettimeofday(&tv, NULL); + + if (!njs_value_is_void(njs_arg(args, nargs, 1))) { + njs_vm_error(vm, "labels not implemented"); + return NJS_ERROR; + } + + if (nxt_fast_path(njs_console_time.tv_sec || njs_console_time.tv_usec)) { + + us = ((int64_t) tv.tv_sec - njs_console_time.tv_sec) * 1000000 + + ((int64_t) tv.tv_usec - njs_console_time.tv_usec); + + ms = us / 1000; + us = us % 1000; + + printf("default: %" PRIu64 ".%03" PRIu64 "ms\n", ms, us); + + njs_console_time.tv_sec = 0; + njs_console_time.tv_usec = 0; + + } else { + printf("Timer \"default\" doesn’t exist.\n"); + } + + vm->retval = njs_value_void; + + return NJS_OK; +} diff --git a/njs/test/njs_expect_test.exp b/njs/test/njs_expect_test.exp index 271adb04..15443f9e 100644 --- a/njs/test/njs_expect_test.exp +++ b/njs/test/njs_expect_test.exp @@ -80,7 +80,7 @@ njs_test { # Global completions, multiple partial match njs_test { {"cons\t\t" - "console*console.help*console.log*const"} + "console*console.help*console.time*const"} } njs_test { @@ -190,6 +190,22 @@ njs_test { "console.help()\r\nVM built-in objects:"} } +# console.time* functions +njs_test { + {"console.time()\r\n" + "console.time()\r\nundefined\r\n>> "} + {"console.time(undefined)\r\n" + "console.time(undefined)\r\nundefined\r\n>> "} + {"console.timeEnd()\r\n" + "console.timeEnd()\r\ndefault: *.*ms\r\nundefined\r\n>> "} + {"console.time('a')\r\n" + "console.time('a')\r\nError: labels not implemented"} + {"console.timeEnd('a')\r\n" + "console.timeEnd('a')\r\nError: labels not implemented"} + {"console.timeEnd()\r\n" + "console.timeEnd()\r\nTimer \"default\" doesn’t exist."} +} + njs_test { {"console.ll()\r\n" "console.ll()\r\nTypeError: 'll' is not a function"}