import importlib.util from pathlib import Path def _load_runtime_hook(case_id: str): path = ( Path(__file__).resolve().parents[2] / "evaluation" / "regression" / "cases" / case_id / "runtime_hook.py" ) spec = importlib.util.spec_from_file_location(f"runtime_hook_{case_id}", path) assert spec is not None assert spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_read_pool_cpu_runtime_hook_uses_thread_cpu_queries(monkeypatch): module = _load_runtime_hook("read-pool-cpu-scope-compare-in-premium-cluster-during-latest-window") calls: list[dict] = [] def _payload(series): return { "status": "success", "data": { "result": series, }, } per_instance_series = [ { "metric": {"instance": "db-125d9e7b-tikv-a"}, "values": [[1784531400, "2.5"], [1784531460, "6.1"]], }, { "metric": {"instance": "db-125d9e7b-tikv-b"}, "values": [[1784531400, "1.2"], [1784531460, "4.3"]], }, ] cluster_total_series = [ { "metric": {}, "values": [[1784531400, "3.7"], [1784531460, "10.4"]], } ] class FakeClient: def __init__(self, *, api_key: str, base_url: str, timeout_s: int = 60) -> None: self.api_key = api_key self.base_url = base_url self.timeout_s = timeout_s def query_range( self, *, cluster_id: str, promql: str, start: int, end: int, step_seconds: int, ) -> dict: calls.append( { "cluster_id": cluster_id, "promql": promql, "start": start, "end": end, "step_seconds": step_seconds, } ) if promql == module.PER_INSTANCE_QUERY: return _payload(per_instance_series) if promql == module.CLUSTER_TOTAL_QUERY: return _payload(cluster_total_series) raise AssertionError(f"unexpected promql: {promql}") monkeypatch.setattr(module, "_env_api_key", lambda: "token") monkeypatch.setattr(module, "ClinicClient", FakeClient) payload = module._build_reference( cluster_id="10748116073341772391", metrics_cluster_id="2026948324023603200", start="2026-07-20T07:10:00Z", end="2026-07-20T08:00:00Z", ) assert [call["promql"] for call in calls] == [ 'sum(rate(tikv_thread_cpu_seconds_total{name=~"unified_read.*"}[1m])) by (instance)', 'sum(sum by (instance) (rate(tikv_thread_cpu_seconds_total{name=~"unified_read.*"}[1m])))', ] assert all(call["cluster_id"] == "2026948324023603200" for call in calls) assert payload == { "cluster_id": "10748116073341772391", "metrics_cluster_id": "2026948324023603200", "window": { "start": "2026-07-20T07:10:00Z", "end": "2026-07-20T08:00:00Z", }, "evidence_sufficient": True, "peak_instance_node": "db-125d9e7b-tikv-a", "peak_instance_read_pool_cpu_cores": 6.1, "cluster_total_peak_read_pool_cpu_cores": 10.4, }