Welcome! Log In Create A New Profile

Advanced

[njs] console.time() and console.timeEnd() methods.

Dmitry Volyntsev
November 15, 2018 12:32PM
details: http://hg.nginx.org/njs/rev/46632012ac03
branches:
changeset: 653:46632012ac03
user: Artem S. Povalyukhin <artem.povaluhin@gmail.com>
date: Wed Nov 14 18:14:49 2018 +0300
description:
console.time() and console.timeEnd() methods.

This fixes #62 issue on Github.

diffstat:

njs/njs_shell.c | 86 ++++++++++++++++++++++++++++++++++++++++++++
njs/test/njs_expect_test.exp | 18 ++++++++-
2 files changed, 103 insertions(+), 1 deletions(-)

diffs (156 lines):

diff -r 7f0f7d149709 -r 46632012ac03 njs/njs_shell.c
--- a/njs/njs_shell.c Thu Nov 15 12:45:02 2018 +0300
+++ b/njs/njs_shell.c Wed Nov 14 18:14:49 2018 +0300
@@ -14,6 +14,7 @@
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
+#include <sys/time.h>
#include <locale.h>

#include <readline.h>
@@ -63,6 +64,10 @@ static njs_ret_t njs_ext_console_dump(nj
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_v

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 -r 7f0f7d149709 -r 46632012ac03 njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp Thu Nov 15 12:45:02 2018 +0300
+++ b/njs/test/njs_expect_test.exp Wed Nov 14 18:14:49 2018 +0300
@@ -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"}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] console.time() and console.timeEnd() methods.

Dmitry Volyntsev 479 November 15, 2018 12:32PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 318
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready