Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/performance-study.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- 'benchmark/study/**'
- 'benchmark/semantic/**'
- 'spec/benchmark/performance_study_spec.rb'
- 'spec/benchmark/rule_suite_spec.rb'
- '.github/workflows/performance-study.yml'

permissions:
Expand Down Expand Up @@ -87,6 +88,69 @@ jobs:
if-no-files-found: warn
retention-days: 14

rule-suite:
if: github.event_name == 'workflow_dispatch'
name: Paired rule suites — ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 25
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
path: candidate
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
# #100 is already merged; isolate #101 rather than including walker gains.
ref: e2d347e14e0f8d06489582bcb7c5473295a7df4b
path: baseline
persist-credentials: false
- uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # v1
with:
ruby-version: '4.0'
bundler-cache: true
working-directory: candidate
- name: Compare identical harnesses in alternating fresh processes
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$candidate = Join-Path $env:GITHUB_WORKSPACE 'candidate'
$baseline = Join-Path $env:GITHUB_WORKSPACE 'baseline'
$reports = Join-Path $env:GITHUB_WORKSPACE 'reports'
New-Item -ItemType Directory -Path $reports -Force | Out-Null
foreach ($file in 'fixture.rb','rule_policies.rb','rule_samples.rb','rule_suite.rb') {
Copy-Item -LiteralPath (Join-Path $candidate "benchmark/study/$file") -Destination (Join-Path $baseline "benchmark/study/$file")
}
$env:BUNDLE_PATH = Join-Path $candidate 'vendor/bundle'
$env:STUDY_PAIRS = '5000'
foreach ($workload in 'graph','one','identical','varied') {
foreach ($trial in 1..3) {
$order = if ($trial % 2) { @('baseline','candidate') } else { @('candidate','baseline') }
$results = @{}
foreach ($side in $order) {
$checkout = if ($side -eq 'baseline') { $baseline } else { $candidate }
$env:BUNDLE_GEMFILE = Join-Path $checkout 'Gemfile'
$env:STUDY_REVISION = git -C $checkout rev-parse HEAD
$output = Join-Path $reports "$workload-$trial-$side.json"
bundle exec ruby (Join-Path $checkout 'benchmark/study/rule_suite.rb') $workload $output
if ($LASTEXITCODE -ne 0) { throw "Failed $workload / $side" }
$results[$side] = Get-Content -LiteralPath $output -Raw | ConvertFrom-Json
}
if ($results.baseline.result.graph_sha256 -ne $results.candidate.result.graph_sha256) { throw 'Graph or evidence changed' }
}
}
- name: Retain raw rule-suite measurements
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: rule-suite-${{ matrix.os }}-${{ github.run_id }}
path: reports/*.json
if-no-files-found: error
retention-days: 14

discovery:
if: github.event_name == 'workflow_dispatch'
name: Paired discovery — ${{ matrix.os }}
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.1.1 (unreleased)

- Lazily reuse immutable node/internal/external-edge views within each cached graph analysis
across file and layer checks. Invalidation, per-rule filtering, callbacks and logging stay intact;
views trade additional retained memory for less repeated projection work.
- Reuse one source-entry status, compile exclusion matching once, and build relative identifiers
from directory children in a pruned iterative source walker, preserving sorted graph inputs.
- Fail closed on unreadable/disappearing included directory listings instead of inheriting Find's
Expand Down
80 changes: 80 additions & 0 deletions benchmark/study/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,86 @@ For the separate source-discovery optimization follow-up (#100), see the
JSON. The manual performance workflow also runs that unchanged generator/harness against the
v1.1.0 and candidate libraries on Linux/Windows, using only the normal production bundle.

## Repeated-rule follow-up (#101)

`rule_suite.rb` reuses this study's generated four-engine fixture, oracle, clock and memory helpers.
It needs only the root production bundle; it does not install or execute any target dependencies.
Run each workload in a fresh process, alternating baseline/candidate order across at least three
trials. Compare against `e2d347e14e0f8d06489582bcb7c5473295a7df4b` (merged #100), copying only
`fixture.rb`, `rule_policies.rb`, `rule_samples.rb` and `rule_suite.rb` into the baseline's study
directory so both runtime libraries receive the identical measurement harness.

```sh
STUDY_PAIRS=5000 bundle exec ruby benchmark/study/rule_suite.rb graph pkg/rules-graph.json
STUDY_PAIRS=5000 bundle exec ruby benchmark/study/rule_suite.rb one pkg/rules-one.json
STUDY_PAIRS=5000 bundle exec ruby benchmark/study/rule_suite.rb identical pkg/rules-identical.json
STUDY_PAIRS=5000 bundle exec ruby benchmark/study/rule_suite.rb varied pkg/rules-varied.json
```

Workloads use 10,000 sources and 5,000 independently known API-to-database dependencies.
`one` checks one nonempty passing boundary; `identical` repeats it twenty times; `varied` runs
seven file, seven layer and six different slice policies. All must pass, while separate forbidden
dependency and empty-selector controls must fail with the expected data/counts. The ordered graph
and evidence digest must match across checkouts. A fast incorrect check aborts the run.

- `cold_graph`: actual graph extraction only, after explicit clearing.
- `cold_plus_suite`: a separate cold graph plus the entire policy suite. Cache clearing and GC
occur before, outside the timer. This does not include Ruby startup, fixture generation or cold disk.
- `warm_rules`: individual public checks after that complete suite; include normal graph lookup,
rule-specific selection and assertion. Their sum is warm suite time, not CLI/process startup.
- Every timed stage records allocated objects. Post-GC Ruby heap is recorded before analysis, after
graph extraction, and after the cold suite. It excludes native memory; Linux peak RSS is a
process-lifetime high-water mark (including both cold stages), not an individual stage's peak.
Windows unavailable RSS is null. Controls run after retained-memory measurements.

Only three built-in graph-invariant views are lazy/shared per existing cached analysis entry:
nodes, internal edges and external edges. Storage grows with that analysis's graph, not the number
of rules. This adds retained O(V+E) memory when requested; raw graph consumers do not eagerly build
indexes. There is no new graph-keyed global query cache. The existing graph snapshot cache still
retains entries until explicit clearing; it is not a content-watching or LRU cache. Per-entry/global
clearing drops the associated views. Rules already running may finish on their original snapshot.

Selectors, callbacks, FileInfo reads, cycle enumeration, logging and arbitrary custom projections
still run per check. Slice labeler closures may capture mutable state, even when the Proc is frozen;
their outputs are intentionally not memoized. Thus varied-suite measurements include unchanged
slice work and do not imply that all policy types benefit equally.

The manual workflow runs three alternating paired trials for all four workloads on Linux/Windows
with pinned actions, read-only permissions, a 25-minute timeout and raw JSON artifacts. Ordinary PR
tests run a four-pair correctness version without speed gates. Timings are observations, not promises
or grounds for changing the existing correctness/coverage thresholds.

### Recorded results

[Run 45446554405](https://github.com/dragonsec-ai/ArchUnitRuby/actions/runs/45446554405)
passed all five jobs. [Raw samples and medians](rule-views-ci.json) preserve all 48 paired samples,
exact revisions, Ruby 4.0.10 platforms, controls and ordered graph/evidence digests. Candidate
`7d73b3e` contains the runtime/harness change; later evidence/documentation commits do not change it.
Three-trial medians at 10,000 sources (seconds; allocations are summed over the warm suite):

| Host / suite | Cold graph + suite, old → new | Warm suite, old → new | Warm allocations, old → new |
|---|---:|---:|---:|
| Linux / one | 1.983 → 2.010 | 0.0767 → 0.0255 | 255,209 → 70,193 |
| Linux / 20 identical | 3.637 → 2.552 | 1.765 → 0.530 | 5,104,180 → 1,403,860 |
| Linux / 20 varied | 3.669 → 2.995 | 1.815 → 0.939 | 5,541,790 → 3,236,581 |
| Windows / one | 5.183 → 5.136 | 0.0867 → 0.0317 | 255,235 → 70,219 |
| Windows / 20 identical | 7.041 → 5.790 | 1.915 → 0.643 | 5,104,700 → 1,404,380 |
| Windows / 20 varied | 7.205 → 6.124 | 1.889 → 1.084 | 5,542,310 → 3,237,101 |

Warm varied suites improve ~48% on Linux / ~43% on Windows, with ~42% fewer allocations; twenty
identical checks improve ~70% / ~66%, with ~72% fewer allocations. Cold varied suites improve
~18% / ~15%. These are three shared-runner trials, not universal promises or competitor rankings.
Cold single-rule timing is ~1.3% higher on Linux and ~0.9% lower on Windows; its allocations are
essentially unchanged. Raw graph-only cold timings also remain similar, with only five extra
allocations in the Linux second cold stage and one extra in Windows.

The cost is retained memory: indexed suites grow post-GC Ruby heap by ~9.83 MiB versus ~6.66 MiB
before (~3.17 MiB extra). Graph-only retained growth differs by only a few hundred bytes. Peak RSS
is not uniformly lower: Linux identical-suite medians rise 139,448 → 154,672 KiB, while varied-suite
medians are 150,124 → 148,852 KiB. Even graph-only RSS varies 118,360 → 128,600 KiB despite nearly
identical retained Ruby heap; do not attribute every high-water change to the indexes. Windows RSS
remains unavailable/null. There is no memory-reduction claim.

## Shared-policy study

`run.rb` generates Rails-like and four-engine layouts with 200 Ruby files by default. Density
Expand Down
4 changes: 2 additions & 2 deletions benchmark/study/fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Fixture
attr_reader :root, :pairs, :density, :shape

def initialize(root:, pairs:, density:, shape:)
unless pairs.is_a?(Integer) && pairs.between?(4, 1000) && [1, 3].include?(density)
raise ArgumentError, 'study requires 4..1000 pairs and density 1 or 3'
unless pairs.is_a?(Integer) && pairs.between?(4, 5000) && [1, 3].include?(density)
raise ArgumentError, 'study requires 4..5000 pairs and density 1 or 3'
end
raise ArgumentError, 'unknown study shape' unless %w[rails engines].include?(shape)

Expand Down
Loading