chore(napi): add oxc-parser to benchmarks (#2724)

Closes #2616.

Adds benchmarks for NodeJS NAPI build. Measurement includes `JSON.parse`
of the AST on JS side, since that's how it'll be used 99% of the time.

Benchmarks run against same files as Rust parser benchmarks, so we can
see the overhead of transferring AST to JS.
This commit is contained in:
overlookmotel 2024-03-15 11:45:02 +00:00 committed by GitHub
parent d7004da253
commit af47aebc7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1090 additions and 4 deletions

View file

@ -38,6 +38,12 @@ jobs:
shared-key: 'benchmark'
save-cache: ${{ github.ref_name == 'main' }}
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'
- name: Install codspeed
uses: taiki-e/install-action@v2
with:
@ -59,9 +65,19 @@ jobs:
mv target/release/deps/codegen_sourcemap-* target/codspeed/oxc_benchmark
rm -rf target/codspeed/oxc_benchmark/*.d
- name: Build NAPI benchmark
working-directory: ./napi/parser
run: |
corepack enable
pnpm install
pnpm run build
- name: Run benchmark
uses: CodSpeedHQ/action@v2
timeout-minutes: 30
with:
run: cargo codspeed run
run: |
cargo codspeed run
cd napi/parser
pnpm run bench
token: ${{ secrets.CODSPEED_TOKEN }}

View file

@ -3,17 +3,20 @@
"private": true,
"scripts": {
"build": "napi build --platform --release",
"test": "node test.mjs"
"test": "node test.mjs",
"bench": "vitest bench"
},
"devDependencies": {
"@codspeed/vitest-plugin": "^3.1.0",
"@napi-rs/cli": "^2.18.0",
"es-module-lexer": "^1.4.1",
"flatbuffers": "^23.5.26"
"flatbuffers": "^23.5.26",
"vitest": "^1.3.1"
},
"engines": {
"node": ">=14.*"
},
"packageManager": "pnpm@8.2.0",
"packageManager": "pnpm@8.15.4+sha256.cea6d0bdf2de3a0549582da3983c70c92ffc577ff4410cbf190817ddc35137c2",
"napi": {
"name": "parser",
"triples": {

View file

@ -0,0 +1,48 @@
import {fileURLToPath} from 'url';
import {join as pathJoin} from 'path';
import {readFile, writeFile} from 'fs/promises';
import assert from 'assert';
import {bench} from 'vitest';
import {parseSync} from './index.js';
const urls = [
// TypeScript syntax (2.81MB)
'https://raw.githubusercontent.com/microsoft/TypeScript/v5.3.3/src/compiler/checker.ts',
// Real world app tsx (1.0M)
'https://raw.githubusercontent.com/oxc-project/benchmark-files/main/cal.com.tsx',
// Real world content-heavy app jsx (3K)
'https://raw.githubusercontent.com/oxc-project/benchmark-files/main/RadixUIAdoptionSection.jsx',
// Heavy with classes (554K)
'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.0.269/build/pdf.mjs',
// ES5 (3.9M)
'https://cdn.jsdelivr.net/npm/antd@5.12.5/dist/antd.js',
];
// Same directory as Rust benchmarks use for downloaded files
const cacheDirPath = pathJoin(fileURLToPath(import.meta.url), '../../../target');
const files = await Promise.all(urls.map(async (url) => {
const filename = url.split('/').at(-1),
path = pathJoin(cacheDirPath, filename);
let code;
try {
code = await readFile(path, 'utf8');
console.log('Found cached file:', filename);
} catch {
console.log('Downloading:', filename);
const res = await fetch(url);
code = await res.text();
await writeFile(path, code);
}
return {filename, code};
}));
for (const {filename, code} of files) {
bench(`parser(napi)[${filename}]`, () => {
const res = parseSync(code, {sourceFilename: filename});
assert(res.errors.length === 0);
JSON.parse(res.program);
});
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
import {defineConfig} from 'vitest/config';
import codspeedPlugin from '@codspeed/vitest-plugin';
export default defineConfig({
plugins: [codspeedPlugin()]
});