python对exe程序进行压力测试并捕抓log的例子
import os
import subprocess
import time
class Pressuretest(object):
def __init__(self, com, path):
# 类的属性
self.COM = com
self.exe_path = path
self.total_num = 0 # 总数
self.success_num = 0 # 通过数
self.error_num = 0 # 失败数
# 捕抓失败信息
def catch_information(self, cmd_log):
cmd_log = str(cmd_log)
# print(cmd_log)
# print(type(cmd_log))
is_error_state = cmd_log.find("failed") # find()查到的值不存在时会返回-1
return is_error_state
def statistical_data(self):
print("当前测试情况为:")
print(f"测试总次数为:{self.total_num}")
print(f"成功总次数为:{self.success_num}")
print(f"失败总次数为:{self.error_num}")
# 正式开始测试
def start_pressure_test(self):
exe_file = self.exe_path # 你要运行的exe文件
params1 = str(self.COM) # 你要传入的参数
# cmd_log = os.system(main+' '+params1) #这个无法捕抓log
while True:
if self.total_num % 10 == 0:
print(f"开始第{self.total_num + 1}次测试")
self.statistical_data()
cmd_log = subprocess.check_output([exe_file, params1], shell=True)
# print(type(cmd_log))
is_error_state = self.catch_information(cmd_log)
self.total_num = self.total_num + 1
if is_error_state == -1: # 找不到"failed",则说明成功了
# print("成功")
self.success_num = self.success_num + 1
else:
# print("失败")
self.error_num = self.error_num + 1
print(cmd_log)
break
# time.sleep(1)
# 测试后处理
def after_pressure_test(self):
print("测试结束")
self.statistical_data()
# 开始测试流程
def start_test(self):
self.start_pressure_test()
self.after_pressure_test()
def main():
com = 26 # 端口
path = "Bes_Demo_Anc.exe" # 路径
dll_pressure_test_obj = Pressuretest(com, path)
dll_pressure_test_obj.start_test()
if __name__ == "__main__":
main()
本文来自博客园,作者:JaxonYe,转载请注明原文链接:https://www.cnblogs.com/yechangxin/articles/16093645.html
侵权必究