forked from ShiZhan/obs-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 17
/
qt-simulation-mmk.py
72 lines (55 loc) · 2.49 KB
/
qt-simulation-mmk.py
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
import simpy
import random
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
import argparse
def handler(env, name, server, service_time, latencies):
"""顾客进程"""
arrive_time = env.now
print(f"{name} arrives at time {arrive_time:.2f}")
with server.request() as req:
yield req
start_service_time = env.now
print(f"{name} starts being handled at time {start_service_time:.2f}")
yield env.timeout(service_time)
end_service_time = env.now
latency = end_service_time - arrive_time
print(f"{name} finishes at time {end_service_time:.2f}, total service time: {latency:.2f}")
# record request latency in latencies list
latencies.append(latency)
def simulate(λ, μ, r, latencies, server):
"""模拟函数"""
for i in range(r):
service_time = random.expovariate(μ)
env.process(handler(env, f"REQUEST {i+1}", server, service_time, latencies))
inter_arrival_time = random.expovariate(λ)
yield env.timeout(inter_arrival_time)
yield env.timeout(0) # 等待所有请求完成
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--lamda", type=float, default=9, help="Arrival rate (default: 9)")
parser.add_argument("-m", "--mu", type=float, default=10, help="Service rate (default: 10)")
parser.add_argument("-k", "--servers", type=int, default=1, help="Number of servers (default: 1)")
parser.add_argument("-r", "--requests", type=int, default=100, help="Number of requests to simulate (default: 100)")
args = parser.parse_args()
latencies = []
random.seed(42)
env = simpy.Environment()
server = simpy.Resource(env, capacity=args.requests)
env.process(simulate(args.lamda, args.mu, args.requests, latencies, server))
env.run()
"""绘制累计概率分布图"""
ax = plt.gca()
ax.yaxis.set_major_formatter(PercentFormatter(xmax=1, decimals=1))
plt.xlim(0, max(latencies))
plt.hist(latencies, cumulative=True, histtype='step', weights=[1./ len(latencies)] * len(latencies))
# 排队论模型
# F(t)=1-e^(-1*a*t)
μ_λ = args.mu - args.lamda
X_qt = np.arange(0, max(latencies), .01)
Y_qt = 1 - np.exp(-1 * μ_λ * X_qt)
# 绘制排队论模型拟合
plt.plot(X_qt, Y_qt)
plt.grid()
plt.show()