|
| 1 | +# ============================================================================== |
| 2 | +# Code Analyzers Performance Comparison |
| 3 | +# |
| 4 | +# Triggered on pull requests that modify Saritasa.Tools.CodeAnalyzers project. |
| 5 | +# |
| 6 | +# How it works: |
| 7 | +# 1. Checks out the PR branch and runs benchmarks against an external test project. |
| 8 | +# 2. Switches only the Saritasa.Tools.CodeAnalyzers source to the base branch (master), |
| 9 | +# keeping the benchmark code and test project unchanged. |
| 10 | +# 3. Runs benchmarks again to get the baseline results. |
| 11 | +# 4. Compares the two runs and fails if mean time or allocations regress |
| 12 | +# beyond the configured thresholds (THRESHOLD_MEAN / THRESHOLD_ALLOCATION). |
| 13 | +# 5. Posts the comparison report as a sticky PR comment. |
| 14 | +# |
| 15 | +# More info about tool used to analyze and compare at https://github.com/TechNobre/PowerUtils.BenchmarkDotnet.Reporter. |
| 16 | +# ============================================================================== |
| 17 | +name: Code Analyzers Performance Comparison |
| 18 | + |
| 19 | +on: |
| 20 | + pull_request: |
| 21 | + branches: |
| 22 | + - master |
| 23 | + |
| 24 | + paths: |
| 25 | + - 'src/Saritasa.Tools.CodeAnalyzers/**' |
| 26 | + |
| 27 | + # Allows you to run this workflow manually from the Actions tab |
| 28 | + workflow_dispatch: |
| 29 | + |
| 30 | +env: |
| 31 | + PATH_BENCHMARKS_PROJECT: 'test/Saritasa.Tools.CodeAnalyzers.Benchmarks/Saritasa.Tools.CodeAnalyzers.Benchmarks.csproj' |
| 32 | + DIR_BASELINE_REPORTS: './artifacts-baseline' |
| 33 | + DIR_TARGET_REPORTS: './artifacts-target' |
| 34 | + PATH_REPORT_RESULT: './BenchmarkReporter/benchmark-comparison-report.md' |
| 35 | + TEST_PROJECT_FOLDER: './testProject' |
| 36 | + PATH_TO_TEST_SOLUTION: 'src/Saritasa.NetForge.slnx' |
| 37 | + THRESHOLD_MEAN: 50% |
| 38 | + THRESHOLD_ALLOCATION: 50% |
| 39 | + |
| 40 | +jobs: |
| 41 | + compare-code-analyzers-performance: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + |
| 44 | + steps: |
| 45 | + # Ensure a clean test project directory before checkout. |
| 46 | + - name: Clean up target directory |
| 47 | + run: rm -rf ${{ env.TEST_PROJECT_FOLDER }} |
| 48 | + |
| 49 | + # Checkout the PR branch so we can benchmark the proposed changes. |
| 50 | + - name: Checkout PR Branch (Current Changes) |
| 51 | + uses: actions/checkout@v4 |
| 52 | + |
| 53 | + # Checkout the external test project used as input for the analyzers. |
| 54 | + - name: Checkout External Repository |
| 55 | + id: external_checkout |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + repository: 'saritasa-nest/saritasa-forge-admin' |
| 59 | + path: ${{ env.TEST_PROJECT_FOLDER }} |
| 60 | + persist-credentials: false |
| 61 | + ssh-strict: false |
| 62 | + set-safe-directory: false |
| 63 | + fetch-depth: 1 |
| 64 | + |
| 65 | + # Pin the test project to a known commit for reproducible benchmark input. |
| 66 | + - name: Reset to specific commit |
| 67 | + uses: actions/checkout@v4 |
| 68 | + with: |
| 69 | + repository: 'saritasa-nest/saritasa-forge-admin' |
| 70 | + ref: dc2d1ed5d58be20e709de5ba884beeaba0c6a733 |
| 71 | + path: ${{ env.TEST_PROJECT_FOLDER }} |
| 72 | + |
| 73 | + - name: Setup .NET |
| 74 | + uses: actions/setup-dotnet@v4 |
| 75 | + with: |
| 76 | + dotnet-version: '10.0.x' |
| 77 | + |
| 78 | + # Benchmark the PR branch. sudo -E preserves the environment so this allows |
| 79 | + # BenchmarkDotNet to raise the process priority for more stable measurements. |
| 80 | + - name: Run PR Benchmarks |
| 81 | + run: | |
| 82 | + sudo -E dotnet run --no-launch-profile -c Release \ |
| 83 | + --project ${{ env.PATH_BENCHMARKS_PROJECT }} -- \ |
| 84 | + --artifacts ${{ env.DIR_TARGET_REPORTS }} \ |
| 85 | + --testProjectPath ${{ env.TEST_PROJECT_FOLDER }}/${{ env.PATH_TO_TEST_SOLUTION }} |
| 86 | +
|
| 87 | + # Swap the Saritasa.Tools.CodeAnalyzers code to the base branch while keeping the test project intact. |
| 88 | + # The directory is removed first to ensure files added in the PR (not present in base) are deleted. |
| 89 | + # Without this, `git checkout FETCH_HEAD -- <path>` only updates existing files and leaves new ones behind. |
| 90 | + - name: Switch Saritasa.Tools.CodeAnalyzers to Base Branch |
| 91 | + run: | |
| 92 | + git fetch origin ${{ github.base_ref }} --depth=1 |
| 93 | + sudo rm -rf src/Saritasa.Tools.CodeAnalyzers/ |
| 94 | + git checkout FETCH_HEAD -- src/Saritasa.Tools.CodeAnalyzers/ |
| 95 | + git status |
| 96 | +
|
| 97 | + # Benchmark the base branch with the same test project for a fair comparison. |
| 98 | + - name: Run Base Benchmarks |
| 99 | + run: | |
| 100 | + sudo -E dotnet run --no-launch-profile -c Release \ |
| 101 | + --project ${{ env.PATH_BENCHMARKS_PROJECT }} -- \ |
| 102 | + --artifacts ${{ env.DIR_BASELINE_REPORTS }} \ |
| 103 | + --testProjectPath ${{ env.TEST_PROJECT_FOLDER }}/${{ env.PATH_TO_TEST_SOLUTION }} |
| 104 | +
|
| 105 | + - name: Install lib for benchmarks comparison |
| 106 | + run: sudo -E dotnet tool install --global PowerUtils.BenchmarkDotnet.Reporter |
| 107 | + |
| 108 | + - name: Run benchmarks compare |
| 109 | + run: | |
| 110 | + pbreporter compare \ |
| 111 | + -b ${{ env.DIR_BASELINE_REPORTS }}/results \ |
| 112 | + -t ${{ env.DIR_TARGET_REPORTS }}/results \ |
| 113 | + -f markdown \ |
| 114 | + -tm ${{ env.THRESHOLD_MEAN }} \ |
| 115 | + -ta ${{ env.THRESHOLD_ALLOCATION }} \ |
| 116 | + -ft -fw |
| 117 | +
|
| 118 | + - name: Publish benchmark report in Summary |
| 119 | + run: cat ${{ env.PATH_REPORT_RESULT }} > $GITHUB_STEP_SUMMARY |
| 120 | + |
| 121 | + - name: Add compare benchmark report in PR Comment |
| 122 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 123 | + if: github.event_name == 'pull_request' |
| 124 | + with: |
| 125 | + header: compare-benchmark-report |
| 126 | + hide_and_recreate: true |
| 127 | + hide_classify: "OUTDATED" |
| 128 | + path: ${{ env.PATH_REPORT_RESULT }} |
0 commit comments