diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml new file mode 100644 index 0000000000..b4db4801e0 --- /dev/null +++ b/.github/workflows/benchmarks.yml @@ -0,0 +1,48 @@ +name: Benchmarks + +on: + push: + branches: [main] + pull_request: + types: [opened, synchronize] + +jobs: + benchmarks: + permissions: + pull-requests: write + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: pnpm/action-setup@v2 + with: + version: 8.2.0 + - uses: actions/setup-node@v3 + with: + node-version: 18 + cache: 'pnpm' + cache-dependency-path: '**/pnpm-lock.yaml' + - run: pnpm install --frozen-lockfile + - run: pnpm run benchmarks + - run: | + FILES=("benchmarks/simple-read.json" "benchmarks/simple-write.json" "benchmarks/subscribe-write.json") + echo "## Benchmark Results" > table.md + for file in "${FILES[@]}"; do + if [ -f "$file" ]; then + name=$(jq -r '.name' "$file") + echo "### $name" >> table.md + echo "| Test Case | Ops/sec | Margin of Error |" >> table.md + echo "|-----------|---------|-----------------|" >> table.md + jq -r '.results[] | "| \(.name) | \(.ops) | \(.margin)% |"' "$file" >> table.md + echo "" >> table.md + fi + done + cat table.md + - run: | + table_content=$(cat table.md) + echo "table_content<> $GITHUB_ENV + echo "$table_content" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + - uses: peter-evans/create-or-update-comment@v2 + with: + issue-number: ${{ github.event.pull_request.number }} + body: ${{ env.table_content }} diff --git a/benchmarks/simple-read.ts b/benchmarks/simple-read.ts index da94008271..bbfbe4319e 100755 --- a/benchmarks/simple-read.ts +++ b/benchmarks/simple-read.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env npx tsx - import { add, complete, cycle, save, suite } from 'benny' import { atom } from '../src/vanilla/atom.ts' import type { PrimitiveAtom } from '../src/vanilla/atom.ts' diff --git a/benchmarks/simple-write.ts b/benchmarks/simple-write.ts index d02054f9e3..ca237316e2 100755 --- a/benchmarks/simple-write.ts +++ b/benchmarks/simple-write.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env npx tsx - import { add, complete, cycle, save, suite } from 'benny' import { atom } from '../src/vanilla/atom.ts' import type { PrimitiveAtom } from '../src/vanilla/atom.ts' diff --git a/benchmarks/subscribe-write.ts b/benchmarks/subscribe-write.ts index 99c7ba80dd..62e4552787 100755 --- a/benchmarks/subscribe-write.ts +++ b/benchmarks/subscribe-write.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env npx tsx - import { add, complete, cycle, save, suite } from 'benny' import { atom } from '../src/vanilla/atom.ts' import type { PrimitiveAtom } from '../src/vanilla/atom.ts' diff --git a/package.json b/package.json index 25ed75a05a..2d545e8341 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,11 @@ "patch-ts3.8": "node -e \"require('shelljs').find('dist/ts3.8/**/*.d.ts').forEach(f=>require('fs').appendFileSync(f,'declare type Awaited = T extends Promise ? V : T;'))\"", "patch-old-ts": "shx touch dist/ts_version_3.8_and_above_is_required.d.ts", "patch-esm-ts": "node -e \"require('shelljs').find('dist/esm/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.mts');require('fs').renameSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.mjs\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.mjs\\'',f2)})\"", - "patch-readme": "shx sed -i 's/.*Jotai \\(dark mode\\).*//' dist/readme.md" + "patch-readme": "shx sed -i 's/.*Jotai \\(dark mode\\).*//' dist/readme.md", + "benchmarks": "pnpm run --parallel '/^benchmark:.*/'", + "benchmark:read": "tsx benchmarks/simple-read.ts", + "benchmark:write": "tsx benchmarks/simple-write.ts", + "benchmark:subscribe-write": "tsx benchmarks/subscribe-write.ts" }, "engines": { "node": ">=12.20.0" @@ -171,7 +175,8 @@ "tslib": "^2.6.3", "typescript": "^5.5.3", "vitest": "^2.0.1", - "wonka": "^6.3.4" + "wonka": "^6.3.4", + "tsx": "^4.16.2" }, "peerDependencies": { "@types/react": ">=17.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ad01a9a8d..722d32d7da 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -164,6 +164,9 @@ devDependencies: tslib: specifier: ^2.6.3 version: 2.6.3 + tsx: + specifier: ^4.16.2 + version: 4.16.2 typescript: specifier: ^5.5.3 version: 5.5.3 @@ -3318,7 +3321,7 @@ packages: dependencies: semver: 7.6.2 shelljs: 0.8.5 - typescript: 5.6.0-dev.20240708 + typescript: 5.6.0-dev.20240713 dev: true /eastasianwidth@0.2.0: @@ -5799,6 +5802,17 @@ packages: resolution: {integrity: sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==} dev: true + /tsx@4.16.2: + resolution: {integrity: sha512-C1uWweJDgdtX2x600HjaFaucXTilT7tgUZHbOE4+ypskZ1OP8CRCSDkCxG6Vya9EwaFIVagWwpaVAn5wzypaqQ==} + engines: {node: '>=18.0.0'} + hasBin: true + dependencies: + esbuild: 0.21.5 + get-tsconfig: 4.7.5 + optionalDependencies: + fsevents: 2.3.3 + dev: true + /type-check@0.4.0: resolution: {integrity: sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==} engines: {node: '>= 0.8.0'} @@ -5866,8 +5880,8 @@ packages: hasBin: true dev: true - /typescript@5.6.0-dev.20240708: - resolution: {integrity: sha512-znYUhYs3kjemvesSDNvtU17+eUiQnX4RNegVbFdWQ54mXzYrg+0nI2isOO/RQCeGHFuO1M6uR/2/bC+mcDnAVQ==} + /typescript@5.6.0-dev.20240713: + resolution: {integrity: sha512-J+4LjdEKAKO95oQ2Ff870vHVxa57ZMVSbfpyL0L5daYrnu48lQiMsXNDiG9IPT2RWF221Qt+LGxjug74XbeseQ==} engines: {node: '>=14.17'} hasBin: true dev: true