discourse/spec/support/perf_capture.rb
Alan Guo Xiang Tan 71a6dd9674
DEV: Add per-test SQL/Redis/network capture RSpec formatter (#40632)
Adds `RspecPerformanceFormatter`, an RSpec formatter that captures the
SQL queries, Redis commands, and outbound network calls each example
performs and emits them as NDJSON with one object per test. It gives an
AI model (or a developer) concrete per-test context for spotting N+1s,
redundant queries, or unexpected outbound calls. Off unless the
formatter is selected, so normal runs and production are unaffected.
Works with both `bin/rspec` and `bin/turbo_rspec`.
2026-06-16 09:47:54 +08:00

19 lines
589 B
Ruby
Vendored

# frozen_string_literal: true
if ENV["DISCOURSE_RSPEC_PERFORMANCE_FORMATTER"] == "1"
require_relative "rspec_performance_formatter"
RSpec.configure do |config|
config.before(:suite) { MethodProfiler.ensure_discourse_instrumentation! }
config.around(:each) do |example|
test_summary = nil
request_groups =
RspecPerformanceFormatter.collect_requests do
test_summary = RspecPerformanceFormatter.measure { example.run }
end
example.metadata[:perf] = RspecPerformanceFormatter.assemble(test_summary, request_groups)
end
end
end