-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add benchmark with vitest + codspeed (#363)
- Loading branch information
Showing
5 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: benchmark | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: pnpm/action-setup@v2 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
cache: pnpm | ||
- name: Install dependencies | ||
run: pnpm install | ||
- name: Run benchmarks | ||
uses: CodSpeedHQ/action@v2 | ||
with: | ||
run: pnpm vitest bench | ||
# token retrieved from the CodSpeed app at the previous step | ||
token: ${{ secrets.CODSPEED_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { bench, describe } from 'vitest' | ||
import { parse } from 'acorn' | ||
|
||
import { MagicRegExpTransformPlugin } from '../src/transform' | ||
|
||
describe(`transformer: magic-regexp`, () => { | ||
const couldTransform = [ | ||
`import { createRegExp, exactly, anyOf } from 'magic-regexp'`, | ||
"const re1 = createRegExp(exactly('bar').notBefore('foo'))", | ||
] | ||
|
||
bench('ignores non-JS files', () => { | ||
transform(couldTransform, 'test.css') | ||
}) | ||
|
||
bench('transforms vue script blocks', () => { | ||
transform(couldTransform, 'test.vue?type=script') | ||
transform(couldTransform, 'test.vue') | ||
transform(couldTransform, 'test.vue?type=template') | ||
}) | ||
|
||
bench(`ignores code without imports from magic-regexp`, () => { | ||
transform(couldTransform[1]) | ||
transform([`// magic-regexp`, couldTransform[1]]) | ||
}) | ||
|
||
bench('preserves context for dynamic regexps', () => { | ||
transform([ | ||
`import { createRegExp } from 'magic-regexp'`, | ||
`console.log(createRegExp(anyOf(keys)))`, | ||
]) | ||
}) | ||
|
||
bench('statically replaces regexps where possible', () => { | ||
transform([ | ||
"import { something } from 'other-module'", | ||
`import { createRegExp, exactly, anyOf } from 'magic-regexp'`, | ||
'//', // this lets us tree-shake the import for use in our test-suite | ||
"const re1 = createRegExp(exactly('bar').notBefore('foo'))", | ||
"const re2 = createRegExp(anyOf(exactly('bar'), 'foo'))", | ||
"const re3 = createRegExp('/foo/bar')", | ||
// This line will be double-escaped in the snapshot | ||
"re3.test('/foo/bar')", | ||
]) | ||
}) | ||
|
||
bench('respects how users import library', () => { | ||
transform([ | ||
`import { createRegExp as cRE } from 'magic-regexp'`, | ||
`import { exactly as ext, createRegExp } from 'magic-regexp'`, | ||
`import * as magicRE from 'magic-regexp'`, | ||
"const re1 = cRE(ext('bar').notBefore('foo'))", | ||
"const re2 = magicRE.createRegExp(magicRE.anyOf('bar', 'foo'))", | ||
"const re3 = createRegExp('test/value')", | ||
]) | ||
}) | ||
}) | ||
|
||
const transform = (code: string | string[], id = 'some-id.js') => { | ||
const plugin = MagicRegExpTransformPlugin.vite() as any | ||
return plugin.transform.call( | ||
{ parse: (code: string) => parse(code, { ecmaVersion: 2022, sourceType: 'module' }) }, | ||
Array.isArray(code) ? code.join('\n') : code, | ||
id | ||
)?.code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters