flameiq.core — Comparison Engine
Comparator
FlameIQ deterministic comparison engine.
This is the most critical module in the codebase. It compares a current
PerformanceSnapshot against a baseline
and produces a ComparisonResult.
Determinism guarantee
Given identical inputs this module always produces identical outputs.
No randomness of any kind.
No
datetime.now()calls.No network I/O.
Floating-point arithmetic is explicit and documented.
All rounding uses Python’s built-in
round()with fixed precision.
Floating-point policy
change_percent is computed as:
((current - baseline) / baseline) * 100
rounded to 4 decimal places for stable threshold comparisons.
Division by zero is guarded — if baseline == 0 the metric is skipped
with a warning and a ComparisonError
is raised internally (caught and logged).
- flameiq.core.comparator.compute_change_percent(baseline, current)[source]
Compute the signed percentage change from baseline to current.
Formula:
((current - baseline) / baseline) * 100
Rounded to
_CHANGE_PERCENT_PRECISIONdecimal places.- Parameters:
- Returns:
Signed percentage change, rounded to 4 d.p. Positive means current is larger than baseline.
- Raises:
ComparisonError – If
baselineis exactly zero.- Return type:
Examples:
compute_change_percent(100.0, 110.0) # → 10.0 compute_change_percent(100.0, 90.0) # → -10.0 compute_change_percent(100.0, 100.0) # → 0.0
- flameiq.core.comparator.compare_snapshots(baseline, current, threshold_config=None, warning_margin_percent=5.0)[source]
Compare current against baseline and return a full diff.
For every metric present in the baseline, the engine:
Computes
change_percentviacompute_change_percent().Looks up the configured threshold (or applies the default).
Calls
evaluate_threshold()to determine pass / warning / regression.
Metrics present in current but absent from baseline are ignored — they have no reference value and cannot regress.
- Parameters:
baseline (PerformanceSnapshot) – The reference snapshot.
current (PerformanceSnapshot) – The snapshot under evaluation.
threshold_config (dict[str, str | float] | None) – Raw threshold dict from
flameiq.yaml, e.g.{"latency.p95": "10%"}. Falls back to defaults ifNone.warning_margin_percent (float) – Distance from threshold that triggers a WARNING instead of PASS.
- Returns:
A
ComparisonResultwith complete per-metric diffs and an overallRegressionStatus.- Return type:
Models
FlameIQ core domain models.
These dataclasses represent the results of FlameIQ operations.
They are distinct from schema models (flameiq.schema.v1.models),
which represent input data.
No external dependencies.
- class flameiq.core.models.RegressionStatus(*values)[source]
-
The overall outcome of a baseline-vs-current comparison.
- PASS = 'pass'
All metrics are within their configured thresholds.
- REGRESSION = 'regression'
One or more metrics exceeded their threshold.
- WARNING = 'warning'
No threshold breached, but metrics are approaching limits.
- INSUFFICIENT_DATA = 'insufficient_data'
Statistical mode requested but sample count too low.
- class flameiq.core.models.MetricDiff(metric_key, baseline_value, current_value, change_percent, threshold_percent, is_regression, is_warning=False, p_value=None, effect_size=None)[source]
Bases:
objectThe computed difference for a single metric key.
- Parameters:
- __init__(metric_key, baseline_value, current_value, change_percent, threshold_percent, is_regression, is_warning=False, p_value=None, effect_size=None)
- class flameiq.core.models.ComparisonResult(status, diffs=<factory>, baseline_commit=None, current_commit=None, statistical_mode=False, summary=None)[source]
Bases:
objectThe complete result of a baseline-vs-current comparison.
- Parameters:
status (RegressionStatus)
diffs (list[MetricDiff])
baseline_commit (str | None)
current_commit (str | None)
statistical_mode (bool)
summary (str | None)
- __init__(status, diffs=<factory>, baseline_commit=None, current_commit=None, statistical_mode=False, summary=None)
- Parameters:
status (RegressionStatus)
diffs (list[MetricDiff])
baseline_commit (str | None)
current_commit (str | None)
statistical_mode (bool)
summary (str | None)
- Return type:
None
- status: RegressionStatus
Overall pass/regression/warning outcome.
- diffs: list[MetricDiff]
Per-metric differences, in metric-key order.
- property regressions: list[MetricDiff]
Metrics that breached their threshold.
- property warnings: list[MetricDiff]
Metrics within threshold but approaching the limit.
- property passed: list[MetricDiff]
Metrics that passed cleanly, with no warning.
Thresholds
FlameIQ threshold configuration and evaluation.
Thresholds are specified in flameiq.yaml as percent strings:
thresholds:
latency.p95: 10% # Allow up to 10% latency increase
throughput: -5% # Allow up to 5% throughput decrease
memory_mb: 8% # Allow up to 8% memory increase
Sign convention:
Positive threshold (e.g.
10%) → allow up to +10% increase.Negative threshold (e.g.
-5%) → allow up to 5% decrease.For known metrics the direction is inferred automatically (see
evaluate_threshold()).
Default threshold: 10% in either direction for unknown metrics.
- flameiq.core.thresholds.DEFAULT_THRESHOLD_PERCENT: float = 10.0
Default allowance applied when no explicit threshold is configured.
- flameiq.core.thresholds.parse_threshold(key, raw)[source]
Parse a raw threshold value into a signed float.
- Parameters:
- Returns:
Float percentage, e.g.
10.0or-5.0.- Raises:
ThresholdConfigError – If the string is not a valid percent.
- Return type:
Examples:
parse_threshold("latency.p95", "10%") # → 10.0 parse_threshold("throughput", "-5%") # → -5.0 parse_threshold("memory_mb", 10.0) # → 10.0
- flameiq.core.thresholds.evaluate_threshold(metric_key, change_percent, threshold_percent)[source]
Determine whether a
change_percentbreaches its threshold.Direction semantics:
Higher-is-worse metrics (
latency.*,memory_mb,cpu_percent): a regression ischange_percent > +threshold.Lower-is-worse metrics (
throughput): a regression ischange_percent < -abs(threshold).Unknown / custom metrics: any absolute deviation beyond the threshold is flagged as a regression.
Errors
FlameIQ exception hierarchy.
All FlameIQ exceptions derive from FlameIQError. This lets callers
catch the entire FlameIQ surface with a single except FlameIQError, or
target specific classes for fine-grained handling.
Rule: Never raise a bare Exception anywhere in the FlameIQ codebase.
Always raise from this hierarchy.
Hierarchy:
FlameIQError
├── ValidationError
│ └── SchemaVersionError
├── ConfigurationError
│ └── ThresholdConfigError
├── BaselineError
│ ├── BaselineNotFoundError
│ └── BaselineCorruptedError
├── ProviderError
│ ├── ProviderNotFoundError
│ └── MetricsFileNotFoundError
├── ComparisonError
│ └── InsufficientSamplesError
└── StorageError
└── MigrationError
- exception flameiq.core.errors.FlameIQError[source]
Bases:
ExceptionBase class for all FlameIQ exceptions.
- exception flameiq.core.errors.ValidationError[source]
Bases:
FlameIQErrorRaised when a snapshot fails schema validation.
- exception flameiq.core.errors.SchemaVersionError(version)[source]
Bases:
ValidationErrorRaised when an unsupported schema version is encountered.
- Parameters:
version (int) – The unsupported version number that was encountered.
- Return type:
None
Initialize the error with the unsupported version number.
- exception flameiq.core.errors.ConfigurationError[source]
Bases:
FlameIQErrorRaised when flameiq.yaml is missing, malformed, or invalid.
- exception flameiq.core.errors.ThresholdConfigError(key, value, reason)[source]
Bases:
ConfigurationErrorRaised when a threshold value cannot be parsed or is out of range.
- Parameters:
- Return type:
None
Initialize the error with the invalid threshold details.
- exception flameiq.core.errors.BaselineError[source]
Bases:
FlameIQErrorRaised for baseline management failures.
- exception flameiq.core.errors.BaselineNotFoundError(path)[source]
Bases:
BaselineErrorRaised when no baseline snapshot exists for the current context.
- Parameters:
path (str) – The filesystem path where the baseline was expected.
- Return type:
None
Initialize the error with the expected baseline path.
- exception flameiq.core.errors.BaselineCorruptedError(path, reason)[source]
Bases:
BaselineErrorRaised when a baseline file exists but cannot be deserialised.
- Parameters:
- Return type:
None
Initialize the error with the corrupted file details.
- exception flameiq.core.errors.ProviderError[source]
Bases:
FlameIQErrorRaised when a metric provider fails to collect or normalise data.
- exception flameiq.core.errors.ProviderNotFoundError(name)[source]
Bases:
ProviderErrorRaised when a requested provider name is not registered.
- Parameters:
name (str) – The requested provider name.
- Return type:
None
Initialize the error with the missing provider name.
- exception flameiq.core.errors.MetricsFileNotFoundError(path)[source]
Bases:
ProviderErrorRaised when the metrics source file does not exist.
- Parameters:
path (str) – The path that was not found.
- Return type:
None
Initialize the error with the missing file path.
- exception flameiq.core.errors.ComparisonError[source]
Bases:
FlameIQErrorRaised when a comparison cannot be completed.
- exception flameiq.core.errors.InsufficientSamplesError(metric, got, required)[source]
Bases:
ComparisonErrorRaised when statistical mode is enabled but sample count is too low.
- Parameters:
- Return type:
None
Initialize the error with the insufficient samples details.
- exception flameiq.core.errors.StorageError[source]
Bases:
FlameIQErrorRaised for storage read/write failures.
- exception flameiq.core.errors.MigrationError[source]
Bases:
StorageErrorRaised when a storage schema migration fails.