Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /tests /test_benchmarking.py
| """Tests for Industry Resilience Benchmarking.""" | |
| from __future__ import annotations | |
| import pytest | |
| from faultray.model.components import ( | |
| AutoScalingConfig, | |
| CircuitBreakerConfig, | |
| Component, | |
| ComponentType, | |
| Dependency, | |
| FailoverConfig, | |
| ) | |
| from faultray.model.graph import InfraGraph | |
| from faultray.simulator.benchmarking import ( | |
| BenchmarkEngine, | |
| BenchmarkResult, | |
| INDUSTRY_PROFILES, | |
| IndustryProfile, | |
| _build_comparison_chart, | |
| _compute_sub_metrics, | |
| _estimate_percentile, | |
| _rank_description, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Helpers | |
| # --------------------------------------------------------------------------- | |
| def _make_graph( | |
| num_components: int = 5, | |
| replicas: int = 2, | |
| failover: bool = True, | |
| autoscaling: bool = True, | |
| circuit_breakers: bool = True, | |
| ) -> InfraGraph: | |
| """Create a test InfraGraph with configurable resilience properties.""" | |
| graph = InfraGraph() | |
| types = [ | |
| ComponentType.LOAD_BALANCER, | |
| ComponentType.WEB_SERVER, | |
| ComponentType.APP_SERVER, | |
| ComponentType.DATABASE, | |
| ComponentType.CACHE, | |
| ] | |
| for i in range(num_components): | |
| comp = Component( | |
| id=f"comp_{i}", | |
| name=f"Component {i}", | |
| type=types[i % len(types)], | |
| port=8080 + i, | |
| replicas=replicas, | |
| failover=FailoverConfig(enabled=failover), | |
| autoscaling=AutoScalingConfig(enabled=autoscaling), | |
| ) | |
| graph.add_component(comp) | |
| # Add dependency chain | |
| for i in range(num_components - 1): | |
| cb = CircuitBreakerConfig(enabled=circuit_breakers) | |
| graph.add_dependency(Dependency( | |
| source_id=f"comp_{i}", | |
| target_id=f"comp_{i + 1}", | |
| circuit_breaker=cb, | |
| )) | |
| return graph | |
| def _make_weak_graph() -> InfraGraph: | |
| """Create a graph with poor resilience characteristics.""" | |
| graph = InfraGraph() | |
| for i in range(3): | |
| comp = Component( | |
| id=f"weak_{i}", | |
| name=f"Weak {i}", | |
| type=ComponentType.APP_SERVER, | |
| replicas=1, | |
| failover=FailoverConfig(enabled=False), | |
| autoscaling=AutoScalingConfig(enabled=False), | |
| ) | |
| graph.add_component(comp) | |
| graph.add_dependency(Dependency( | |
| source_id="weak_0", | |
| target_id="weak_1", | |
| circuit_breaker=CircuitBreakerConfig(enabled=False), | |
| )) | |
| graph.add_dependency(Dependency( | |
| source_id="weak_1", | |
| target_id="weak_2", | |
| circuit_breaker=CircuitBreakerConfig(enabled=False), | |
| )) | |
| return graph | |
| # --------------------------------------------------------------------------- | |
| # 1. IndustryProfile data | |
| # --------------------------------------------------------------------------- | |
| class TestIndustryProfiles: | |
| def test_all_industries_defined(self): | |
| expected = { | |
| "fintech", "ecommerce", "healthcare", "saas", "gaming", | |
| "media_streaming", "government", "telecommunications", | |
| "insurance", "logistics", | |
| } | |
| assert set(INDUSTRY_PROFILES.keys()) == expected | |
| def test_profile_fields(self): | |
| for name, profile in INDUSTRY_PROFILES.items(): | |
| assert profile.industry == name | |
| assert profile.display_name != "" | |
| assert 0 <= profile.avg_resilience_score <= 100 | |
| assert 0 <= profile.median_resilience_score <= 100 | |
| assert profile.p25_score <= profile.median_resilience_score | |
| assert profile.median_resilience_score <= profile.p75_score | |
| assert profile.p75_score <= profile.p90_score | |
| assert profile.sample_size > 0 | |
| assert len(profile.common_weaknesses) > 0 | |
| assert len(profile.regulatory_requirements) > 0 | |
| assert len(profile.typical_stack) > 0 | |
| def test_avg_scores_in_range(self): | |
| for profile in INDUSTRY_PROFILES.values(): | |
| assert profile.avg_resilience_score >= 50.0 | |
| assert profile.avg_resilience_score <= 100.0 | |
| # --------------------------------------------------------------------------- | |
| # 2. Sub-metric computation | |
| # --------------------------------------------------------------------------- | |
| class TestSubMetrics: | |
| def test_strong_graph(self): | |
| graph = _make_graph(replicas=3, failover=True, circuit_breakers=True) | |
| metrics = _compute_sub_metrics(graph) | |
| assert metrics["redundancy"] == 100.0 | |
| assert metrics["isolation"] == 100.0 | |
| assert metrics["recovery"] == 100.0 | |
| assert metrics["diversity"] > 0 | |
| def test_weak_graph(self): | |
| graph = _make_weak_graph() | |
| metrics = _compute_sub_metrics(graph) | |
| assert metrics["redundancy"] == 0.0 | |
| assert metrics["isolation"] == 0.0 | |
| assert metrics["recovery"] == 0.0 | |
| def test_empty_graph(self): | |
| graph = InfraGraph() | |
| metrics = _compute_sub_metrics(graph) | |
| assert metrics["redundancy"] == 0.0 | |
| assert metrics["isolation"] == 0.0 | |
| assert metrics["recovery"] == 0.0 | |
| assert metrics["diversity"] == 0.0 | |
| def test_mixed_graph(self): | |
| graph = InfraGraph() | |
| # One strong component, one weak | |
| comp_strong = Component( | |
| id="strong", name="Strong", type=ComponentType.DATABASE, | |
| replicas=3, | |
| failover=FailoverConfig(enabled=True), | |
| autoscaling=AutoScalingConfig(enabled=True), | |
| ) | |
| comp_weak = Component( | |
| id="weak", name="Weak", type=ComponentType.APP_SERVER, | |
| replicas=1, | |
| failover=FailoverConfig(enabled=False), | |
| autoscaling=AutoScalingConfig(enabled=False), | |
| ) | |
| graph.add_component(comp_strong) | |
| graph.add_component(comp_weak) | |
| metrics = _compute_sub_metrics(graph) | |
| assert metrics["redundancy"] == 50.0 | |
| assert metrics["recovery"] == 50.0 | |
| # --------------------------------------------------------------------------- | |
| # 3. Percentile estimation | |
| # --------------------------------------------------------------------------- | |
| class TestPercentileEstimation: | |
| def test_at_median(self): | |
| profile = INDUSTRY_PROFILES["fintech"] | |
| pct = _estimate_percentile(profile.median_resilience_score, profile) | |
| assert abs(pct - 50.0) < 5.0 | |
| def test_above_p90(self): | |
| profile = INDUSTRY_PROFILES["fintech"] | |
| pct = _estimate_percentile(profile.p90_score + 3, profile) | |
| assert pct > 90.0 | |
| def test_below_p25(self): | |
| profile = INDUSTRY_PROFILES["fintech"] | |
| pct = _estimate_percentile(profile.p25_score - 10, profile) | |
| assert pct < 25.0 | |
| def test_very_low_score(self): | |
| profile = INDUSTRY_PROFILES["fintech"] | |
| pct = _estimate_percentile(0.0, profile) | |
| assert pct <= 5.0 | |
| def test_very_high_score(self): | |
| profile = INDUSTRY_PROFILES["fintech"] | |
| pct = _estimate_percentile(100.0, profile) | |
| assert pct >= 95.0 | |
| # --------------------------------------------------------------------------- | |
| # 4. Rank description | |
| # --------------------------------------------------------------------------- | |
| class TestRankDescription: | |
| def test_top_10(self): | |
| assert _rank_description(95.0) == "Top 10%" | |
| def test_top_quartile(self): | |
| assert _rank_description(80.0) == "Top Quartile" | |
| def test_above_average(self): | |
| assert _rank_description(55.0) == "Above Average" | |
| def test_below_average(self): | |
| assert _rank_description(35.0) == "Below Average" | |
| def test_bottom_quartile(self): | |
| assert _rank_description(15.0) == "Bottom Quartile" | |
| # --------------------------------------------------------------------------- | |
| # 5. BenchmarkEngine.benchmark | |
| # --------------------------------------------------------------------------- | |
| class TestBenchmark: | |
| def test_benchmark_strong_graph(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph(replicas=3, failover=True, circuit_breakers=True) | |
| result = engine.benchmark(graph, "fintech") | |
| assert isinstance(result, BenchmarkResult) | |
| assert result.your_score > 0 | |
| assert result.industry == "fintech" | |
| assert 0 <= result.percentile <= 100 | |
| assert result.rank_description != "" | |
| assert "resilience" in result.comparison | |
| assert result.peer_comparison_chart != "" | |
| def test_benchmark_weak_graph(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_weak_graph() | |
| result = engine.benchmark(graph, "government") | |
| # The weak graph has poor sub-metrics (redundancy, isolation, recovery) | |
| # even if the overall resilience_score may be high due to simple topology | |
| assert len(result.weaknesses) > 0 | |
| # Sub-metrics should be below industry average | |
| assert result.comparison["redundancy"][0] < result.comparison["redundancy"][1] | |
| assert result.comparison["isolation"][0] < result.comparison["isolation"][1] | |
| def test_benchmark_unknown_industry(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| with pytest.raises(ValueError, match="Unknown industry"): | |
| engine.benchmark(graph, "nonexistent_industry") | |
| def test_benchmark_strengths_and_weaknesses(self): | |
| engine = BenchmarkEngine() | |
| # Strong graph against government (lower avg score) | |
| graph = _make_graph(replicas=3, failover=True, circuit_breakers=True) | |
| result = engine.benchmark(graph, "government") | |
| # Should have strengths when score is higher than government avg | |
| assert len(result.strengths) > 0 or len(result.weaknesses) > 0 | |
| def test_comparison_dict(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| result = engine.benchmark(graph, "saas") | |
| for key, (yours, theirs) in result.comparison.items(): | |
| assert isinstance(yours, float) | |
| assert isinstance(theirs, float) | |
| # --------------------------------------------------------------------------- | |
| # 6. BenchmarkEngine.list_industries | |
| # --------------------------------------------------------------------------- | |
| class TestListIndustries: | |
| def test_returns_all_profiles(self): | |
| engine = BenchmarkEngine() | |
| profiles = engine.list_industries() | |
| assert len(profiles) == len(INDUSTRY_PROFILES) | |
| assert all(isinstance(p, IndustryProfile) for p in profiles) | |
| def test_profiles_have_required_fields(self): | |
| engine = BenchmarkEngine() | |
| for p in engine.list_industries(): | |
| assert p.industry != "" | |
| assert p.display_name != "" | |
| assert p.sample_size > 0 | |
| # --------------------------------------------------------------------------- | |
| # 7. BenchmarkEngine.get_industry_profile | |
| # --------------------------------------------------------------------------- | |
| class TestGetIndustryProfile: | |
| def test_valid_industry(self): | |
| engine = BenchmarkEngine() | |
| profile = engine.get_industry_profile("fintech") | |
| assert profile.industry == "fintech" | |
| assert profile.display_name == "Financial Technology" | |
| def test_invalid_industry(self): | |
| engine = BenchmarkEngine() | |
| with pytest.raises(ValueError, match="Unknown industry"): | |
| engine.get_industry_profile("nonexistent") | |
| # --------------------------------------------------------------------------- | |
| # 8. BenchmarkEngine.compare_across_industries | |
| # --------------------------------------------------------------------------- | |
| class TestCompareAcrossIndustries: | |
| def test_compares_all(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| results = engine.compare_across_industries(graph) | |
| assert len(results) == len(INDUSTRY_PROFILES) | |
| assert all( | |
| isinstance(v, BenchmarkResult) for v in results.values() | |
| ) | |
| def test_consistent_score(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| results = engine.compare_across_industries(graph) | |
| # All results should have the same your_score | |
| scores = [r.your_score for r in results.values()] | |
| assert len(set(scores)) == 1 | |
| def test_different_percentiles(self): | |
| engine = BenchmarkEngine() | |
| # Use a mid-range graph so percentiles vary across industries | |
| graph = _make_graph(replicas=1, failover=False, autoscaling=False, circuit_breakers=False) | |
| results = engine.compare_across_industries(graph) | |
| # Percentiles should vary across industries (different avg scores) | |
| percentiles = [r.percentile for r in results.values()] | |
| # At minimum, the set should have more than 1 value | |
| # (industries have different score distributions) | |
| assert len(set(percentiles)) >= 1 # at minimum all results returned | |
| # --------------------------------------------------------------------------- | |
| # 9. Radar chart data | |
| # --------------------------------------------------------------------------- | |
| class TestRadarChartData: | |
| def test_generate_radar_data(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| result = engine.benchmark(graph, "fintech") | |
| radar = engine.generate_radar_chart_data(result) | |
| assert "labels" in radar | |
| assert "your_values" in radar | |
| assert "industry_values" in radar | |
| assert "industry" in radar | |
| assert "percentile" in radar | |
| assert "rank" in radar | |
| assert len(radar["labels"]) == len(radar["your_values"]) | |
| assert len(radar["labels"]) == len(radar["industry_values"]) | |
| assert len(radar["labels"]) >= 3 | |
| def test_radar_values_are_numeric(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph() | |
| result = engine.benchmark(graph, "saas") | |
| radar = engine.generate_radar_chart_data(result) | |
| for val in radar["your_values"]: | |
| assert isinstance(val, (int, float)) | |
| for val in radar["industry_values"]: | |
| assert isinstance(val, (int, float)) | |
| # --------------------------------------------------------------------------- | |
| # 10. ASCII comparison chart | |
| # --------------------------------------------------------------------------- | |
| class TestComparisonChart: | |
| def test_chart_contains_metrics(self): | |
| your_metrics = { | |
| "resilience": 75.0, | |
| "redundancy": 80.0, | |
| "isolation": 50.0, | |
| "recovery": 85.0, | |
| "diversity": 40.0, | |
| } | |
| industry_metrics = { | |
| "resilience": 78.0, | |
| "redundancy": 70.0, | |
| "isolation": 70.0, | |
| "recovery": 80.0, | |
| "diversity": 65.0, | |
| } | |
| chart = _build_comparison_chart( | |
| your_metrics, industry_metrics, "Fintech" | |
| ) | |
| assert "Fintech" in chart | |
| assert "Resilience" in chart | |
| assert "Redundancy" in chart | |
| assert "Isolation" in chart | |
| def test_chart_is_multiline(self): | |
| your_metrics = {"resilience": 80.0, "redundancy": 90.0} | |
| industry_metrics = {"resilience": 75.0, "redundancy": 80.0} | |
| chart = _build_comparison_chart( | |
| your_metrics, industry_metrics, "Test" | |
| ) | |
| assert "\n" in chart | |
| # --------------------------------------------------------------------------- | |
| # 11. Improvement priority ordering | |
| # --------------------------------------------------------------------------- | |
| class TestImprovementPriority: | |
| def test_priority_ordered_by_gap(self): | |
| engine = BenchmarkEngine() | |
| # Create a graph with specific weaknesses | |
| graph = InfraGraph() | |
| # Only 1 component type -> low diversity score | |
| for i in range(3): | |
| comp = Component( | |
| id=f"app_{i}", | |
| name=f"App {i}", | |
| type=ComponentType.APP_SERVER, | |
| replicas=1, | |
| failover=FailoverConfig(enabled=False), | |
| ) | |
| graph.add_component(comp) | |
| result = engine.benchmark(graph, "fintech") | |
| # Should have improvement priorities | |
| assert len(result.improvement_priority) >= 0 | |
| def test_no_priority_when_all_strong(self): | |
| engine = BenchmarkEngine() | |
| graph = _make_graph( | |
| num_components=5, replicas=3, | |
| failover=True, circuit_breakers=True, | |
| ) | |
| result = engine.benchmark(graph, "government") | |
| # Against government (low avg), strong graph may have few priorities | |
| # but shouldn't crash | |
| assert isinstance(result.improvement_priority, list) | |
Xet Storage Details
- Size:
- 16.6 kB
- Xet hash:
- 868854ea9b4c9036cfe41f8e73bc1139ff75dab8bd80c9e9b1d8456edc2f8597
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.