Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Demo script to test the queueing network simulation.
This demonstrates Scenario 1 from the project requirements:
1 fast server - should be UNSTABLE (ρ > 1)
"""
from src.core.simulation import Simulator, SimulationConfig
def main():
print("=" * 70)
print("QUEUEING NETWORK SIMULATION - DEMO")
print("=" * 70)
# Scenario 1: 1 fast server (should be unstable)
print("\n📊 SCENARIO 1: Single Fast Server (Instability Test)")
print("-" * 70)
config = SimulationConfig(
arrival_rate=1/125, # Mean inter-arrival: 125ms
coordinator_service_rate=1/10, # Mean service: 10ms
coordinator_exit_probability=0.5, # p = 0.5
server_service_rates=[1/120], # Mean service: 120ms (fast server)
server_routing_probs=[0.5], # q1 = 0.5
warmup_time=10000.0,
simulation_time=50000.0,
random_seed=42
)
print(f"\nConfiguration:")
print(f" λ (arrival rate) = {config.arrival_rate:.6f} req/ms")
print(f" μc (coordinator) = {config.coordinator_service_rate:.6f} req/ms")
print(f" μ1 (server 1) = {config.server_service_rates[0]:.6f} req/ms")
print(f" p (exit prob) = {config.coordinator_exit_probability}")
print(f" q1 (routing prob) = {config.server_routing_probs[0]}")
# Calculate theoretical utilizations
lambda_ext = config.arrival_rate
lambda_c = lambda_ext
lambda_1 = lambda_ext * config.server_routing_probs[0]
rho_c = lambda_c / config.coordinator_service_rate
rho_1 = lambda_1 / config.server_service_rates[0]
print(f"\n📐 Theoretical Analysis (Jackson):")
print(f" λc = {lambda_c:.6f} (effective arrival rate at coordinator)")
print(f" λ1 = {lambda_1:.6f} (effective arrival rate at server 1)")
print(f" ρc = λc/μc = {rho_c:.4f}")
print(f" ρ1 = λ1/μ1 = {rho_1:.4f} {'❌ UNSTABLE (>1)' if rho_1 >= 1 else '✅ STABLE'}")
if rho_c >= 1 or rho_1 >= 1:
print(f"\n⚠️ EXPECTED: System is UNSTABLE - queues will grow without bound")
else:
print(f"\n✅ EXPECTED: System is STABLE")
# Run simulation
print(f"\n🚀 Running simulation...")
simulator = Simulator(config)
results = simulator.run()
# Display results
print("\n" + "=" * 70)
print("SIMULATION RESULTS")
print("=" * 70)
print(f"\n📈 Overall Statistics:")
print(f" Total requests arrived: {results.total_requests_arrived}")
print(f" Requests completed (after warmup): {results.total_requests_completed}")
print(f" Average system time: {results.average_system_time:.2f} ms")
print(f"\n📊 Coordinator Queue:")
coord = results.coordinator_stats
print(f" Arrivals: {coord['total_arrivals']}")
print(f" Departures: {coord['total_departures']}")
print(f" Avg wait time: {coord['average_wait_time']:.2f} ms")
print(f" Avg service time: {coord['average_service_time']:.2f} ms")
print(f" Avg system time: {coord['average_system_time']:.2f} ms")
print(f" Utilization: {coord['utilization']:.4f} (theoretical: {rho_c:.4f})")
print(f"\n📊 Server 1 Queue:")
server1 = results.server_stats['server_1']
print(f" Arrivals: {server1['total_arrivals']}")
print(f" Departures: {server1['total_departures']}")
print(f" Avg wait time: {server1['average_wait_time']:.2f} ms")
print(f" Avg service time: {server1['average_service_time']:.2f} ms")
print(f" Avg system time: {server1['average_system_time']:.2f} ms")
print(f" Utilization: {server1['utilization']:.4f} (theoretical: {rho_1:.4f})")
# Verdict
print("\n" + "=" * 70)
if server1['utilization'] > 0.95:
print("⚠️ VERDICT: System is UNSTABLE - Server 1 is overloaded")
print(" Queue is growing, system cannot keep up with arrivals")
else:
print("✅ VERDICT: System appears STABLE")
print("=" * 70)
if __name__ == "__main__":
main()