Skip to content

Commit b1bb4a9

Browse files
author
sanex3339
committed
Added heap usage performance test
1 parent e4ca9f7 commit b1bb4a9

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/performance-tests/JavaScriptObfuscatorPerformance.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { readFileAsString } from '../helpers/readFileAsString';
44

55
import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
66

7+
const heapValueToMB = (value: number) => Math.round(value / 1024 / 1024 * 100) / 100;
8+
79
describe('JavaScriptObfuscator performance', function () {
810
const iterationsCount: number = 500;
911

@@ -26,4 +28,60 @@ describe('JavaScriptObfuscator performance', function () {
2628
assert.isOk(true);
2729
});
2830
});
31+
32+
describe('performance: heap usage', () => {
33+
it('should keep heap usage without memory leakd', () => {
34+
const sourceCode: string = readFileAsString('./test/fixtures/sample.js');
35+
36+
const maxHeapUsed: number[] = [];
37+
let prevHeapUsed: number | null = null;
38+
39+
for (let i: number = 0; i < iterationsCount; i++) {
40+
JavaScriptObfuscator.obfuscate(
41+
sourceCode,
42+
{
43+
compact: true,
44+
controlFlowFlattening: true,
45+
controlFlowFlatteningThreshold: 0.75,
46+
deadCodeInjection: true,
47+
deadCodeInjectionThreshold: 0.4,
48+
debugProtection: false,
49+
debugProtectionInterval: false,
50+
disableConsoleOutput: true,
51+
identifierNamesGenerator: 'mangled',
52+
log: false,
53+
renameGlobals: false,
54+
rotateStringArray: true,
55+
selfDefending: true,
56+
shuffleStringArray: true,
57+
splitStrings: true,
58+
splitStringsChunkLength: 2,
59+
stringArray: true,
60+
stringArrayEncoding: 'base64',
61+
stringArrayThreshold: 0.75,
62+
transformObjectKeys: true,
63+
unicodeEscapeSequence: false
64+
}
65+
);
66+
67+
const used = process.memoryUsage();
68+
const heapUsed: number = heapValueToMB(used.heapUsed);
69+
70+
if (prevHeapUsed !== null && heapUsed < prevHeapUsed) {
71+
maxHeapUsed.push(prevHeapUsed);
72+
}
73+
74+
prevHeapUsed = heapUsed;
75+
}
76+
77+
const sortedMaxHeapUsed: number[] = [...maxHeapUsed].sort();
78+
79+
const firstMaxHeapMBUsed: number = sortedMaxHeapUsed[0];
80+
const lastMaxHeapMbUsed: number = sortedMaxHeapUsed[sortedMaxHeapUsed.length - 1];
81+
82+
const diff: number = lastMaxHeapMbUsed - firstMaxHeapMBUsed;
83+
84+
assert.closeTo(diff, 0, 20);
85+
});
86+
});
2987
});

0 commit comments

Comments
 (0)