-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_network.py
More file actions
157 lines (117 loc) · 5.4 KB
/
Copy pathopen_network.py
File metadata and controls
157 lines (117 loc) · 5.4 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
"""
Open Network Example
This example demonstrates an open queueing network with
multiple nodes and shows how to analyze web server performance.
"""
import pdq
def main():
"""Run open network example"""
print("=== Open Network Example: Web Application ===\\n")
# Model parameters
arrival_rate = 10.0 # requests per second
# Service times at each tier
loadbalancer_time = 0.005 # 5ms
webserver_time = 0.050 # 50ms
database_time = 0.100 # 100ms
print(f"Model Parameters:")
print(f" Arrival rate: {arrival_rate} req/sec")
print(f" Load balancer time: {loadbalancer_time*1000:.1f} ms")
print(f" Web server time: {webserver_time*1000:.1f} ms")
print(f" Database time: {database_time*1000:.1f} ms")
print()
# Build the network
print("Building network topology...")
# Create model for web application
model = pdq.PDQModel("Web Application")
model.create_open("HTTPRequests", arrival_rate)
model.set_comment("Three-tier web application with load balancer")
# Create nodes
model.create_node("LoadBalancer", pdq.CEN, pdq.FCFS)
model.create_node("WebServer", pdq.CEN, pdq.FCFS)
model.create_node("Database", pdq.CEN, pdq.FCFS)
# Set appropriate units
model.set_work_unit("Requests")
model.set_time_unit("Seconds")
# Set service demands
model.set_demand("LoadBalancer", "HTTPRequests", loadbalancer_time)
model.set_demand("WebServer", "HTTPRequests", webserver_time)
model.set_demand("Database", "HTTPRequests", database_time)
print("Network built successfully\\n")
# Solve using canonical method for open networks
print("Solving open network...")
model.solve(pdq.CANON) # or pdq.STREAMING
print("Solution completed\\n")
# Get system-level metrics
response_time = model.get_response(pdq.TRANS, "HTTPRequests")
throughput = model.get_throughput(pdq.TRANS, "HTTPRequests")
print(f"System Performance:")
print(f" End-to-end response time: {response_time*1000:.1f} ms")
print(f" System throughput: {throughput:.2f} req/sec")
print()
# Get node-level metrics
nodes = ["LoadBalancer", "WebServer", "Database"]
service_times = [loadbalancer_time, webserver_time, database_time]
print(f"Node Performance:")
print(f"{'Node':<12} | {'Residence':<10} | {'Utilization':<11} | {'Queue Len':<9}")
print("-" * 50)
total_residence = 0
for node_name, svc_time in zip(nodes, service_times):
residence = model.get_residence_time(node_name, "HTTPRequests", pdq.TRANS)
utilization = model.get_utilization(node_name, "HTTPRequests", pdq.TRANS)
queue_length = model.get_queue_length(node_name, "HTTPRequests", pdq.TRANS)
total_residence += residence
print(f"{node_name:<12} | "
f"{residence*1000:8.1f}ms | "
f"{utilization*100:9.1f}% | "
f"{queue_length:7.3f}")
print()
print(f"Total residence time: {total_residence*1000:.1f} ms")
print(f"Service time only: {sum(service_times)*1000:.1f} ms")
print(f"Queueing delay: {(total_residence - sum(service_times))*1000:.1f} ms")
print()
# Bottleneck analysis
bottleneck_util = 0
bottleneck_node = ""
for node_name in nodes:
util = model.get_utilization(node_name, "HTTPRequests", pdq.TRANS)
if util > bottleneck_util:
bottleneck_util = util
bottleneck_node = node_name
print(f"Bottleneck Analysis:")
print(f" Bottleneck node: {bottleneck_node}")
print(f" Bottleneck utilization: {bottleneck_util*100:.1f}%")
# Calculate max possible throughput
max_throughput = model.get_max_throughput(pdq.TRANS, "HTTPRequests")
capacity_remaining = max_throughput - throughput
print(f" Max throughput: {max_throughput:.2f} req/sec")
print(f" Remaining capacity: {capacity_remaining:.2f} req/sec")
print()
# What-if analysis: double the arrival rate
print("=== What-If Analysis: Double the Load ===")
model2 = pdq.PDQModel("Web Application - High Load")
high_arrival_rate = arrival_rate * 2
model2.create_node("LoadBalancer", pdq.CEN, pdq.FCFS)
model2.create_node("WebServer", pdq.CEN, pdq.FCFS)
model2.create_node("Database", pdq.CEN, pdq.FCFS)
model2.create_open("HTTPRequests", high_arrival_rate)
model2.set_demand("LoadBalancer", "HTTPRequests", loadbalancer_time)
model2.set_demand("WebServer", "HTTPRequests", webserver_time)
model2.set_demand("Database", "HTTPRequests", database_time)
try:
model2.solve(pdq.CANON)
high_response = model2.get_response(pdq.TRANS, "HTTPRequests")
high_throughput = model2.get_throughput(pdq.TRANS, "HTTPRequests")
print(f"With {high_arrival_rate} req/sec arrival rate:")
print(f" Response time: {high_response*1000:.1f} ms")
print(f" Throughput: {high_throughput:.2f} req/sec")
print(f" Response time increase: {(high_response/response_time):.1f}x")
except pdq.PDQSolutionError as e:
print(f"System would be saturated at {high_arrival_rate} req/sec")
print(f"Error: {e}")
print()
# Generate full report
print("=== Full PDQ Report (Original Load) ===")
model.print_report()
if __name__ == "__main__":
main()