最近在技术群里面讨论到使用python操作jmeter的话题
有人提到了subprocess启动命令行来操作jmeter,也有人提到了jmeterapi,另外也有人提到了pymeter
除了这些,实际上我之前也基于docker+subprocess来操作jmeter的项目
jmeterapi这个库年久失修,就不想搞它了,下面跟我一起试试pymeter吧
直接参考项目的说明即可
使用方法比较简单,设置GUI类似,只是前后顺序不相同:
python# 省略 import
class TestTestPlanClass(TestCase):
def test_1(self: Any) -> None:
# 创建一个JsonExtractor对象,参数分别是"variable"和"args.var"
json_extractor: JsonExtractor = JsonExtractor("variable", "args.var")
# 创建一个均匀随机计时器,时间间隔在1000至2000毫秒之间
timer: UniformRandomTimer = UniformRandomTimer(1000, 2000)
ra: ResponseAssertion = ResponseAssertion.contains_substrings("var")
# 创建一个HttpSampler对象,用于发送HTTP请求并记录响应
http_sampler: HttpSampler = HttpSampler(
"Echo",
"http://119.91.147.215:18080/get?var=${__Random(0,10)}",
timer,
json_extractor,
ra,
)
# 创建一个DummySampler对象,仅用于模拟一些操作,不实际执行任何网络请求
dummy_sampler: DummySampler = DummySampler("dummy ${variable}", "hi dummy")
# 创建一个线程组,其中包含10个线程,每个线程的运行时间为1秒,整个线程组的持续时间为60秒
tg: ThreadGroupWithRampUpAndHold = ThreadGroupWithRampUpAndHold(
100, 3, 60, http_sampler, dummy_sampler, name="Some Name"
)
# 创建一个HTML报告生成器
html_reporter: HtmlReporter = HtmlReporter()
# 创建一个测试计划,其中包含tg线程组和html_reporter报告生成器
tp: TestPlan = TestPlan(tg, html_reporter)
# 运行测试计划,返回统计数据
stats = tp.run()
# 打印测试的统计數據
print(
f"duration= {stats.duration_milliseconds}",
f"mean= {stats.sample_time_mean_milliseconds}",
f"min= {stats.sample_time_min_milliseconds}",
f"median= {stats.sample_time_median_milliseconds}",
f"90p= {stats.sample_time_90_percentile_milliseconds}",
f"95p= {stats.sample_time_95_percentile_milliseconds}",
f"99p= {stats.sample_time_99_percentile_milliseconds}",
f"max= {stats.sample_time_max_milliseconds}",
sep="\t",
)
# 断言99%分位数的样本时间小于2000毫秒
self.assertLess(stats.sample_time_99_percentile_milliseconds, 30)
如果你使用的PyCharm,会自动识别
shell# rikasai @ huacai-mbp in ~/code/python/pymeter-demo [22:23:38] C:1
$ python main.py
+ 579 in 00:00:07 = 86.2/s Avg: 139 Min: 9 Max: 500 Err: 0 (0.00%) Active: 100 Started: 100 Finished: 0
+ 3964 in 00:00:30 = 131.9/s Avg: 142 Min: 8 Max: 500 Err: 0 (0.00%) Active: 100 Started: 100 Finished: 0
= 4543 in 00:00:37 = 123.6/s Avg: 142 Min: 8 Max: 500 Err: 0 (0.00%)
duration= 64265 mean= 144 min= 8 median= 54 90p= 404 95p= 451 99p= 491 max= 500
F + 7 in 00:00:03 = 2.1/s Avg: 864 Min: 225 Max: 977 Err: 0 (0.00%) Active: 6 Started: 6 Finished: 0
duration= 14276 mean= 323 min= 223 median= 230 90p= 960 95p= 973 99p= 1012 max= 1207
.
======================================================================
FAIL: test_1 (__main__.TestTestPlanClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/rikasai/code/python/pymeter-demo/main.py", line 67, in test_1
self.assertLess(stats.sample_time_99_percentile_milliseconds, 30)
AssertionError: 491 not less than 30
----------------------------------------------------------------------
Ran 2 tests in 79.631s
FAILED (failures=1)
(pymeter-demo)
需要注意的是,这里断言失败,是unittest断言失败,这个数据并不会在jmeter报告中体现
报告会自动生成在output文件夹下面,一个test函数对应一份报告
使用浏览器打开index.html就能看到完整的报告
本文作者:花菜
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!