Skip to content

Commit

Permalink
test: add benchmark with vitest + codspeed (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 22, 2024
1 parent 040611c commit 30ec92e
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/bench.yml
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 }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"unplugin": "^1.8.3"
},
"devDependencies": {
"@codspeed/vitest-plugin": "^3.1.0",
"@antfu/eslint-config": "2.8.0",
"@nuxt/kit": "3.10.3",
"@types/estree": "1.0.5",
Expand Down
97 changes: 96 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions test/transform.bench.ts
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
}
2 changes: 2 additions & 0 deletions vitest.config.ts → vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { fileURLToPath } from 'node:url'
import codspeedPlugin from '@codspeed/vitest-plugin'
import { defineConfig } from 'vitest/config'

export default defineConfig({
plugins: [codspeedPlugin()],
resolve: {
alias: {
'magic-regexp': fileURLToPath(new URL('./src/index.ts', import.meta.url).href),
Expand Down

0 comments on commit 30ec92e

Please sign in to comment.