Преглед изворни кода

Reworked memory performance tests

sanex3339 пре 5 година
родитељ
комит
6cec1f8737

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/index.cli.js


Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/index.js


+ 1 - 0
package.json

@@ -104,6 +104,7 @@
     "test:full": "scripts/test-full",
     "test:full": "scripts/test-full",
     "test:coveralls": "scripts/test-coveralls",
     "test:coveralls": "scripts/test-coveralls",
     "test:mocha": "scripts/test-mocha",
     "test:mocha": "scripts/test-mocha",
+    "test:mocha-memory-performance": "scripts/test-mocha-memory-performance",
     "test:removeTmpDir": "scripts/test-remove-tmp-dir",
     "test:removeTmpDir": "scripts/test-remove-tmp-dir",
     "test": "scripts/test",
     "test": "scripts/test",
     "eslint": "scripts/eslint",
     "eslint": "scripts/eslint",

+ 2 - 1
scripts/test-full

@@ -4,4 +4,5 @@ yarn run test:removeTmpDir &&
 yarn run test:compile &&
 yarn run test:compile &&
 node test-tmp/test/dev/dev.js &&
 node test-tmp/test/dev/dev.js &&
 $(yarn bin)/nyc --reporter text-summary $(yarn bin)/mocha -- test-tmp/test/index.spec.js --exit &&
 $(yarn bin)/nyc --reporter text-summary $(yarn bin)/mocha -- test-tmp/test/index.spec.js --exit &&
-yarn run test:removeTmpDir
+yarn run test:removeTmpDir &&
+yarn run test:mocha-memory-performance

+ 6 - 0
scripts/test-mocha-memory-performance

@@ -0,0 +1,6 @@
+#!/bin/bash
+
+yarn run test:removeTmpDir &&
+yarn run test:compile &&
+NODE_OPTIONS=--max-old-space-size=200 $(yarn bin)/mocha test-tmp/test/performance-tests/JavaScriptObfuscatorMemory.spec.js &&
+yarn run test:removeTmpDir

+ 73 - 0
test/performance-tests/JavaScriptObfuscatorMemory.spec.ts

@@ -0,0 +1,73 @@
+import { assert } from 'chai';
+
+import { readFileAsString } from '../helpers/readFileAsString';
+
+import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
+
+const heapValueToMB = (value: number) => Math.round(value / 1024 / 1024 * 100) / 100;
+
+describe('JavaScriptObfuscator memory', function () {
+    const iterationsCount: number = 500;
+    const gcDiffThreshold: number = 10;
+    const allowedHeapDiffThreshold: number = 40;
+
+    this.timeout(100000);
+
+    describe('memory: heap usage', () => {
+        it('should keep heap usage without memory leaks', () => {
+            const sourceCode: string = readFileAsString('./test/fixtures/sample.js');
+
+            const maxHeapUsed: number[] = [];
+            let prevHeapUsed: number | null = null;
+
+            for (let i: number = 0; i < iterationsCount; i++) {
+                JavaScriptObfuscator.obfuscate(
+                    sourceCode,
+                    {
+                        compact: true,
+                        controlFlowFlattening: true,
+                        controlFlowFlatteningThreshold: 0.75,
+                        deadCodeInjection: true,
+                        deadCodeInjectionThreshold: 0.4,
+                        debugProtection: false,
+                        debugProtectionInterval: false,
+                        disableConsoleOutput: true,
+                        identifierNamesGenerator: 'mangled',
+                        log: false,
+                        renameGlobals: false,
+                        rotateStringArray: true,
+                        selfDefending: true,
+                        shuffleStringArray: true,
+                        splitStrings: true,
+                        splitStringsChunkLength: 2,
+                        stringArray: true,
+                        stringArrayEncoding: 'base64',
+                        stringArrayThreshold: 0.75,
+                        transformObjectKeys: true,
+                        unicodeEscapeSequence: false
+                    }
+                );
+
+                const heap = process.memoryUsage();
+                const heapUsed: number = heapValueToMB(heap.heapUsed);
+
+                const gcDiff: number = (prevHeapUsed ?? heapUsed) - heapUsed;
+
+                if (prevHeapUsed && gcDiff > gcDiffThreshold) {
+                    maxHeapUsed.push(prevHeapUsed);
+                }
+
+                prevHeapUsed = heapUsed;
+            }
+
+            const sortedMaxHeapUsed: number[] = [...maxHeapUsed].sort((a: number, b: number) => a - b);
+
+            const firstMaxHeapMBUsed: number = sortedMaxHeapUsed[0];
+            const lastMaxHeapMbUsed: number = sortedMaxHeapUsed[sortedMaxHeapUsed.length - 1];
+
+            const diff: number = lastMaxHeapMbUsed - firstMaxHeapMBUsed;
+
+            assert.closeTo(diff, 0, allowedHeapDiffThreshold);
+        });
+    });
+});

+ 0 - 58
test/performance-tests/JavaScriptObfuscatorPerformance.spec.ts

@@ -4,8 +4,6 @@ import { readFileAsString } from '../helpers/readFileAsString';
 
 
 import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
 import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
 
 
-const heapValueToMB = (value: number) => Math.round(value / 1024 / 1024 * 100) / 100;
-
 describe('JavaScriptObfuscator performance', function () {
 describe('JavaScriptObfuscator performance', function () {
     const iterationsCount: number = 500;
     const iterationsCount: number = 500;
 
 
@@ -28,60 +26,4 @@ describe('JavaScriptObfuscator performance', function () {
             assert.isOk(true);
             assert.isOk(true);
         });
         });
     });
     });
-
-    describe('performance: heap usage', () => {
-        it('should keep heap usage without memory leaks', () => {
-            const sourceCode: string = readFileAsString('./test/fixtures/sample.js');
-
-            const maxHeapUsed: number[] = [];
-            let prevHeapUsed: number | null = null;
-
-            for (let i: number = 0; i < iterationsCount; i++) {
-                JavaScriptObfuscator.obfuscate(
-                    sourceCode,
-                    {
-                        compact: true,
-                        controlFlowFlattening: true,
-                        controlFlowFlatteningThreshold: 0.75,
-                        deadCodeInjection: true,
-                        deadCodeInjectionThreshold: 0.4,
-                        debugProtection: false,
-                        debugProtectionInterval: false,
-                        disableConsoleOutput: true,
-                        identifierNamesGenerator: 'mangled',
-                        log: false,
-                        renameGlobals: false,
-                        rotateStringArray: true,
-                        selfDefending: true,
-                        shuffleStringArray: true,
-                        splitStrings: true,
-                        splitStringsChunkLength: 2,
-                        stringArray: true,
-                        stringArrayEncoding: 'base64',
-                        stringArrayThreshold: 0.75,
-                        transformObjectKeys: true,
-                        unicodeEscapeSequence: false
-                    }
-                );
-
-                const used = process.memoryUsage();
-                const heapUsed: number = heapValueToMB(used.heapUsed);
-
-                if (prevHeapUsed !== null && heapUsed < prevHeapUsed) {
-                    maxHeapUsed.push(prevHeapUsed);
-                }
-
-                prevHeapUsed = heapUsed;
-            }
-
-            const sortedMaxHeapUsed: number[] = [...maxHeapUsed].sort();
-
-            const firstMaxHeapMBUsed: number = sortedMaxHeapUsed[0];
-            const lastMaxHeapMbUsed: number = sortedMaxHeapUsed[sortedMaxHeapUsed.length - 1];
-
-            const diff: number = lastMaxHeapMbUsed - firstMaxHeapMBUsed;
-
-            assert.closeTo(diff, 0, 20);
-        });
-    });
 });
 });

Неке датотеке нису приказане због велике количине промена