forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptObfuscatorMemory.spec.ts
More file actions
75 lines (58 loc) · 2.93 KB
/
Copy pathJavaScriptObfuscatorMemory.spec.ts
File metadata and controls
75 lines (58 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { assert } from 'chai';
import { readFileAsString } from '../helpers/readFileAsString';
import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
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 = 80;
this.timeout(250000);
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: [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] ?? 0;
const lastMaxHeapMbUsed: number = sortedMaxHeapUsed[sortedMaxHeapUsed.length - 1] ?? 0;
const diff: number = lastMaxHeapMbUsed - firstMaxHeapMBUsed;
assert.closeTo(diff, 0, allowedHeapDiffThreshold);
});
});
});