使用Python调用JMeter压测并生成报告
创始人
2024-11-18 08:01:50
0次

使用Python调用JMeter压测并生成报告

Python中可以使用os.system()或supprocess.Popen()调用系统命令,前者实时显示在屏幕上,后者可以获取到屏幕输出信息。
使用Python调用JMeter运行及生成报告的命令如下。

import subprocess

jmx_file = 'testplan.jmx' # jmx文件路径
result_file = 'result.jtl' # 
log_file = 'run.log'
report_dir = 'report'

run_cmd = f'jmeter -n -t {jmx_file} -l {result_file} -j {log_file}' # 无界面运行JMeter压测命令
report_cmd = f'jmeter -g {result_file} -o {report_dir}' # 生成HTML报告命令

# 不需要获取屏幕输出是,可以使用os.system()
# os.system(run_cmd)
# os.system(report_cmd)

# 需要获取屏幕输出是,可以使用subprocess.Popen()
p1 = subprocess.Popen(run_cmd, shell=True, stdout=subprocess.PIPE)
print(p1.stdout.read().decode('utf-8'))
p2 = subprocess.Popen(report_cmd, shell=True, stdout=subprocess.PIPE)
print(p2.stdout.read().decode('utf-8'))

组装jmx

每一测试计划为一个jmx文件,jmx实际上是xml格式的,包含一些JMeter自定义的格式规范。
常用的组件有:

  • : 测试计划
  • : 线程组
  • : CSV数据文件
  • : HTTP请求
  • : HTTP请求头管理器
  • : Cookies管理器
  • : DNS缓存管理器
  • : 监听器(包括查看结果树、聚合报告等)
  • : 响应断言
  • : 三方Dubbo请求插件
     

Dubbo插件jmeter-plugins-dubbo下载链接

jmx中,如果一个组件有子组件,格式为


   ...线程组配置


   ...内部子组件

···
如果不包含子组件,则后面接一个 单标签直接结束,例如:
```xml

  ... CSV数据组件配置

详细的格式可以自己使用JMeter创建一个测试计划,使用文本文件打开jmx文件查看。

使用Python组装jmx文件的方式有两种,一种是固定模板的数据渲染,一种类似JMeter的逐个组件添加,第一种比较简单。
通过分析jmx文件中的变量,我们使用jinja2模板语法,将其中的变量进行参数化,对需要判断和循环的变量设置if和for循环。

Jinja2中文文档

假设我们的测试计划结构为:

测试计划
  DNS缓存管理器
  Cookies管理器
  CSV文件(多个)
  ...
  聚合报告
  线程组(多个)
    CSV文件(多个)
    HTTP请求(或Dubbo请求)
      HTTP请求头管理器
      CSV文件(多个)
      响应断言
      察看结果树

将jmx中的关键数据抽取并组合,我使用的完整数据格式如下:

data.yaml

test_plan_name: 测试计划
comments: 测试计划描述
hosts:
 - name: las.secoo.com
  address: 112.126.120.128
cookies:
 clear_each_iteration: 'true'
csv_files:
 - {'name': '数据文件1', 'path': 'data.csv', 'varnames': 'a,b', 'delimiter': ','}
 - {'name': '数据文件2', 'path': 'data.csv', 'varnames': 'c,d', 'delimiter': ','}
thread_groups:
 - thread_group_name: 线程组1
  comments: 线程组1描述
  enabled: 'true'
  num_threads: 50
  loops: -1
  ramp_time: 0
  scheduler: 'true'
  duration: 30
  delay: ''
  http_samples:
   - request_name: HTTP-GET
    enabled: 'true'
    csv_files:
     - {'name': '数据文件4', 'path': 'data.csv', 'varnames': 'a,b', 'delimiter': ','}
    request:
     protocol: https
     domain: httpbin.org
     port: ''
     encoding: ''
     path: /get
     method: GET
     connect_timeout: ''
     response_timeout: ''
     params: {'a': 1, 'b': 2}
     headers: {'token': 'aaaaaa', 'x-text': 'bbbbb'}
     follow_redirects: 'false'
     auto_redirects: 'false'
     use_keepalive: 'false'
    validate:
     - test_field: response_data # response_data响应文本 response_code响应代码response_message响应信息response_headers
      # 请求头request_headers sample_label URL样本 response_data_as_document文档(文本) 请求数据request_data
      test_type: 2 # 2 包括 1匹配 8 相等 16字符串 否+4 或者+32
      strings: ['a', 'b']
   - request_name: HTTP-POST
    enabled: 'true'
    request:
     protocol: https
     domain: httpbin.org
     port: ''
     encoding: ''
     path: /post
     method: POST
     data: {'c': 3, 'd': 4}
     follow_redirects: 'false'
     auto_redirects: 'false'
     use_keepalive: 'false'
     connect_timeout: ''
     response_timeout: ''
   - request_name: HTTP-JSON
    enabled: 'true'
    request:
     protocol: https
     domain: httpbin.org
     port: ''
     encoding: ''
     path: /post
     method: POST
     connect_timeout: ''
     response_timeout: ''
     raw_data: '{"e": 5, "f": 6}'
     follow_redirects: 'false'
     auto_redirects: 'false'
     use_keepalive: 'false'
 - thread_group_name: 线程组2
  comments: 线程组2描述
  enabled: 'false'
  num_threads: 50
  loops: -1
  ramp_time: 0
  scheduler: 'true'
  duration: 30
  delay: ''
  csv_files:
   - {'name': '数据文件3', 'path': 'data.csv', 'varnames': 'a,b','delimiter': '\t'}
  dubbo_samples:
   - request_name: 查询运费接口-dubbo
    enabled: 'true'
    registry:
     type: zookeeper
     group: ''
     address: 'zk-mall1.secoolocal.com:5181?backup=zk-mall2.secoolocal.com:5181,zk-mall3.secoolocal.com:5181'
    dubbo:
     timeout: 100
     retires: 0
     group: ''
     connections: 100
     load_balance: random
     cluster: failfast
     service: 'com.secoo.business.config.rpc.service.BusinessConfigStoreService'
     method: queryFreight
     headers:
      Content-Type: 'application/json'
     params:
      - type: java.util.List
       value: ${freight_wareHouseId_sendAreaId}
      - type: java.lang.String
       value: 110100
      - type: java.util.List
       value: ${freight_wareHouseId_sendAreaId}
    validate:
     - test_field: response_data # response_data响应文本 response_code响应代码response_message响应信息response_headers
      test_type: 16 # 2 包括 1匹配 8 相等 16字符串 否+4 或者+32
      strings: ['"code": 0']

对应的模板文件tpl.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>

 
  
   {{comments}}
   false
   true
   false
   
    
   
   
  
  {% if hosts %}
   
    
    {% for host in hosts %}
     
      {{host.name}}
      {{host.address}}
     {% endfor %}
    
    false
    true
   
   {% endif %} {% if cookies %}
   
    
    {{cookies.clear_each_iteration}}
   
   {% endif %} {% if csv_files %}{% for csv_file in csv_files %}
   
    dat/{{csv_file.path}}
    UTF-8
    {{csv_file.varnames}}
    true
    {{csv_file.delimiter}}
    false
    true
    false
    shareMode.group
   
   {% endfor %}{% endif %}
   
    false
    
     saveConfig
     
      
      true
      true
      true
      
      true
      true
      true
      true
      false
      true
      true
      false
      false
      false
      true
      false
      false
      true
      true
      0
      true
      true
      true
      true
     
    
    
   
   {% for thread_group in thread_groups %}
   
    {{thread_group.comments}}
    continue
    
     false
     {{thread_group.loops}}
    
    {{thread_group.num_threads}}
    {{thread_group.ramp_time}}
    {{thread_group.scheduler}}
    {{thread_group.duration}}
    {{thread_group.delay}}
    false
   
   {% if thread_group.csv_files %}{% for csv_file in thread_group.csv_files %}
    
    dat/{{csv_file.path}}
    UTF-8
    {{csv_file.varnames}}
    true
    {{csv_file.delimiter}}
    false
    true
    false
    shareMode.group
   
    {% endfor %}{% endif %} {% if thread_group.http_samples %}{% for http_sample in thread_group.http_samples %}
    
     
      {% if http_sample.request.params %}{% for name, value in http_sample.request.params.items() %}
       
        false
        {{value}}
        =
        true
        {{name}}
       {% endfor %}{% endif %} {% if http_sample.request.data %}{% for name, value in http_sample.request.data.items() %}
       
        false
        {{value}}
        =
        true
        {{name}}
       {% endfor %}{% endif %} {% if http_sample.request.raw_data %}
       
        false
        {{http_sample.request.raw_data}}
        =
       {% endif %}
      
     
     {{http_sample.request.domain}}
     {{http_sample.request.port}}
     {{http_sample.request.protocol}}
     {{http_sample.request.encoding}}
     {{http_sample.request.path}}
     {{http_sample.request.method}}
     {{http_sample.request.follow_redirects}}
     {{http_sample.request.auto_redirects}}
     {{http_sample.request.use_keepalive}}
     false
     
     {{http_sample.request.connect_timeout}}
     {{http_sample.request.response_timeout}}
    
    {% if http_sample.request.headers %}
     
      {% for name, value in http_sample.request.headers.items() %}
       
        {{name}}
        {{value}}
       {% endfor %}
      
     
     {% endif %} {% if http_sample.csv_files %}{% for csv_file in http_sample.csv_files %}
     
      {{csv_file.delimiter}}
      UTF_8
      dat/{{csv_file.path}}
      true
      false
      true
      shareMode.group
      false
      {{csv_file.varnames}}
     
     {% endfor %}{% endif %} {% if http_sample.validate %}{% for assertion in http_sample.validate %}
     
      {% if assertion.strings %}{% for string in assertion.strings %}
       {{string}}{% endfor %}{% endif %}
      
      
      Assertion.{{assertion.test_field}}
      false
      {{assertion.test_type}}
     
     {% endfor %}{% endif %}
     
      false
      
       saveConfig
       
        
        true
        true
        true
        
        true
        true
        true
        true
        false
        true
        true
        false
        false
        false
        true
        false
        false
        false
        true
        0
        true
        true
        true
        true
        true
        true
       
      
      
     
     
    {% endfor %}{% endif %} {% if thread_group.dubbo_samples %} {% for dubbo_sample in thread_group.dubbo_samples %}
    
     {{dubbo_sample.registry.type}}
     {{dubbo_sample.registry.group}}
     dubbo://
     {{dubbo_sample.registry.address}}
     {{dubbo_sample.dubbo.timeout}}
     
     {{dubbo_sample.dubbo.retries}}
     {{dubbo_sample.dubbo.group}}
     {{dubbo_sample.dubbo.connections}}
     {{dubbo_sample.dubbo.load_balance}}
     sync
     {{dubbo_sample.dubbo.cluster}}
     {{dubbo_sample.dubbo.service}}
     {{dubbo_sample.dubbo.method}}
     1{% for param in dubbo_sample.dubbo.params %}
      {{param.type}}
      {{param.value}}{% endfor %}
     0
     
     
     
     
     
     
     
     
     
     
    
    {% if dubbo_sample.dubbo.headers %}
     
      {% for name, value in dubbo_sample.dubbo.headers.items() %}
       
        {{name}}
        {{value}}
       {% endfor %}
      
     
     {% endif %} {% if dubbo_sample.validate %} {% for assertion in dubbo_sample.validate %}
     
      {% if assertion.strings %}{% for string in assertion.strings %}
       {{string}}{% endfor %}{% endif %}
      
      
      Assertion.{{assertion.test_field}}
      false
      {{assertion.test_type}}
     {% endfor %} {% endif %}
     {% endfor %}{% endif %} {% endfor %}
   
  
 

组装出类似data.yaml格式的数据,并使用jinja2渲染模板即可得到完整的jmx文件

pip install pyyaml jinja2

import yaml
import jinja2

# 组装或读取数据
with open('data.yaml', encoding='utf-8') as f:
  data = yaml.safe_load(f)

 # 读取模板
with open('tpl.xml', encoding='utf-8') as f:
  tpl = f.read()

# 渲染模板生成jmx
jmx = jinja2.Template(tpl).render(data)
with open(jmx_file, 'w', encoding='utf-8') as f:
  f.write(jmx)

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...