diff --git a/.github/workflows/performance-instructions.yml b/.github/workflows/performance-instructions.yml new file mode 100644 index 000000000..e3eee1446 --- /dev/null +++ b/.github/workflows/performance-instructions.yml @@ -0,0 +1,107 @@ +name: Performance (instruction count) +permissions: + contents: read + +# Advisory companion of performance.yml. Instead of wall-clock time it counts the CPU instructions of each scenario's +# measured region with valgrind/callgrind, which does not depend on the load of the shared runner. The report goes to +# the job summary; nothing is posted to the pull request and the job never fails the pull request. +on: + pull_request: + types: + - opened + - reopened + - synchronize + +jobs: + instruction-count: + name: Count instructions + runs-on: ubuntu-latest + continue-on-error: true + timeout-minutes: 60 + steps: + # Counts depend on the exact Node.js build; base and head run in this job on the same one. + - name: Setup Node.js + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: '22' + + - name: Install valgrind + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends valgrind + valgrind --version + + - name: (base) Checkout main repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.base.sha }} + path: base + + - name: (head) Checkout main repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: head + + - name: Checkout hyperformula-tests repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ssh-key: ${{ secrets.DEPLOY_TOKEN }} + repository: handsontable/hyperformula-tests + path: head/hyperformula/test/hyperformula-tests + + - name: Fetch hyperformula-tests and sync branches + run: cd head/hyperformula/test && ./fetch-tests.sh + + # Both sides run the same (head) copy of the suite; only the engine under test differs. + - name: Share the tests between base and head + run: cp -r head/hyperformula/test/hyperformula-tests base/hyperformula/test/hyperformula-tests + + - name: Record the revisions under test + run: | + echo "base: $(git -C base rev-parse HEAD)" + echo "head: $(git -C head rev-parse HEAD)" + echo "tests: $(git -C head/hyperformula/test/hyperformula-tests rev-parse HEAD)" + node --version + + - name: (base) Install dependencies + run: cd base && npm ci + + - name: (head) Install dependencies + run: cd head && npm ci + + # A tests branch that predates the runner skips the counts with a notice instead of failing. + - name: Check for the instruction-count runner + id: runner + run: | + if [ -f head/hyperformula/test/hyperformula-tests/performance/run-instructions.ts ]; then + echo "present=true" >> "$GITHUB_OUTPUT" + else + echo "::notice::The paired hyperformula-tests branch predates the instruction-count runner; skipping." + fi + + # The runner comes with the shared tests and is started through the generic tsnode script of the engine's + # package.json, which every revision has. Each run compiles the engine of its own tree into the same build directory and measures it + # there, so the two runs must stay sequential: the count depends on the paths a process sees. + - name: (base) Count instructions + if: steps.runner.outputs.present == 'true' + run: cd base && npm run tsnode --workspace=hyperformula test/hyperformula-tests/performance/run-instructions.ts "$GITHUB_WORKSPACE/base-instructions.json" + + - name: (head) Count instructions + if: steps.runner.outputs.present == 'true' + run: cd head && npm run tsnode --workspace=hyperformula test/hyperformula-tests/performance/run-instructions.ts "$GITHUB_WORKSPACE/head-instructions.json" + + - name: Compare the counts + if: steps.runner.outputs.present == 'true' + run: | + cd head && npm run tsnode --workspace=hyperformula test/hyperformula-tests/performance/compare-instructions.ts "$GITHUB_WORKSPACE/base-instructions.json" "$GITHUB_WORKSPACE/head-instructions.json" "$GITHUB_WORKSPACE/instructions-report.md" + cat "$GITHUB_WORKSPACE/instructions-report.md" >> "$GITHUB_STEP_SUMMARY" + + - name: Print the raw counts + if: always() + run: | + for f in base-instructions head-instructions; do + echo "::group::$f.json" + cat "$GITHUB_WORKSPACE/$f.json" 2>/dev/null || echo "(missing)" + echo + echo "::endgroup::" + done diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index 32cde4b97..377525c0a 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -10,6 +10,11 @@ on: - reopened - synchronize # the head branch is updated from the base branch, new commits are pushed to the head branch, or the base branch is changed +# A newer push cancels the run of the same pull request, so an older run that finishes last cannot replace the newer table. +concurrency: + group: performance-${{ github.event.pull_request.number }} + cancel-in-progress: true + jobs: performance-test: strategy: @@ -18,59 +23,110 @@ jobs: os: [ 'ubuntu-latest' ] name: Test performance runs-on: ${{ matrix.os }} + timeout-minutes: 45 steps: - name: Setup Node.js ${{ matrix.node-version }} uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: ${{ matrix.node-version }} + # Base and head live side by side so their runs can alternate. Measuring one side + # in a single block and the other afterwards lets the drift of a shared runner show up as a "regression"; + # alternating spreads that drift over both sides. - name: (base) Checkout main repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ github.event.pull_request.base.sha }} + path: base + + - name: (head) Checkout main repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + path: head - name: Checkout hyperformula-tests repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ssh-key: ${{ secrets.DEPLOY_TOKEN }} repository: handsontable/hyperformula-tests - path: hyperformula/test/hyperformula-tests + path: head/hyperformula/test/hyperformula-tests - name: Fetch hyperformula-tests and sync branches - run: cd hyperformula/test && ./fetch-tests.sh + run: cd head/hyperformula/test && ./fetch-tests.sh - - name: (base) Install dependencies - run: npm ci + # Both sides run the same (head) copy of the suite; only the engine under test differs. + - name: Share the tests between base and head + run: cp -r head/hyperformula/test/hyperformula-tests base/hyperformula/test/hyperformula-tests - - name: (base) Run performance tests - run: npm run benchmark:write-to-file --workspace=hyperformula base.json + - name: Record the revisions under test + id: revisions + run: | + echo "base: $(git -C base rev-parse HEAD)" + echo "head: $(git -C head rev-parse HEAD)" + echo "tests: $(git -C head/hyperformula/test/hyperformula-tests rev-parse HEAD)" + echo "tests=$(git -C head/hyperformula/test/hyperformula-tests rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" - - name: (head) Checkout main repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - clean: false + - name: (base) Install dependencies + run: cd base && npm ci - name: (head) Install dependencies - run: npm ci + run: cd head && npm ci - - name: (head) Run performance tests + # Two rounds, each with half of the samples; the comparison merges the rounds of a side. The order is + # reversed in the second round (base, head, head, base) so that any effect of running first or second + # is shared by both sides. + - name: Run performance tests (alternating base and head) + env: + PERF_SAMPLE_SCALE: '0.5' run: | - npm run benchmark:write-to-file --workspace=hyperformula head.json + run() { (cd "$1" && npm run benchmark:write-to-file --workspace=hyperformula "$GITHUB_WORKSPACE/$1-$2.json"); } + run base 1; run head 1 + run head 2; run base 2 + # Exits non-zero when a results file is unreadable or nothing is comparable, so a broken comparison is a + # red check instead of a stale report. A regression is reported in the comment but does not fail this step. + # When the tests carry a noise history, each scenario is judged against the band of changes measured + # earlier on identical code. - name: Compare the results run: | - npm run benchmark:compare-benchmarks --workspace=hyperformula base.json head.json performance-report.md + HISTORY="$GITHUB_WORKSPACE/head/hyperformula/test/hyperformula-tests/performance/noise-history.json" + if [ -f "$HISTORY" ]; then export PERF_HISTORY_FILE="$HISTORY"; fi + cd head && npm run benchmark:compare-benchmarks --workspace=hyperformula "$GITHUB_WORKSPACE/base-1.json,$GITHUB_WORKSPACE/base-2.json" "$GITHUB_WORKSPACE/head-1.json,$GITHUB_WORKSPACE/head-2.json" "$GITHUB_WORKSPACE/performance-report.md" + # A tests branch older than the two-round comparison reads the comma-separated list as one file name, + # logs the error, exits 0 and writes no report. Fail here instead, so the failure comment below says so. + test -s "$GITHUB_WORKSPACE/performance-report.md" || { echo "No performance report: merge develop of hyperformula-tests into the paired tests branch." >&2; exit 1; } + + # The runner is gone after the job; the raw samples in the log are what a later analysis can use. + - name: Print the raw samples + if: always() + run: | + for f in base-1 head-1 head-2 base-2; do + echo "::group::$f.json" + cat "$GITHUB_WORKSPACE/$f.json" 2>/dev/null || echo "(missing)" + echo + echo "::endgroup::" + done - name: Publish a comment - header uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5 with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} message: | - ## Performance comparison of head (${{ github.event.pull_request.head.sha }}) vs base (${{ github.event.pull_request.base.sha }}) + ## Performance comparison of head (${{ github.event.pull_request.head.sha }}) vs base (${{ github.event.pull_request.base.sha }}) with the tests at ${{ steps.revisions.outputs.tests }} - name: Publish a comment - performance comparison report uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5 with: append: true GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - path: hyperformula/performance-report.md + path: performance-report.md + + # Replaces the previous push's table, so a failed, timed-out or cancelled run never leaves an older report + # standing for this head (a timeout counts as cancelled, not as a failure). + - name: Publish a comment - failure + if: failure() || cancelled() + uses: marocchino/sticky-pull-request-comment@5770ad5eb8f42dd2c4f34da00c94c5381e49af88 # v3.0.5 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + message: | + ## Performance comparison of head (${{ github.event.pull_request.head.sha }}) did not finish; see the job log. diff --git a/hyperformula/dev-docs/PERFORMANCE.md b/hyperformula/dev-docs/PERFORMANCE.md index 2405e45b8..dc5e94dac 100644 --- a/hyperformula/dev-docs/PERFORMANCE.md +++ b/hyperformula/dev-docs/PERFORMANCE.md @@ -8,7 +8,7 @@ HyperFormula is a calculation engine, so the performance of production code is a - **Memory counts.** No cache grows without eviction, and no reference is left behind after sheets, rows or columns are removed. The memory footprint of a new cache is known and acceptable. - **Bound the input.** Set a limit or a budget against huge ranges and very long strings. - **Mind the bundle.** No unnecessary imports, polyfills or data tables. -- Run `npm run test:performance` for changes that may affect the evaluation or CRUD hot paths, and check the result is within noise. +- Run `npm run test:performance` for changes that may affect the evaluation or CRUD hot paths, and check the result is within noise. On a pull request, the Performance workflow posts a comment with an advisory verdict for each scenario; `hyperformula/test/hyperformula-tests/performance/README.md` explains where each threshold comes from and how to read the verdicts. A second workflow, Performance (instruction count), counts the CPU instructions of the same scenarios with valgrind, which does not depend on the load of the runner, and writes the comparison to the job summary; it is a prototype and never fails the pull request. Hot paths worth knowing about before touching them: