Skip to content

Commit 80c9b9f

Browse files
committed
Move benchmarks top-level
1 parent 40f7e48 commit 80c9b9f

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { readFileSync, readdirSync, statSync } from 'node:fs';
33
import { dirname, join as pjoin } from 'node:path';
44
import { performance } from 'node:perf_hooks';
55
import { fileURLToPath } from 'node:url';
6-
import decodeQR from '../../src/decode.ts';
6+
import decodeQR from '../src/decode.ts';
77
// Reuse the curated expectations from decode tests; should.runWhen keeps tests idle on import.
8-
import { DECODED, DECODE_VECTOR_EXCLUDE } from '../decode.test.ts';
8+
import { DECODED, DECODE_VECTOR_EXCLUDE } from '../test/decode.test.ts';
99

1010
const _dirname = dirname(fileURLToPath(import.meta.url));
11-
const DETECTION_PATH = pjoin(_dirname, '..', 'vectors', 'boofcv-v3', 'detection');
11+
const DETECTION_PATH = pjoin(_dirname, '..', 'test', 'vectors', 'boofcv-v3', 'detection');
1212

1313
const listFiles = (path, isDir = false) =>
1414
readdirSync(path)

benchmark/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { bench } from '@paulmillr/jsbt/bench.js';
2+
import { deepStrictEqual } from 'node:assert';
3+
import decodeQR from '../src/decode.ts';
4+
import { Bitmap, encodeQR } from '../src/index.ts';
5+
6+
const section = (name) => console.log(`\n# ${name}`);
7+
8+
function imageFromText(text, opts = {}) {
9+
const { scale = 4, ...qrOpts } = opts;
10+
const raw = encodeQR(text, 'raw', { border: 4, ecc: 'medium', ...qrOpts });
11+
return new Bitmap(raw.length, raw).scale(scale).toImage();
12+
}
13+
14+
const smallText = 'HELLO WORLD';
15+
const largeText = 'H'.repeat(768);
16+
const smallImage = imageFromText(smallText, { version: 1, scale: 4 });
17+
const largeImage = imageFromText(largeText, { scale: 2 });
18+
19+
async function main() {
20+
section('encode');
21+
await bench('ascii small', () => encodeQR(smallText, 'ascii', { ecc: 'medium' }));
22+
await bench('gif small', () => encodeQR(smallText, 'gif', { ecc: 'medium' }));
23+
await bench('svg small', () => encodeQR(smallText, 'svg', { ecc: 'medium' }));
24+
25+
section('encode large');
26+
await bench('raw large', () => encodeQR(largeText, 'raw', { ecc: 'medium' }));
27+
await bench('ascii large', () => encodeQR(largeText, 'ascii', { ecc: 'medium' }));
28+
29+
section('decode generated');
30+
await bench('v1 scale4', () => deepStrictEqual(decodeQR(smallImage), smallText));
31+
await bench('large scale2', () => deepStrictEqual(decodeQR(largeImage), largeText));
32+
}
33+
34+
main();
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { fileURLToPath } from 'node:url';
1313
import * as qrcodeGenerator from 'qrcode-generator';
1414

1515
const decodeExp = 'https://www.surveymonkey.com/s/TheClubatLAS_T3';
16-
const decodeJPG = jpeg.decode(readFileSync('../vectors/boofcv-v3/detection/blurred/image007.jpg'));
16+
const decodeJPG = jpeg.decode(
17+
readFileSync('../../test/vectors/boofcv-v3/detection/blurred/image007.jpg')
18+
);
1719

1820
// Compared to other JS libraries:
1921
// - Don't work: [jsQR](https://github.com/cozmo/jsQR) is dead, [zxing-js](https://github.com/zxing-js/) is [dead](https://github.com/zxing-js/library/commit/b797504c25454db32aa2db410e6502b6db12a401), [qr-scanner](https://github.com/nimiq/qr-scanner/) uses jsQR, doesn't work outside of browser, [qcode-decoder](https://github.com/cirocosta/qcode-decoder) broken version of jsQR, doesn't work outside of browser
@@ -97,7 +99,7 @@ async function main() {
9799

98100
section('Decoding quality');
99101
const _dirname = dirname(fileURLToPath(import.meta.url));
100-
const DETECTION_PATH = pjoin(_dirname, '..', 'vectors', 'boofcv-v3', 'detection');
102+
const DETECTION_PATH = pjoin(_dirname, '..', '..', 'test', 'vectors', 'boofcv-v3', 'detection');
101103

102104
for (const category of listFiles(DETECTION_PATH, true)) {
103105
const DIR_PATH = `${DETECTION_PATH}/${category}`;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"main": "index.js",
77
"type": "module",
88
"scripts": {
9-
"bench": "node index.ts",
10-
"quality": "node decode-quality.ts"
9+
"benchmark": "node index.ts"
1110
},
1211
"keywords": [],
1312
"author": "",

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
"build": "tsc",
3636
"bundle": "npx --no @paulmillr/jsbt bundle test/build",
3737
"check": "npx --no @paulmillr/jsbt check",
38-
"bench": "cd test/benchmark && npm ci && node index.ts",
39-
"bench:quality": "node test/benchmark/decode-quality.ts",
38+
"benchmark": "node benchmark/index.ts",
39+
"benchmark:quality": "node benchmark/decode-quality.ts",
40+
"benchmark:thirdparty": "cd benchmark/thirdparty && npm ci && node index.ts",
4041
"format": "prettier --write src",
4142
"test": "node --no-warnings test/index.ts",
4243
"test:bun": "bun test/index.ts",

0 commit comments

Comments
 (0)