import sys import tempfile import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'modelhub_submmit_api')) from routing_engine import SuccessFirstRoutingEngine from profile_admission import filter_profile_batch, profile_key def route(model='m', gpu='a', rate=0.8): return dict(repoId=model, targetGpu=gpu, framework='vllm', taskType='text-generation', frameworkMarketSamples=1000, frameworkMarketSuccessRate=rate, frameworkMarketWilsonLowerBound=rate - 0.04, frameworkEvidenceQualified=True, frameworkOfficialConfigValid=True, preflightMetadata={'modelType': 'qwen', 'quantizationSignature': 'none:abc'}) class ThroughputRoutingTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.engine = SuccessFirstRoutingEngine(Path(self.tmp.name) / 'routing.json') def test_alternative_survives_saturated_preferred_profile(self): best, fallback = route(gpu='a', rate=.9), route(gpu='b', rate=.8) ordered = self.engine.order_candidates([best, fallback]) self.assertEqual(2, len(ordered)) pending = [{**best, 'outcome': 'pending'} for _ in range(4)] selected, deferred = filter_profile_batch(ordered, pending, {}, max_selected=1) self.assertEqual(['b'], [r['targetGpu'] for r in selected]) self.assertEqual(['a'], [r['targetGpu'] for r in deferred]) def test_batch_has_one_route_per_model(self): selected, _ = filter_profile_batch([route(gpu='a'), route(gpu='b'), route('other')], [], {}) self.assertEqual(['m', 'other'], [r['repoId'] for r in selected]) def test_strong_public_evidence_increases_productive_concurrency(self): candidates = [route(str(i)) for i in range(8)] self.assertEqual(4, len(filter_profile_batch(candidates, [], {})[0])) for r in candidates: r['frameworkEvidenceQualified'] = False self.assertEqual(2, len(filter_profile_batch(candidates, [], {})[0])) def test_exact_failures_prevent_public_expansion_and_lower_ranking(self): candidate = route() stats = {'encodingCombinationStats': {'|'.join(profile_key(candidate)): { 'successCount': 0, 'failureCount': 5}}} self.assertEqual(2, len(filter_profile_batch([route(str(i)) for i in range(8)], [], stats)[0])) before = self.engine.annotate(candidate)['routingSuccessLowerBound'] self.engine.outcome_stats = stats after = self.engine.annotate(candidate) self.assertLess(after['routingSuccessLowerBound'], before) self.assertEqual('local_encoding_shrunk', after['routingEvidenceLevel']) def test_small_batch_does_not_consume_unselected_profile_budget(self): selected, deferred = filter_profile_batch([route(str(i)) for i in range(8)], [], {}, max_selected=1) self.assertEqual(1, len(selected)) self.assertEqual([], deferred) def test_unknown_queue_does_not_invent_eta_or_success_per_hour(self): result = self.engine.annotate({**route(), 'queueBacklogHours': .01}) self.assertTrue(result['routingEtaUnknown']) self.assertIsNone(result['routingExpectedSuccessPerHour']) def test_validated_speed_can_win_within_quality_frontier(self): slow = {**route('slow', rate=.90), 'queueBacklogHours': 100, 'queueEtaValidated': True} fast = {**route('fast', rate=.89), 'queueBacklogHours': 1, 'queueEtaValidated': True} self.assertEqual('fast', self.engine.order_candidates([slow, fast])[0]['repoId']) poor = {**route('poor', rate=.3), 'queueBacklogHours': .25, 'queueEtaValidated': True} self.assertEqual('slow', self.engine.order_candidates([poor, slow])[0]['repoId']) def test_sample_threshold_has_no_abrupt_source_switch(self): a = {**route(), 'frameworkLocalSamples': 99, 'frameworkLocalSuccessRate': .3} b = {**a, 'frameworkLocalSamples': 100} self.assertLess(abs(self.engine._evidence(a)[3] - self.engine._evidence(b)[3]), .01) def test_platform_failures_lower_operational_score(self): healthy = self.engine.annotate(route()) unhealthy = self.engine.annotate({**route(), 'frameworkRecentPlatformFailures': 8, 'frameworkRecentTerminalSamples': 10}) self.assertLess(unhealthy['routingSuccessEstimate'], healthy['routingSuccessEstimate']) def test_readmission_after_pending_completes(self): candidate = route() pending = [{**candidate, 'outcome': 'pending'} for _ in range(4)] self.assertEqual([], filter_profile_batch([candidate], pending, {})[0]) pending[0]['outcome'] = 'success' self.assertEqual(1, len(filter_profile_batch([candidate], pending, {})[0])) if __name__ == '__main__': unittest.main()