python主流框架对比
压测工具
https://github.com/wg/wrk
压测过程
压测结果
压测脚本
"""
生成对应的压测命令
解析shell返回的内容
绘制成折线图保存在当前的目录下
"""
import os
from pathlib import Path
import time
from pathlib import Path
import multiprocessing
import matplotlib.pyplot as plt
# pip install requests
# pip install pyparsing
# pip install matplotlib
# Ubuntu下让matplotlib显示中文字体
# https://blog.csdn.net/takedachia/article/details/131017286
# cp /usr/share/fonts/MyFonts/simhei.ttf /usr/local/lib/python3.10/dist-packages/matplotlib/mpl-data/fonts/
# python -m pip install --upgrade pip
# pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
shell_path = "example.sh"
path_log = "example.log"
path_png = "example2.png"
# 定义回合数
count = 40
# 并发数的步进数
connect_step = 20
# 持续压测的时间
runtime = 5
base_url = "http://119.29.161.77/get_json"
thread_number = multiprocessing.cpu_count()
def gen_command():
if os.path.exists(shell_path):
Path(shell_path).rename(Path(shell_path + "." + str(int(time.time()))))
for i in range(count):
connect = connect_step * (i + 1)
# 这个地方是生成执行shell命令的地方
bash_cmd = f"./wrk -t{thread_number} -c{connect} -d{runtime}s {base_url}\n"
with open(shell_path, "a") as f:
f.write(bash_cmd)
os.chmod(shell_path, 0o755)
def run_command():
# 读取文件
content_lines = []
with open(shell_path, "r") as file:
content_lines = file.readlines()
content_lines = [item.strip() for item in content_lines if item]
if os.path.exists(path_log):
Path(path_log).rename(Path(path_log + "." + str(int(time.time()))))
for item in content_lines:
print(item)
# result = os.system(item)
# print(result)
# if result:
# with open(path_log, "a") as f:
# f.write(result)
import subprocess
# return_code = subprocess.call([item])
# print(return_code)
with open(path_log, "a") as f:
subprocess.call([item], stdout=f, shell=True)
def read_log_file():
with open(path_log, "r") as file:
content_lines = file.readlines()
content_lines = [
item.strip() for item in content_lines if "Socket errors" not in item
]
# 分组
content_group = []
for index in range(0, len(content_lines), 8):
temp_list = []
for i in range(8):
print(i)
temp_list.append(content_lines[index + i])
content_group.append(temp_list)
# 组合数据
connections = []
requests_sec = []
for item in content_group:
print(item)
connections.append(int(item[1].split(" ")[3]))
latency_list = item[3].split(" ")
latency_list = [item2 for item2 in latency_list if item2]
latency_temp = latency_list[1].replace("ms",'')
latency_temp = int(float(latency_temp))
# requests_sec.append(latency_temp)
requests_sec.append(int(float(item[6].split(" ")[-1])))
return connections, requests_sec
def draw_png(x, y):
# # 准备数据
# x = [1, 2, 3, 4, 5] # x轴数据
# y = [2, 6, 1, 3, 10] # y轴数据
# 设置字体
# plt.rcParams['font.family']='Times New Roman, SimSun'
# 绘制折线图
# /usr/share/fonts/MyFonts
font_name = "simhei"
import matplotlib
matplotlib.rcParams["font.family"] = (
font_name # 指定字体,实际上相当于修改 matplotlibrc 文件 只不过这样做是暂时的 下次失效
)
matplotlib.rcParams["axes.unicode_minus"] = False # 正确显示负号,防止变成方框
plt.plot(x, y)
# # 添加标题和坐标轴标签
plt.title("折线图示例")
plt.xlabel("X轴_connections")
plt.ylabel("Y轴_requests_sec")
max_value = max(y)
max_indx = y.index(max_value)
show_max = "[" + str(x[max_indx]) + "," + str(y[max_indx]) + "]"
plt.annotate(
show_max, xytext=(x[max_indx], y[max_indx]), xy=(x[max_indx], y[max_indx])
)
plt.plot(x[max_indx], y[max_indx], "ks")
# # 显示图形
# # plt.show()
plt.grid()
plt.savefig(path_png)
if __name__ == "__main__":
gen_command()
run_command()
x, y = read_log_file()
draw_png(x, y)