背景: 近期打算把接口平台的引擎换成jmeter,同时兼容性能和接口自动化
1. 随之而来的问题 - 报告解析:
以前每次在linux上压测,最麻烦的就是报错的时候,要把jtl文件拷贝出来,然后导入jmeterGUI界面查看结果树,显示错误的请求信息,所以这次重构平台,打算直接解析jtl文件展示到平台前端。
2. 使用的第三方库xml
PS: 要把jmeter.properties中的配置改成jmeter.save.saveservice.output_format=xml
# -*- coding=utf-8 -*-
import xml.etree.ElementTree as ET
from pprint import pprint
tree = ET.ElementTree() # 实例化
tree.parse("C:/Users/Administrator/Desktop/test2.jtl")
root = tree.getroot()
err_list = list()
for child in root:
if child.get("s") == "false":
data = dict()
data["name"] = child.get("lb")
for sub in child:
data[sub.tag] = sub.text
err_list.append(data)
pprint(err_list)
输出如下:
[{'assertionResult': '\n ',
'cookies': None,
'java.net.URL': 'https://xxxx/login/',
'method': 'POST',
'name': 'xxx登录',
'queryString': '-----------------------------7d159c1302d0y0\r\n'
'Content-Disposition: form-data; name="username"\r\n'
'Content-Type: text/plain; charset=UTF-8\r\n'
'Content-Transfer-Encoding: 8bit\r\n'
'\r\n'
'jms13999999999\r\n'
'-----------------------------7d159c1302d0y0\r\n'
'Content-Disposition: form-data; name="password"\r\n'
'Content-Type: text/plain; charset=UTF-8\r\n'
'Content-Transfer-Encoding: 8bit\r\n'
'\r\n'
'yh111111\r\n'
'-----------------------------7d159c1302d0y0\r\n'
'Content-Disposition: form-data; name="smscode"\r\n'
'Content-Type: text/plain; charset=UTF-8\r\n'
'Content-Transfer-Encoding: 8bit\r\n'
'\r\n'
'971011\r\n'
'-----------------------------7d159c1302d0y0--\r\n',
'requestHeader': 'Connection: keep-alive\n'
'Content-Length: 590\n'
'Content-Type: multipart/form-data; '
'boundary=---------------------------7d159c1302d0y0\n',
'responseData': '{"response": "ok", "data": "", "error": "", "message": ""}',
'responseHeader': 'HTTP/1.1 200 OK\n'
'Date: Fri, 04 Dec 2020 03:36:57 GMT\n'
'Content-Type: text/html; charset=utf-8\n'
'Content-Length: 58\n'
'Connection: keep-alive\n'
'Vary: Cookie\n'
'strict-transport-security: max-age=600\n'
'Set-Cookie: device_no=QgBqrTI6NEUO2hR18yxn; Path=/\n'
'Set-Cookie: sessionid=o6mw7prygvs24bgtyppntkyxivyl8gli; '
'HttpOnly; Path=/\n'}]