python运行jmx文件

使用jmeter无GUI运行jmx命令时,每次都需要手写文件jmx中的参数,生成的jtl文件名称,result文件名称

 1 yangfengjuan@office1:~/example$ jmeter -n -t javaRequest5.3-1-con-yiyi.jmx -l javaRequest5.3-1-con-yiyi-120s-5qps.jtl -e -o result-120s-5qps/
 2 SLF4J: Class path contains multiple SLF4J bindings.
 3 SLF4J: Found binding in [jar:file:/home/yangfengjuan/apache-jmeter-5.3/lib/log4j-slf4j-impl-2.13.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
 4 SLF4J: Found binding in [jar:file:/home/yangfengjuan/apache-jmeter-5.3/lib/ext/pepper-box-2.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
 5 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
 6 SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
 7 Creating summariser <summary>
 8 Created the tree successfully using javaRequest5.3-1-con-yiyi.jmx
 9 Starting standalone test @ Sun Feb 28 15:50:48 CST 2021 (1614498648720)
10 Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
11 summary +     18 in 00:00:11 =    1.6/s Avg:    29 Min:     0 Max:   133 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
12 summary +    151 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
13 summary =    169 in 00:00:41 =    4.1/s Avg:     3 Min:     0 Max:   133 Err:     0 (0.00%)
14 summary +    150 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
15 summary =    319 in 00:01:11 =    4.5/s Avg:     1 Min:     0 Max:   133 Err:     0 (0.00%)
16 summary +    150 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
17 summary =    469 in 00:01:41 =    4.6/s Avg:     1 Min:     0 Max:   133 Err:     0 (0.00%)
18 summary +     95 in 00:00:19 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 0 Started: 5 Finished: 5
19 summary =    564 in 00:02:00 =    4.7/s Avg:     1 Min:     0 Max:   133 Err:     0 (0.00%)
20 Tidying up ...    @ Sun Feb 28 15:52:49 CST 2021 (1614498769206)
21 ... end of run

 

下面代码使用参数动态执行文件,生成动态的文件

文件结构如下:

 1 yangfengjuan@office1:~/example$ ls -ll
 2 total 96
 3 -rw-r--r--  1 yangfengjuan yangfengjuan 13449 2月  28 14:56 javaRequest5.3-1-con-yiyi.jmx
 4 -rw-rw-r--  1 yangfengjuan yangfengjuan 13449 2月  28 14:56 javaRequest5.3-1-con-yiyi-P.jmx 7 -rw-r--r--  1 yangfengjuan yangfengjuan   280 2月  28 13:34 message.txt
 8 drwxrwxr-x 10 yangfengjuan yangfengjuan  4096 2月  28 14:58 result10 -rw-r--r--  1 yangfengjuan yangfengjuan  3426 2月  28 14:47 test.py

 生成的文件在result中:

 1 yangfengjuan@office1:~/example$ cd result/
 2 yangfengjuan@office1:~/example/result$ ls -ll
 3 total 264
 4 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:08 20210228140801-10s-120qps-2threads-1loops-htmlreport
 5 -rw-rw-r-- 1 yangfengjuan yangfengjuan  1607 2月  28 14:08 20210228140801-10s-120qps-2threads-1loops.jtl
 6 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:11 20210228140920-120s-300qps-5threads-1loops-htmlreport
 7 -rw-rw-r-- 1 yangfengjuan yangfengjuan 43373 2月  28 14:11 20210228140920-120s-300qps-5threads-1loops.jtl
 8 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:19 20210228141737-120s-5.0qps-5threads-1loops-htmlreport
 9 -rw-rw-r-- 1 yangfengjuan yangfengjuan  1181 2月  28 14:19 20210228141737-120s-5.0qps-5threads-1loops.jtl
10 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:21 20210228141952-120s-5.0qps-5threads-1loops-htmlreport
11 -rw-rw-r-- 1 yangfengjuan yangfengjuan  1181 2月  28 14:21 20210228141952-120s-5.0qps-5threads-1loops.jtl
12 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:26 20210228142358-120s-300qps-5threads-1loops-htmlreport
13 -rw-rw-r-- 1 yangfengjuan yangfengjuan 43444 2月  28 14:26 20210228142358-120s-300qps-5threads-1loops.jtl
14 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:30 20210228142818-120s-300qps-5threads-1loops-htmlreport
15 -rw-rw-r-- 1 yangfengjuan yangfengjuan 43372 2月  28 14:30 20210228142818-120s-300qps-5threads-1loops.jtl
16 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:49 20210228144707-120s-300qps-5threads-1loops-htmlreport
17 -rw-rw-r-- 1 yangfengjuan yangfengjuan 43445 2月  28 14:49 20210228144707-120s-300qps-5threads-1loops.jtl
18 drwxrwxr-x 4 yangfengjuan yangfengjuan  4096 2月  28 14:58 20210228145609-120s-300qps-5threads-1loops-htmlreport
19 -rw-rw-r-- 1 yangfengjuan yangfengjuan 41428 2月  28 14:58 20210228145609-120s-300qps-5threads-1loops.jtl

 test.py代码:

 1 # Python 调 Jmeter 执行参数化 jmx 脚本
 2 # coding=utf-8
 3 import os
 4 from os.path import join
 5 import time
 6 import re
 7 from string import Template
 8 
 9 jmeter_Home = f"/home/yangfengjuan/apache-jmeter-5.3/bin/jmeter"
10 
11 # jmx文件路径
12 currpath = os.path.dirname(os.path.realpath(__file__))
13 # 要运行的jmx脚本
14 jmx = currpath + f"/javaRequest5.3-1-con-yiyi.jmx"
15 
16 # 生成的报告放到result文件夹中
17 resultpath = currpath + os.sep + "result"
18 
19 # 创建参数化jmx文件
20 # 参数化的内容为:循环次数 loops、线程数 num_threads、持续运行时间 duration
21 def create_para_jmx():
22     global replaced_jmx
23     jmx_str = ''
24     with open(jmx, 'r', encoding='utf-8') as file:
25         jmx_str = file.read()
26 
27     # 循环次数
28     loops = '<stringProp name="LoopController.loops">(.*?)</stringProp>'
29     replcae_loops = '<stringProp name="LoopController.loops">$loops</stringProp>'
30     jmx_str = re.sub(loops, replcae_loops, jmx_str)
31 
32     # 线程数
33     num_threads = '<stringProp name="ThreadGroup.num_threads">(.*?)</stringProp>'
34     repalce_num_threads = '<stringProp name="ThreadGroup.num_threads">$num_threads</stringProp>'
35     jmx_str = re.sub(num_threads, repalce_num_threads, jmx_str)
36 
37     # 持续运行时间
38     duration = '<stringProp name="ThreadGroup.duration">(.*?)</stringProp>'
39     replace_duration = '<stringProp name="ThreadGroup.duration">$duration</stringProp>'
40     jmx_str = re.sub(duration, replace_duration, jmx_str)
41 
42     # qps
43     throughput = '<value>(.*?)</value>'
44     replace_throughput = '<value>$throughput</value>'
45     jmx_str = re.sub(throughput, replace_throughput, jmx_str)
46 
47     replaced_jmx = jmx.replace('.jmx', '-P.jmx')
48 
49     with open(replaced_jmx, "w+", encoding="utf-8") as file:
50         file.writelines(jmx_str)
51 
52 # 执行参数化jmx文件,生成带参数的jmx文件
53 create_para_jmx()
54 
55 61 # 获取当前时间,格式为20210301122059
63 now = time.strftime(r'%Y%m%d%H%M%S', time.localtime(time.time()))
64 
65 # 创建执行jmx的参数
66 def execjmx(duration, throughput, num_threads, loops):
67     print(f"本次运行的场景为:运行时间:{duration}s、吞吐量:{throughput/60}qps、线程数:{num_threads}、循环次数:{loops}")
68     tmpstr = ''
69     with open(replaced_jmx, "r", encoding="utf-8") as file:
70         tmpstr = Template(file.read()).safe_substitute(loops = loops, num_threads = num_threads, duration = duration, throughput = throughput)
71 
72     with open(replaced_jmx, "w+", encoding="utf-8") as file:
73         file.writelines(tmpstr)
74 
75     # 生成的jtl文件
76     jtl = resultpath + f'/{now}-{duration}s-{throughput}qps-{num_threads}threads-{loops}loops.jtl'
77     # 生成的html文件
78     html = resultpath + f'/{now}-{duration}s-{throughput}qps-{num_threads}threads-{loops}loops-htmlreport'
79 
80     # Terminal = f"open -a Terminal.app"
81     run = f"{jmeter_Home} -n -t {replaced_jmx} -l {jtl} -e -o {html}"
82     os.system(run)
83 
84 # 执行jmx文件
85 # 时间、吞吐量、线程数、循环次数
86 
87 duration = ''
88 throughput = ''
89 num_threads = ''
90 loops = ''
91 
92 duration = int(input("运行时间:" + duration))
93 throughput = int(input("吞吐量:" + throughput))
94 num_threads = int(input("线程数:" + num_threads))
95 loops = int(input("循环次数:" + loops))
96 execjmx(duration, throughput, num_threads, loops)

 

运行test.py,执行结果:

 1 yangfengjuan@office1:~/example$ python3 test.py
 2 运行时间:120
 3 吞吐量:300
 4 线程数:5
 5 循环次数:1
 6 本次运行的场景为:运行时间:120s、吞吐量:5.0qps、线程数:5、循环次数:1
 7 SLF4J: Class path contains multiple SLF4J bindings.
 8 SLF4J: Found binding in [jar:file:/home/yangfengjuan/apache-jmeter-5.3/lib/log4j-slf4j-impl-2.13.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
 9 SLF4J: Found binding in [jar:file:/home/yangfengjuan/apache-jmeter-5.3/lib/ext/pepper-box-2.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
10 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
11 SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
12 Creating summariser <summary>
13 Created the tree successfully using /home/yangfengjuan/example/javaRequest5.3-1-con-yiyi-P.jmx
14 Starting standalone test @ Sun Feb 28 14:56:17 CST 2021 (1614495377291)
15 Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
16 summary +     34 in 00:00:13 =    2.7/s Avg:    16 Min:     0 Max:   127 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
17 summary +    150 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
18 summary =    184 in 00:00:43 =    4.3/s Avg:     3 Min:     0 Max:   127 Err:     0 (0.00%)
19 summary +    150 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
20 summary =    334 in 00:01:13 =    4.6/s Avg:     1 Min:     0 Max:   127 Err:     0 (0.00%)
21 summary +    150 in 00:00:30 =    5.0/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
22 summary =    484 in 00:01:43 =    4.7/s Avg:     1 Min:     0 Max:   127 Err:     0 (0.00%)
23 summary +     89 in 00:00:17 =    5.1/s Avg:     0 Min:     0 Max:     1 Err:     0 (0.00%) Active: 0 Started: 5 Finished: 5
24 summary =    573 in 00:02:00 =    4.8/s Avg:     1 Min:     0 Max:   127 Err:     0 (0.00%)
25 Tidying up ...    @ Sun Feb 28 14:58:17 CST 2021 (1614495497828)
26 ... end of run

 

posted @ 2021-02-28 15:58  琥珀主yang  阅读(866)  评论(0编辑  收藏  举报