Skip to content

Commit 18bd418

Browse files
authored
Added memory benchmark (#979)
1 parent 8d247d0 commit 18bd418

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"],
1919
// The `prefer-destructuring` rule causes issues
2020
// https://github.com/naptha/tesseract.js/issues/847
21-
"prefer-destructuring": 0
21+
"prefer-destructuring": 0,
22+
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }]
2223
}
2324
}

benchmarks/node/memory-benchmark.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
const path = require('path');
3+
const { createWorker, createScheduler } = require('../../src');
4+
5+
const formatBytes = (bytes) => `${(bytes / 1024 / 1024).toFixed(2)} MB`;
6+
const formatTime = (seconds) => `${seconds.toFixed(2)}s`;
7+
8+
const getMemoryRow = (iteration, memUsage, time) => `| ${iteration} | ${formatTime(time)} | ${formatBytes(memUsage.heapUsed)} | ${formatBytes(memUsage.heapTotal)} | ${formatBytes(memUsage.rss)} | ${formatBytes(memUsage.external)} |`;
9+
10+
const scheduler = createScheduler();
11+
12+
if (!global.gc) {
13+
console.log('Garbage collection unavailable. Pass --expose-gc '
14+
+ 'when launching node to enable forced garbage collection.');
15+
}
16+
17+
const workerGen = async () => {
18+
const worker = await createWorker('eng', 1, { cachePath: '.' });
19+
scheduler.addWorker(worker);
20+
};
21+
22+
(async () => {
23+
const fileArr = [
24+
path.join(__dirname, '..', 'data', 'meditations.jpg'),
25+
path.join(__dirname, '..', 'data', 'tyger.jpg'),
26+
path.join(__dirname, '..', 'data', 'testocr.png'),
27+
];
28+
29+
const workerN = 4;
30+
const resArr = Array(workerN);
31+
for (let i = 0; i < workerN; i++) {
32+
resArr[i] = workerGen();
33+
}
34+
await Promise.all(resArr);
35+
36+
// Print table header
37+
console.log('| Iteration | Time | Heap Used | Heap Total | RSS | External |');
38+
console.log('|-----------|------|------------|------------|-----|----------|');
39+
40+
for (let i = 0; i < 10; i++) {
41+
let iterationTime = 0;
42+
for (const file of fileArr) {
43+
const time1 = Date.now();
44+
const promises = [];
45+
for (let j = 0; j < 10; j++) {
46+
promises.push(scheduler.addJob('recognize', file));
47+
}
48+
// eslint-disable-next-line no-await-in-loop
49+
await Promise.all(promises);
50+
51+
if (global.gc) global.gc();
52+
53+
const time2 = Date.now();
54+
const timeDif = (time2 - time1) / 1e3;
55+
iterationTime += timeDif;
56+
}
57+
// Print memory stats and time after each iteration
58+
console.log(getMemoryRow(i + 1, process.memoryUsage(), iterationTime));
59+
}
60+
scheduler.terminate();
61+
})();

0 commit comments

Comments
 (0)