|
| 1 | +name: Benchmarks |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [labeled, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + bench: |
| 13 | + name: Benchmark comparison |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: contains(github.event.pull_request.labels.*.name, 'bench') |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 |
| 22 | + with: |
| 23 | + toolchain: stable |
| 24 | + |
| 25 | + - uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2 |
| 26 | + |
| 27 | + - name: Build benchmark (PR) |
| 28 | + run: cargo build --release -p lightning-bench |
| 29 | + |
| 30 | + - name: Copy PR binary |
| 31 | + run: cp target/release/lightning-bench /tmp/lightning-bench-pr |
| 32 | + |
| 33 | + - name: Checkout main |
| 34 | + run: git checkout origin/main |
| 35 | + |
| 36 | + - name: Build benchmark (main) |
| 37 | + id: build-main |
| 38 | + continue-on-error: true |
| 39 | + run: cargo build --release -p lightning-bench |
| 40 | + |
| 41 | + - name: Copy main binary |
| 42 | + if: steps.build-main.outcome == 'success' |
| 43 | + run: cp target/release/lightning-bench /tmp/lightning-bench-main |
| 44 | + |
| 45 | + - name: Run main benchmark |
| 46 | + if: steps.build-main.outcome == 'success' |
| 47 | + run: /tmp/lightning-bench-main > /tmp/bench-main.json 2>/dev/null |
| 48 | + |
| 49 | + - name: Run PR benchmark |
| 50 | + run: /tmp/lightning-bench-pr > /tmp/bench-pr.json 2>/dev/null |
| 51 | + |
| 52 | + - name: Post comparison comment |
| 53 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const fs = require('fs'); |
| 57 | + const pr = JSON.parse(fs.readFileSync('/tmp/bench-pr.json', 'utf8')); |
| 58 | +
|
| 59 | + let hasMain = false; |
| 60 | + let main = {}; |
| 61 | + try { |
| 62 | + main = JSON.parse(fs.readFileSync('/tmp/bench-main.json', 'utf8')); |
| 63 | + hasMain = true; |
| 64 | + } catch {} |
| 65 | +
|
| 66 | + function fmt(v) { |
| 67 | + return typeof v === 'number' ? v.toFixed(2) : String(v); |
| 68 | + } |
| 69 | +
|
| 70 | + function pctChange(oldVal, newVal, lowerIsBetter) { |
| 71 | + if (!oldVal || oldVal === 0) return ''; |
| 72 | + const pct = ((newVal - oldVal) / oldVal) * 100; |
| 73 | + const sign = pct > 0 ? '+' : ''; |
| 74 | + const tag = lowerIsBetter |
| 75 | + ? (pct > 15 ? ' !!!' : pct < -15 ? ' +++' : '') |
| 76 | + : (pct < -15 ? ' !!!' : pct > 15 ? ' +++' : ''); |
| 77 | + return ` (${sign}${pct.toFixed(1)}%${tag})`; |
| 78 | + } |
| 79 | +
|
| 80 | + let body = '<!-- bench-results -->\n## Benchmark Results\n\n'; |
| 81 | +
|
| 82 | + body += '### Connection Setup (ms)\n\n'; |
| 83 | + body += '| Percentile | PR |' + (hasMain ? ' main | change |' : '') + '\n'; |
| 84 | + body += '|---|---|' + (hasMain ? '---|---|' : '') + '\n'; |
| 85 | + for (const p of ['p50', 'p95', 'p99']) { |
| 86 | + const prVal = pr.connection_setup_ms[p]; |
| 87 | + if (hasMain) { |
| 88 | + const mainVal = main.connection_setup_ms[p]; |
| 89 | + body += `| ${p} | ${fmt(prVal)} | ${fmt(mainVal)} | ${pctChange(mainVal, prVal, true)} |\n`; |
| 90 | + } else { |
| 91 | + body += `| ${p} | ${fmt(prVal)} |\n`; |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | + const sizes = Object.keys(pr.latency_ms).sort((a, b) => { |
| 96 | + const order = {'256B': 0, '1KB': 1, '10KB': 2, '100KB': 3, '1MB': 4}; |
| 97 | + return (order[a] ?? 99) - (order[b] ?? 99); |
| 98 | + }); |
| 99 | +
|
| 100 | + body += '\n### Latency (ms)\n\n'; |
| 101 | + body += '| Size | Percentile | PR |' + (hasMain ? ' main | change |' : '') + '\n'; |
| 102 | + body += '|---|---|---|' + (hasMain ? '---|---|' : '') + '\n'; |
| 103 | + for (const size of sizes) { |
| 104 | + for (const p of ['p50', 'p95', 'p99']) { |
| 105 | + const prVal = pr.latency_ms[size][p]; |
| 106 | + if (hasMain && main.latency_ms?.[size]) { |
| 107 | + const mainVal = main.latency_ms[size][p]; |
| 108 | + body += `| ${size} | ${p} | ${fmt(prVal)} | ${fmt(mainVal)} | ${pctChange(mainVal, prVal, true)} |\n`; |
| 109 | + } else { |
| 110 | + body += `| ${size} | ${p} | ${fmt(prVal)} |\n`; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +
|
| 115 | + body += '\n### Throughput (req/s)\n\n'; |
| 116 | + body += '| Size | PR |' + (hasMain ? ' main | change |' : '') + '\n'; |
| 117 | + body += '|---|---|' + (hasMain ? '---|---|' : '') + '\n'; |
| 118 | + for (const size of sizes) { |
| 119 | + const prVal = pr.throughput_rps[size]; |
| 120 | + if (hasMain && main.throughput_rps?.[size] != null) { |
| 121 | + const mainVal = main.throughput_rps[size]; |
| 122 | + body += `| ${size} | ${fmt(prVal)} | ${fmt(mainVal)} | ${pctChange(mainVal, prVal, false)} |\n`; |
| 123 | + } else { |
| 124 | + body += `| ${size} | ${fmt(prVal)} |\n`; |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + if (pr.wire_bytes) { |
| 129 | + body += '\n### Wire Bytes\n\n'; |
| 130 | + body += '| Size | PR |' + (hasMain ? ' main | change |' : '') + '\n'; |
| 131 | + body += '|---|---|' + (hasMain ? '---|---|' : '') + '\n'; |
| 132 | + for (const size of sizes) { |
| 133 | + const prVal = pr.wire_bytes[size]; |
| 134 | + if (hasMain && main.wire_bytes?.[size] != null) { |
| 135 | + const mainVal = main.wire_bytes[size]; |
| 136 | + body += `| ${size} | ${prVal} | ${mainVal} | ${pctChange(mainVal, prVal, true)} |\n`; |
| 137 | + } else { |
| 138 | + body += `| ${size} | ${prVal} |\n`; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +
|
| 143 | + const marker = '<!-- bench-results -->'; |
| 144 | + const { data: comments } = await github.rest.issues.listComments({ |
| 145 | + owner: context.repo.owner, |
| 146 | + repo: context.repo.repo, |
| 147 | + issue_number: context.issue.number, |
| 148 | + }); |
| 149 | + const existing = comments.find(c => c.body?.includes(marker)); |
| 150 | +
|
| 151 | + if (existing) { |
| 152 | + await github.rest.issues.updateComment({ |
| 153 | + owner: context.repo.owner, |
| 154 | + repo: context.repo.repo, |
| 155 | + comment_id: existing.id, |
| 156 | + body, |
| 157 | + }); |
| 158 | + } else { |
| 159 | + await github.rest.issues.createComment({ |
| 160 | + owner: context.repo.owner, |
| 161 | + repo: context.repo.repo, |
| 162 | + issue_number: context.issue.number, |
| 163 | + body, |
| 164 | + }); |
| 165 | + } |
0 commit comments