|
| 1 | +"""Integration test: LB with two servers and concurrent event injections. |
| 2 | +
|
| 3 | +Topology: |
| 4 | +
|
| 5 | + rqs-1 → client-1 → lb-1 → {srv-1, srv-2} |
| 6 | + srv-* → client-1 |
| 7 | +
|
| 8 | +Events: |
| 9 | +- NETWORK_SPIKE on 'client-to-lb' in [0.20, 0.35]. |
| 10 | +- SERVER_DOWN/UP on 'srv-1' in [0.40, 0.55]. |
| 11 | +
|
| 12 | +Assertions: |
| 13 | +- Simulation completes. |
| 14 | +- Latency stats and throughput exist. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from typing import TYPE_CHECKING |
| 20 | + |
| 21 | +import simpy |
| 22 | + |
| 23 | +from asyncflow.config.constants import Distribution, EventDescription, LatencyKey |
| 24 | +from asyncflow.runtime.simulation_runner import SimulationRunner |
| 25 | +from asyncflow.schemas.common.random_variables import RVConfig |
| 26 | +from asyncflow.schemas.events.injection import EventInjection |
| 27 | +from asyncflow.schemas.payload import SimulationPayload |
| 28 | +from asyncflow.schemas.settings.simulation import SimulationSettings |
| 29 | +from asyncflow.schemas.topology.edges import Edge |
| 30 | +from asyncflow.schemas.topology.graph import TopologyGraph |
| 31 | +from asyncflow.schemas.topology.nodes import ( |
| 32 | + Client, |
| 33 | + LoadBalancer, |
| 34 | + Server, |
| 35 | + ServerResources, |
| 36 | + TopologyNodes, |
| 37 | +) |
| 38 | +from asyncflow.schemas.workload.rqs_generator import RqsGenerator |
| 39 | + |
| 40 | +if TYPE_CHECKING: |
| 41 | + from asyncflow.metrics.analyzer import ResultsAnalyzer |
| 42 | + |
| 43 | + |
| 44 | +def _server(sid: str) -> Server: |
| 45 | + return Server(id=sid, server_resources=ServerResources(), endpoints=[]) |
| 46 | + |
| 47 | + |
| 48 | +def _edge(eid: str, src: str, tgt: str, mean: float = 0.002) -> Edge: |
| 49 | + return Edge( |
| 50 | + id=eid, |
| 51 | + source=src, |
| 52 | + target=tgt, |
| 53 | + latency=RVConfig(mean=mean, distribution=Distribution.POISSON), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_lb_two_servers_with_events_end_to_end() -> None: |
| 58 | + """Round-robin LB with events; check that KPIs are produced.""" |
| 59 | + env = simpy.Environment() |
| 60 | + rqs = RqsGenerator( |
| 61 | + id="rqs-1", |
| 62 | + avg_active_users=RVConfig(mean=1.0), |
| 63 | + avg_request_per_minute_per_user=RVConfig(mean=2.0), |
| 64 | + user_sampling_window=10.0, |
| 65 | + ) |
| 66 | + sim = SimulationSettings(total_simulation_time=0.8) |
| 67 | + |
| 68 | + client = Client(id="client-1") |
| 69 | + lb = LoadBalancer(id="lb-1") |
| 70 | + srv1 = _server("srv-1") |
| 71 | + srv2 = _server("srv-2") |
| 72 | + |
| 73 | + edges = [ |
| 74 | + _edge("gen-to-client", "rqs-1", "client-1"), |
| 75 | + _edge("client-to-lb", "client-1", "lb-1"), |
| 76 | + _edge("lb-to-srv1", "lb-1", "srv-1"), |
| 77 | + _edge("lb-to-srv2", "lb-1", "srv-2"), |
| 78 | + _edge("srv1-to-client", "srv-1", "client-1"), |
| 79 | + _edge("srv2-to-client", "srv-2", "client-1"), |
| 80 | + ] |
| 81 | + nodes = TopologyNodes(servers=[srv1, srv2], client=client, load_balancer=lb) |
| 82 | + topo = TopologyGraph(nodes=nodes, edges=edges) |
| 83 | + |
| 84 | + events = [ |
| 85 | + EventInjection( |
| 86 | + event_id="spike", |
| 87 | + target_id="client-to-lb", |
| 88 | + start={ |
| 89 | + "kind": EventDescription.NETWORK_SPIKE_START, |
| 90 | + "t_start": 0.20, |
| 91 | + "spike_s": 0.02, |
| 92 | + }, |
| 93 | + end={"kind": EventDescription.NETWORK_SPIKE_END, "t_end": 0.35}, |
| 94 | + ), |
| 95 | + EventInjection( |
| 96 | + event_id="outage-srv1", |
| 97 | + target_id="srv-1", |
| 98 | + start={"kind": EventDescription.SERVER_DOWN, "t_start": 0.40}, |
| 99 | + end={"kind": EventDescription.SERVER_UP, "t_end": 0.55}, |
| 100 | + ), |
| 101 | + ] |
| 102 | + |
| 103 | + payload = SimulationPayload(rqs_input=rqs, topology_graph=topo, sim_settings=sim) |
| 104 | + payload.events = events |
| 105 | + |
| 106 | + runner = SimulationRunner(env=env, simulation_input=payload) |
| 107 | + results: ResultsAnalyzer = runner.run() |
| 108 | + |
| 109 | + stats = results.get_latency_stats() |
| 110 | + assert stats |
| 111 | + assert stats[LatencyKey.TOTAL_REQUESTS] > 0 |
| 112 | + ts, rps = results.get_throughput_series() |
| 113 | + assert len(ts) == len(rps) > 0 |
0 commit comments