[njs] Splitting unit tests and benchmark.

Dmitry Volyntsev xeioex at nginx.com
Fri Jul 7 16:17:25 UTC 2017


details:   http://hg.nginx.org/njs/rev/0ad7f75f9dbf
branches:  
changeset: 385:0ad7f75f9dbf
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jul 07 19:17:26 2017 +0300
description:
Splitting unit tests and benchmark.

diffstat:

 Makefile                 |   12 ++
 njs/test/njs_benchmark.c |  220 +++++++++++++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  153 --------------------------------
 3 files changed, 232 insertions(+), 153 deletions(-)

diffs (424 lines):

diff -r d09638142829 -r 0ad7f75f9dbf Makefile
--- a/Makefile	Thu Jul 06 19:09:56 2017 +0300
+++ b/Makefile	Fri Jul 07 19:17:26 2017 +0300
@@ -76,6 +76,7 @@ all:	test lib_test
 
 test:	\
 	$(NXT_BUILDDIR)/njs_unit_test \
+	$(NXT_BUILDDIR)/njs_benchmark \
 
 	$(NXT_BUILDDIR)/njs_unit_test d
 
@@ -396,4 +397,15 @@ dist:
 		$(NXT_BUILDDIR)/libnjs.a \
 		-lm $(NXT_PCRE_LIB)
 
+$(NXT_BUILDDIR)/njs_benchmark: \
+	$(NXT_BUILDDIR)/libnxt.a \
+	$(NXT_BUILDDIR)/libnjs.a \
+	njs/test/njs_benchmark.c \
+
+	$(NXT_CC) -o $(NXT_BUILDDIR)/njs_benchmark $(NXT_CFLAGS) \
+		-I$(NXT_LIB) -Injs \
+		njs/test/njs_benchmark.c \
+		$(NXT_BUILDDIR)/libnjs.a \
+		-lm $(NXT_PCRE_LIB)
+
 include $(NXT_LIB)/Makefile
diff -r d09638142829 -r 0ad7f75f9dbf njs/test/njs_benchmark.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/njs/test/njs_benchmark.c	Fri Jul 07 19:17:26 2017 +0300
@@ -0,0 +1,220 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) NGINX, Inc.
+ */
+
+#include <nxt_auto_config.h>
+#include <nxt_types.h>
+#include <nxt_clang.h>
+#include <nxt_string.h>
+#include <nxt_stub.h>
+#include <nxt_malloc.h>
+#include <nxt_array.h>
+#include <nxt_lvlhsh.h>
+#include <nxt_mem_cache_pool.h>
+#include <njscript.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/resource.h>
+#include <time.h>
+
+
+static void *
+njs_alloc(void *mem, size_t size)
+{
+    return nxt_malloc(size);
+}
+
+
+static void *
+njs_zalloc(void *mem, size_t size)
+{
+    void  *p;
+
+    p = nxt_malloc(size);
+
+    if (p != NULL) {
+        memset(p, 0, size);
+    }
+
+    return p;
+}
+
+
+static void *
+njs_align(void *mem, size_t alignment, size_t size)
+{
+    return nxt_memalign(alignment, size);
+}
+
+
+static void
+njs_free(void *mem, void *p)
+{
+    nxt_free(p);
+}
+
+
+static const nxt_mem_proto_t  njs_mem_cache_pool_proto = {
+    njs_alloc,
+    njs_zalloc,
+    njs_align,
+    NULL,
+    njs_free,
+    NULL,
+    NULL,
+};
+
+
+static nxt_int_t
+njs_unit_test_benchmark(nxt_str_t *script, nxt_str_t *result, const char *msg,
+    nxt_uint_t n)
+{
+    u_char                *start;
+    njs_vm_t              *vm, *nvm;
+    uint64_t              us;
+    nxt_int_t             ret;
+    nxt_str_t             s;
+    nxt_uint_t            i;
+    nxt_bool_t            success;
+    njs_vm_opt_t          options;
+    struct rusage         usage;
+    nxt_mem_cache_pool_t  *mcp;
+
+    mcp = nxt_mem_cache_pool_create(&njs_mem_cache_pool_proto, NULL, NULL,
+                                    2 * nxt_pagesize(), 128, 512, 16);
+    if (nxt_slow_path(mcp == NULL)) {
+        return NXT_ERROR;
+    }
+
+    options.mcp = mcp;
+    options.shared = NULL;
+    options.externals = NULL;
+
+    vm = njs_vm_create(&options);
+    if (vm == NULL) {
+        return NXT_ERROR;
+    }
+
+    start = script->start;
+
+    ret = njs_vm_compile(vm, &start, start + script->length);
+    if (ret != NXT_OK) {
+        return NXT_ERROR;
+    }
+
+    for (i = 0; i < n; i++) {
+
+        nvm = njs_vm_clone(vm, NULL, NULL);
+        if (nvm == NULL) {
+            return NXT_ERROR;
+        }
+
+        if (njs_vm_run(nvm) == NXT_OK) {
+            if (njs_vm_retval(nvm, &s) != NXT_OK) {
+                return NXT_ERROR;
+            }
+
+        } else {
+            njs_vm_exception(nvm, &s);
+        }
+
+        success = nxt_strstr_eq(result, &s);
+
+        if (!success) {
+            return NXT_ERROR;
+        }
+
+        njs_vm_destroy(nvm);
+    }
+
+    nxt_mem_cache_pool_destroy(mcp);
+
+    getrusage(RUSAGE_SELF, &usage);
+
+    us = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec
+         + usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;
+
+    if (n == 1) {
+        printf("%s: %.3fs\n", msg, (double) us / 1000000);
+
+    } else {
+        printf("%s: %.3fµs, %d times/s\n",
+               msg, (double) us / n, (int) ((uint64_t) n * 1000000 / us));
+    }
+
+    return NXT_OK;
+}
+
+
+int nxt_cdecl
+main(int argc, char **argv)
+{
+    static nxt_str_t  script = nxt_string("null");
+    static nxt_str_t  result = nxt_string("null");
+
+    static nxt_str_t  fibo_number = nxt_string(
+        "function fibo(n) {"
+        "    if (n > 1)"
+        "        return fibo(n - 1) + fibo(n - 2)"
+        "    return 1"
+        "}"
+        "fibo(32)");
+
+    static nxt_str_t  fibo_ascii = nxt_string(
+        "function fibo(n) {"
+        "    if (n > 1)"
+        "        return fibo(n - 1) + fibo(n - 2)"
+        "    return '.'"
+        "}"
+        "fibo(32).length");
+
+    static nxt_str_t  fibo_bytes = nxt_string(
+        "var a = '\\x80'.toBytes();"
+        "function fibo(n) {"
+        "    if (n > 1)"
+        "        return fibo(n - 1) + fibo(n - 2)"
+        "    return a"
+        "}"
+        "fibo(32).length");
+
+    static nxt_str_t  fibo_utf8 = nxt_string(
+        "function fibo(n) {"
+        "    if (n > 1)"
+        "        return fibo(n - 1) + fibo(n - 2)"
+        "    return 'α'"
+        "}"
+        "fibo(32).length");
+
+    static nxt_str_t  fibo_result = nxt_string("3524578");
+
+
+    if (argc > 1) {
+        switch (argv[1][0]) {
+
+        case 'v':
+            return njs_unit_test_benchmark(&script, &result,
+                                           "nJSVM clone/destroy", 1000000);
+
+        case 'n':
+            return njs_unit_test_benchmark(&fibo_number, &fibo_result,
+                                           "fibobench numbers", 1);
+
+        case 'a':
+            return njs_unit_test_benchmark(&fibo_ascii, &fibo_result,
+                                           "fibobench ascii strings", 1);
+
+        case 'b':
+            return njs_unit_test_benchmark(&fibo_bytes, &fibo_result,
+                                           "fibobench byte strings", 1);
+
+        case 'u':
+            return njs_unit_test_benchmark(&fibo_utf8, &fibo_result,
+                                           "fibobench utf8 strings", 1);
+        }
+    }
+
+    printf("unknown agrument\n");
+    return EXIT_FAILURE;
+}
diff -r d09638142829 -r 0ad7f75f9dbf njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Jul 06 19:09:56 2017 +0300
+++ b/njs/test/njs_unit_test.c	Fri Jul 07 19:17:26 2017 +0300
@@ -8224,169 +8224,16 @@ njs_unit_test(nxt_bool_t disassemble)
 }
 
 
-static nxt_int_t
-njs_unit_test_benchmark(nxt_str_t *script, nxt_str_t *result, const char *msg,
-    nxt_uint_t n)
-{
-    void                  *ext_object;
-    u_char                *start;
-    njs_vm_t              *vm, *nvm;
-    uint64_t              us;
-    nxt_int_t             ret;
-    nxt_str_t             s;
-    nxt_uint_t            i;
-    nxt_bool_t            success;
-    nxt_lvlhsh_t          externals;
-    njs_vm_opt_t          options;
-    struct rusage         usage;
-    njs_unit_test_req     r;
-    nxt_mem_cache_pool_t  *mcp;
-
-    mcp = nxt_mem_cache_pool_create(&njs_mem_cache_pool_proto, NULL, NULL,
-                                    2 * nxt_pagesize(), 128, 512, 16);
-    if (nxt_slow_path(mcp == NULL)) {
-        return NXT_ERROR;
-    }
-
-    r.mem_cache_pool = mcp;
-    r.uri.length = 6;
-    r.uri.start = (u_char *) "АБВ";
-
-    ext_object = &r;
-
-    if (njs_unit_test_externals(&externals, mcp) != NXT_OK) {
-        return NXT_ERROR;
-    }
-
-    options.mcp = mcp;
-    options.shared = NULL;
-    options.externals = &externals;
-
-    vm = njs_vm_create(&options);
-    if (vm == NULL) {
-        return NXT_ERROR;
-    }
-
-    start = script->start;
-
-    ret = njs_vm_compile(vm, &start, start + script->length);
-    if (ret != NXT_OK) {
-        return NXT_ERROR;
-    }
-
-    for (i = 0; i < n; i++) {
-
-        nvm = njs_vm_clone(vm, NULL, &ext_object);
-        if (nvm == NULL) {
-            return NXT_ERROR;
-        }
-
-        if (njs_vm_run(nvm) == NXT_OK) {
-            if (njs_vm_retval(nvm, &s) != NXT_OK) {
-                return NXT_ERROR;
-            }
-
-        } else {
-            njs_vm_exception(nvm, &s);
-        }
-
-        success = nxt_strstr_eq(result, &s);
-
-        if (!success) {
-            return NXT_ERROR;
-        }
-
-        njs_vm_destroy(nvm);
-    }
-
-    nxt_mem_cache_pool_destroy(mcp);
-
-    getrusage(RUSAGE_SELF, &usage);
-
-    us = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec
-         + usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;
-
-    if (n == 1) {
-        printf("%s: %.3fs\n", msg, (double) us / 1000000);
-
-    } else {
-        printf("%s: %.3fµs, %d times/s\n",
-               msg, (double) us / n, (int) ((uint64_t) n * 1000000 / us));
-    }
-
-    return NXT_OK;
-}
-
-
 int nxt_cdecl
 main(int argc, char **argv)
 {
     nxt_bool_t  disassemble;
 
-    static nxt_str_t  script = nxt_string("null");
-    static nxt_str_t  result = nxt_string("null");
-
-    static nxt_str_t  fibo_number = nxt_string(
-        "function fibo(n) {"
-        "    if (n > 1)"
-        "        return fibo(n - 1) + fibo(n - 2)"
-        "    return 1"
-        "}"
-        "fibo(32)");
-
-    static nxt_str_t  fibo_ascii = nxt_string(
-        "function fibo(n) {"
-        "    if (n > 1)"
-        "        return fibo(n - 1) + fibo(n - 2)"
-        "    return '.'"
-        "}"
-        "fibo(32).length");
-
-    static nxt_str_t  fibo_bytes = nxt_string(
-        "var a = '\\x80'.toBytes();"
-        "function fibo(n) {"
-        "    if (n > 1)"
-        "        return fibo(n - 1) + fibo(n - 2)"
-        "    return a"
-        "}"
-        "fibo(32).length");
-
-    static nxt_str_t  fibo_utf8 = nxt_string(
-        "function fibo(n) {"
-        "    if (n > 1)"
-        "        return fibo(n - 1) + fibo(n - 2)"
-        "    return 'α'"
-        "}"
-        "fibo(32).length");
-
-    static nxt_str_t  fibo_result = nxt_string("3524578");
-
-
     disassemble = 0;
 
     if (argc > 1) {
         switch (argv[1][0]) {
 
-        case 'v':
-            return njs_unit_test_benchmark(&script, &result,
-                                           "nJSVM clone/destroy", 1000000);
-
-        case 'n':
-            return njs_unit_test_benchmark(&fibo_number, &fibo_result,
-                                           "fibobench numbers", 1);
-
-        case 'a':
-            return njs_unit_test_benchmark(&fibo_ascii, &fibo_result,
-                                           "fibobench ascii strings", 1);
-
-        case 'b':
-            return njs_unit_test_benchmark(&fibo_bytes, &fibo_result,
-                                           "fibobench byte strings", 1);
-
-        case 'u':
-            return njs_unit_test_benchmark(&fibo_utf8, &fibo_result,
-                                           "fibobench utf8 strings", 1);
-
         case 'd':
             disassemble = 1;
             break;


More information about the nginx-devel mailing list