]> git.kaiwu.me - njs.git/commitdiff
Tests: add parser stress benchmarks
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 3 Sep 2026 00:39:41 +0000 (17:39 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 10 Sep 2026 03:11:04 +0000 (20:11 -0700)
Add generated workloads for large array literals, declarations, references,
closures, and labels.  These tests are intended to expose catastrophic parser
scaling and compare compilation changes; source construction and Function()
compilation are both included in the measured interval.

src/test/njs_benchmark.c

index cdb5954e30fd26dac03a033dec235453f3ffdecf..896f3fcbdd79b9b749f34c137709c227ade678fb 100644 (file)
@@ -119,6 +119,11 @@ njs_benchmark_test(njs_vm_t *parent, njs_opts_t *opts, njs_value_t *report,
     options.backtrace = 1;
     options.addons = njs_benchmark_addon_external_modules;
 
+    if (strncmp(test->name, "compile ", njs_length("compile ")) == 0) {
+        options.max_stack_size = 64 * 1024 * 1024;
+        options.unsafe = 1;
+    }
+
     vm = NULL;
     nvm = NULL;
     ret = NJS_ERROR;
@@ -286,6 +291,45 @@ static njs_benchmark_test_t  njs_test[] =
       njs_str("4"),
       100000 },
 
+    { "compile array references 1M",
+      njs_str("Function('var x; return [' + 'x,'.repeat(999999) + 'x]'); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile sequential block declarations 100K",
+      njs_str("Function('{let x=0;}'.repeat(100000)); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile sequential labels 100K",
+      njs_str("Function('a:;'.repeat(100000)); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile unique declarations 100K",
+      njs_str("Function(Array(100000).fill(0)"
+              ".map((v, i) => 'let x' + i + '=0;').join('')); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile referenced declarations 100K",
+      njs_str("Function(Array(100000).fill(0)"
+              ".map((v, i) => 'let x' + i + '=0;x' + i + ';').join('')); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile closures 20K",
+      njs_str("Function('void function(){let x=1;return ()=>x;};'"
+              ".repeat(20000)); 1"),
+      njs_str("1"),
+      1 },
+
+    { "compile nested labels 10K",
+      njs_str("Function(Array(10000).fill(0)"
+              ".map((v, i) => 'a' + i + ':').join('') + ';'); 1"),
+      njs_str("1"),
+      1 },
+
     { "string create 'abcdefABCDEF'",
       njs_str("benchmark.string('create', 'abcdef', 1000000)"),
       njs_str("undefined"),