jmeter怎么在linux使用及封装操作
一、背景:
1.众所周知jmeter在GUI模式下执行、对本机消耗很大,当大量压测时建议使用命令行模式(linux或windows都可以);
2.在 Linux 中执行 JMeter 脚本时候,大家是否一直使用jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]命令执行,是否想过通过 shell 脚本或者 python 快速执行。
今天简单介绍下 shell 脚本执行。前置条件需要配置 JMeter 环境变量,如果没有配置,需要在脚本中修改相应位置。
重点:jmx脚本参数提取集控制
二、结构和流程:
三、jmx脚本控制:
1.python控制jmx脚本下的请求状态 (在linux修改jmx脚本比较麻烦,所以通过脚本处理比较好)
#! /usr/bin/env python # -*- coding:utf-8 -*- import os,sys import xml.etree.ElementTree as ET def main(): #单接口指定状态 specified_state={ "AI助理—基础信息":True, "基准接口": False, "AI助理—更新基础规则":False, "AI助理—技能列表":False, "AI助理—设置高级技能":False, "AI助理—查看可用技能列表(available)":False, "流式聊天(妙语Gpt3.5)":False, "同步聊天(妙语Gpt3.5)":False, "外链聊天-同步":False, "外链聊天-流式":False, "创建知识集":False, "知识库列表":False, "新增知识库-文件":False, "薪友聊天-异步":False, "查询数据集列表":False, "命中测试":False, "企微-获取账号会话列表":False, "企微-获取标签列表":False, "企微-获取账号设置":False, "企微-获取消息会话消息":False, "企微-获取群成员列表":False, "企微-发送消息":False, "企微-数据看板":False, "企微-创建标签":False, "企微-添加用户到标签":False } #业务接口指定状态 business_tate={ "助理初始化":False, "AI助理—基础信息":False, "AI助理—更新基础规则":False, "AI助理—设置高级技能":True, "AI助理—查看可用技能列表(available)":True, "AI助理—技能列表":False, "流式聊天(妙语Gpt3.5)":True, "同步聊天(妙语Gpt3.5)":True, "新增知识库-文件":True, "向量化切片":False, "向量化保存切片":True, "查询数据集列表":True, "外链聊天-流式":True, "外链聊天-同步":True, "薪友聊天-异步":False, "命中测试":True } #企微接口指定状态 chat_tate={ "助理初始化":True, "获取账号会话列表":True, "获取标签列表":True, "获取账号设置":True, "获取群成员列表":False, "获取消息会话消息":False, "发送消息":False, "数据看板":True, "创建标签":True, "添加用户到标签":False } #智慧速记接口状态 batch_tate={ "提取关键词":False, "生成任务":True, "生成会议纪要":False } if os.path.exists(sys.argv[1]): print("参数路径:",sys.argv[1]) # 1.读取XML文档 tree = ET.parse(sys.argv[1]) root = tree.getroot() #遍历XML文档(################把testname的状态全部改为false##################) for child in root[0][1][5]: print('Tag:', child.tag) print('Text:', child.text) print('Attributes:', child.attrib) if child.tag == 'HTTPSamplerProxy' : print(child.get("testname")) print(child.get("enabled")) child.set('enabled', "false") elif child.tag == 'IfController': print("=======if控制器==========") print(child.get("testname")) print(child.get("enabled")) child.set('enabled', "false") #再次遍历XML文档(把部分testname的状态、改为ture) for child in root[0][1][5]: if child.tag == 'HTTPSamplerProxy': if "单接口测试计划" in sys.argv[1]: #通过字典里是否有testname这个key if specified_state.get(child.get("testname")): # print(child.get("testname")) # print(child.get("enabled")) child.set('enabled', "true") elif "业务测试计划" in sys.argv[1]: if business_tate.get(child.get("testname")): # print(child.get("testname")) # print(child.get("enabled")) child.set('enabled', "true") elif "企微测试计划" in sys.argv[1]: if chat_tate.get(child.get("testname")): # print(child.get("testname")) # print(child.get("enabled")) child.set('enabled', "true") elif "智慧速记" in sys.argv[1]: if batch_tate.get(child.get("testname")): # print(child.get("testname")) # print(child.get("enabled")) child.set('enabled', "true") elif child.tag == 'IfController': if "业务测试计划" in sys.argv[1]: child.set('enabled', str(business_tate.get(child.get("testname"))).lower()) elif "企微测试计划" in sys.argv[1]: child.set('enabled', str(chat_tate.get(child.get("testname"))).lower()) else: #print("无需重新赋值") pass tree.write(sys.argv[1], encoding="utf-8") else: print("文件不存在,请检查映射路径!") if __name__ == '__main__': main()
备份脚本
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys import xml.etree.ElementTree as ET #单接口指定状态 specified_state={ "助理初始化":False, "AI助理—基础信息":False, "基准接口": True, "AI助理—更新基础规则":False, "AI助理—技能列表":False, "AI助理—设置高级技能":False, "AI助理—查看可用技能列表(available)":False, "流式聊天(妙语Gpt3.5)":False, "同步聊天(妙语Gpt3.5)":False, "外链聊天-同步":False, "外链聊天-流式":False, "创建知识集":False, "知识库列表":False, "新增知识库-文件":False, "薪友聊天-异步":False, "向量化切片":False, "向量化保存切片":False, "查询数据集列表":False, "命中测试":False, "企微-获取账号会话列表":False, "企微-获取标签列表":False, "企微-获取账号设置":False, "企微-获取消息会话消息":False, "企微-获取群成员列表":False, "企微-发送消息":False, "企微-数据看板":False, "企微-创建标签":False, "企微-添加用户到标签":False, "BI/ETL创建数据源知识库":False, "BI/ETL数据源列表":False, "BI/ETL向量化数据源表":False, "BI/ETL向量化数据集列表":False, "BI聊天":True, "ETL聊天":False, "提取关键词":False, "生成任务":False, "生成会议纪要":True } # 业务接口指定状态 business_tate = { "助理初始化": True, "AI助理—基础信息": False, "AI助理—更新基础规则": False, "AI助理—设置高级技能": False, "AI助理—查看可用技能列表(available)": False, "AI助理—技能列表": False, "流式聊天(妙语Gpt3.5)": False, "同步聊天(妙语Gpt3.5)": False, "新增知识库-文件": False, "向量化切片": True, "向量化保存切片": False, "查询数据集列表": False, "外链聊天-流式": False, "外链聊天-同步": False, "薪友聊天-异步": False, "命中测试": True } # 企微接口指定状态 chat_tate = { "助理初始化": True, "获取账号会话列表": False, "获取标签列表": False, "获取账号设置": True, "获取群成员列表": False, "获取消息会话消息": False, "发送消息": True, "数据看板": False, "创建标签": False, "添加用户到标签": False } # 智慧速记接口状态 batch_tate = { "提取关键词": True, "生成任务": True, "生成会议纪要": True } def file_processing(dictname,filepath): # 1.读取XML文档 tree = ET.parse(filepath) root = tree.getroot() # 处理初始化开关 allelement = root.find('.//IfController') try: allelement.set('enabled', str(dictname.get("助理初始化")).lower()) print("初始化:",allelement.attrib) except AttributeError as e: print("异常捕获:",e) except: print("执行异常未知") allent = root.findall('hashTree/hashTree/hashTree/HTTPSamplerProxy') for ele in allent: print("遍历元素,当前元素的属性字典:", ele.attrib) # 当前元素字典没有testname时,显示None print("遍历元素,当前元素的指定属性值:", ele.get("testname")) if dictname.get(ele.get("testname")): ele.set('enabled', 'true') elif dictname.get(ele.get("testname")) == False: ele.set('enabled', 'false') else: print("可能py脚本没更新或错误,None值:", dictname.get(ele.get("testname"))) tree.write(filepath, encoding="utf-8") def main(): if os.path.exists(sys.argv[1]): print("参数路径:",sys.argv[1]) if "单接口测试计划" in sys.argv[1]: file_processing(specified_state,sys.argv[1]) elif "业务测试计划" in sys.argv[1]: file_processing(business_tate,sys.argv[1]) elif "企微测试计划" in sys.argv[1]: file_processing(chat_tate,sys.argv[1]) elif "智慧速记" in sys.argv[1]: file_processing(batch_tate,sys.argv[1]) else: print("路径不对或脚本不存在:{}".format(sys.argv[1])) if __name__ == '__main__': main()
备注:部分参数需要结合自己业务填写
2.shell脚本
#!/bin/bash TIMESTAMP=$(date +"%Y%m%d_%H%M%S") echo -e "\033[31m >>>脚本枚举(单场景接口1、业务接口2、企微接口3、智慧速记4) \033[0m" read -p "请输入脚本类型:" scripttype read -p "请输入线程数量:" threadsnum read -p "请输入循环次数: " cycles if [[ $threadsnum =~ ^[0-9]+$ ]] && [[ $cycles =~ ^(-1|[0-9]+)$ ]]; then if [ $scripttype == 1 ] ;then filepath="./jmxscript/Pet单接口测试计划.jmx" elif [ $scripttype == 2 ];then filepath="./jmxscript/Pet业务测试计划.jmx" elif [ $scripttype == 3 ];then filepath="./jmxscript/Pet企微测试计划.jmx" elif [ $scripttype == 4 ];then filepath="./jmxscript/Pet智慧速记测试计划.jmx" else echo "没有符合的条件" exit 1 fi echo "开始处理..............." python3 ./jmx_handle.py $filepath #执行生成报告 /usr/local/codes/apache-jmeter-5.6.2/bin/jmeter.sh -n -t $filepath -Jconcurrent_number=$threadsnum -Jcycles=$cycles -l /tmp/wrk/log/results_log${TIMESTAMP}.jtl -e -o "/tmp/wrk/report/${TIMESTAMP}" if [ -d "./report/${TIMESTAMP}" ] && [ -n "$(ls -A "./report/${TIMESTAMP}")" ]; then zip -r ./report/${TIMESTAMP}.zip ./report/${TIMESTAMP} else echo -e "\033[31m >>>>>>>目录不存在或目录为空,报告生成失败!>>>>>>\033[0m" fi else echo "输入的不是正整数,程序退出!" fi
相关链接:
https://blog.csdn.net/zuozewei/article/details/118406079 ......................JMeter 使用 shell 脚本快速执行