[njs] Added function micro benchmarks.

Dmitry Volyntsev xeioex at nginx.com
Fri Apr 30 10:11:39 UTC 2021


details:   https://hg.nginx.org/njs/rev/282a13ecd193
branches:  
changeset: 1627:282a13ecd193
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Apr 30 10:10:57 2021 +0000
description:
Added function micro benchmarks.

diffstat:

 src/test/njs_benchmark.c |  52 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 45 insertions(+), 7 deletions(-)

diffs (98 lines):

diff -r 0746b2ed76cc -r 282a13ecd193 src/test/njs_benchmark.c
--- a/src/test/njs_benchmark.c	Fri Apr 16 13:46:46 2021 +0000
+++ b/src/test/njs_benchmark.c	Fri Apr 30 10:10:57 2021 +0000
@@ -35,7 +35,7 @@ njs_benchmark_test(njs_vm_t *parent, njs
 {
     u_char                *start;
     njs_vm_t              *vm, *nvm;
-    uint64_t              us;
+    uint64_t              ns;
     njs_int_t             ret, proto_id;
     njs_str_t             s, *expected;
     njs_uint_t            i, n;
@@ -76,7 +76,7 @@ njs_benchmark_test(njs_vm_t *parent, njs
     expected = &test->result;
 
     ret = NJS_ERROR;
-    us = njs_time() / 1000;
+    ns = njs_time();
 
     for (i = 0; i < n; i++) {
 
@@ -105,18 +105,18 @@ njs_benchmark_test(njs_vm_t *parent, njs
         nvm = NULL;
     }
 
-    us = njs_time() / 1000 - us;
+    ns = njs_time() - ns;
 
     if (!opts->dump_report) {
         if (n == 1) {
             njs_printf("%s%s: %.3fs\n", opts->previous ? "    " : "",
-                       test->name, (double) us / 1000000);
+                       test->name, (double) ns / 1000000000);
 
         } else {
             njs_printf("%s%s: %.3fµs, %d times/s\n",
                        opts->previous ? "    " : "",
-                       test->name, (double) us / n,
-                       (int) ((uint64_t) n * 1000000 / us));
+                       test->name, (double) ns / n / 1000,
+                       (int) ((uint64_t) n * 1000000000 / ns));
         }
     }
 
@@ -133,7 +133,7 @@ njs_benchmark_test(njs_vm_t *parent, njs
         goto done;
     }
 
-    njs_value_number_set(&usec, us);
+    njs_value_number_set(&usec, 1000 * ns);
     njs_value_number_set(&times, n);
 
     ret = njs_vm_object_alloc(parent, result, &name_key, &name,
@@ -165,6 +165,44 @@ static njs_benchmark_test_t  njs_test[] 
       njs_str("null"),
       1000000 },
 
+    { "func call",
+      njs_str("function test(a) { return 1 }"
+              ""
+              "test(1);"
+              "test(1);"
+              "test(1);"
+              "test(1);"),
+      njs_str("1"),
+      100000 },
+
+    { "func call (3 local functions)",
+      njs_str("function test(a) { "
+              "    function g(x) {}"
+              "    function h(x) {}"
+              "    function f(x) {}"
+              "    return 1;"
+              "}"
+              ""
+              "test(1);"
+              "test(1);"
+              "test(1);"
+              "test(1);"),
+      njs_str("1"),
+      100000 },
+
+    { "closure var global",
+      njs_str("function test(a) { sum++ }"
+              ""
+              "var sum = 0;"
+              ""
+              "test(1);"
+              "test(1);"
+              "test(1);"
+              "test(1);"
+              "sum"),
+      njs_str("4"),
+      100000 },
+
     { "JSON.parse",
       njs_str("JSON.parse('{\"a\":123, \"XXX\":[3,4,null]}').a"),
       njs_str("123"),


More information about the nginx-devel mailing list