比较简单就不注释了
import subprocess
import time
# import re
import os
import sys
import json
# from requests_html import HTMLSession
base_path = '/data/jmeter/apache-jmeter-5.1.1/bin/data'
thread_num = 'ThreadGroup.num_threads'
my_path = '/data/zyz/'
def getAll(dir_list):
try:
result_list = []
print('getAll**********')
print(dir_list)
for i in dir_list:
p = i + '/' + 'statistics.json'
re = analysisJSON(p)
result_list.append(re)
print(result_list)
max_ = result_list[0]
for i in result_list[1:]:
if i > max_:
max_ = i
else:
continue
max_index = result_list.index(max_)
max_file = dir_list[max_index]
print(max_file)
print("当并发数为" + max_file.split('_')[len(max_file.split('_')) - 1] + "的时候,压测的吞吐量最大。最大值是" + str(max_))
except Exception as e:
print(e)
def analysisJSON(json_path):
try:
# print(index_path)
# session = HTMLSession()
# session.mount('file://', FileAdapter())
# # Windows系统路径目录分隔符为反斜杠,但get需要正斜杠所以先进行一下替换
# pwd = os.getcwd().replace("\\", "/")
# h = session.get(url=index_path)
# print(h.html.html)
# # print(h.html.links)
with open(json_path) as fd:
json_result = json.load(fd)
# print(json_result)
throughput = json_result['HTTP请求']['throughput']
print(throughput)
return float(throughput)
fd.close()
except Exception as e:
print(e)
def readJmx(file, num, dir_pre=None):
if file:
# cur_path = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE)
# if cur_path.stdout.read().decode("utf-8") != base_path:
# subprocess.Popen("cd " + base_path, shell=True, stdout=subprocess.PIPE)
file_list = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE)
print(file_list.stdout.read().decode("utf-8"))
if not file_list.stdout.read().decode("utf-8").find(file):
print("error file name!")
else:
with open(file, 'r') as fd:
readLines = fd.readlines()
# print(readLines)
with open(file, 'w+') as fw:
for line in readLines:
if thread_num in line:
# print(type(line.index(thread_num)))
# print(type(num))
# print(type(len(thread_num)))
# print(line)
start = line.index(thread_num) + len(thread_num) + 2
if line[start + 1] == '<':
# print('当前线程数为', line[start])
nline = line.replace(line[start], str(num))
print(nline)
fw.write(nline)
else:
# print('当前线程数为', line[start: start + 2])
nline = line.replace(line[start: start + 2], str(num))
print(nline)
fw.write(nline)
else:
fw.write(line)
fw.close()
fd.close()
try:
if not dir_pre:
cmd = 'jmeter.sh -n -t ' + file + ' -l ' + 'test_' + str(num) + '.jtl'
print(cmd)
ru = subprocess.Popen(cmd ,shell=True, stdout=subprocess.PIPE)
print(ru.stdout.read().decode('utf-8'))
else:
cmd = 'jmeter.sh -n -t ' + file + ' -l ' + 'test_' + str(num) + '.jtl' + ' -e -o ' + my_path + dir_pre + '_' + str(num)
print(cmd)
ru = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
print(ru.stdout.read().decode('utf-8'))
except Exception as e:
print(e)
else:
print("请输入性能测试脚本名称!")
if __name__ == "__main__":
# file = 'as_search.jmx'
argv = sys.argv
print(argv)
l = len(argv)
if len(argv) == 1:
print("please input enough arguments!")
print('*************Help Infp*************')
print('-f xxx.jmx -t start end step -m dir_pre \n'
'这是完整的指令:-f xxx.jmx用于摸底拐点,默认从1到25次并发,step默认为1\n'
'-f xxx.jmx -t start end step :表明自定义并发开始值和结束值以及step\n'
'-f xxx.jmx -t start end step -m dir_pre: -m dir_pre用于拐点峰值\n')
elif l == 3 and argv[1] == '-f':
file = argv[2]
for i in range(1, 10):
readJmx(file, i)
subprocess.Popen('rm D*', shell=True, stdout=subprocess.PIPE)
elif l == 7 and argv[1] == '-f' and argv[3] == '-t':
print("默认step都是大于等于1的,逆序不作处理")
if type(argv[4]) == type(argv[5]) and type(argv[6]) == type(argv[5]) and type(argv[5]) == type(1):
file = argv[2]
start = int(argv[4])
end = int(argv[5])
step = int(argv[6])
if start >= 1 and end > start and step >= 1:
for i in range(start, end + 1, step):
readJmx(file, i)
subprocess.Popen('rm D*', shell=True, stdout=subprocess.PIPE)
elif l == 9 and argv[1] == '-f' and argv[3] == '-t' and argv[7] == '-m':
file = argv[2]
start = int(argv[4])
end = int(argv[5])
step = int(argv[6])
dir_pre = argv[8]
dir_list = []
# print("待定。。。")
for i in range(start, end + 1, step):
path = my_path + dir_pre + '_' + str(i)
dir_list.append(path)
readJmx(file, i, dir_pre=dir_pre)
subprocess.Popen('rm D*', shell=True, stdout=subprocess.PIPE)
getAll(dir_list)
else:
print("please input your cmd like help info.ß")