Python性能测试工具之Locust的用法

发表于:2021-3-09 09:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Garden_M    来源:博客园

#
Locust
分享:
  背景
  在一次机缘巧合下,公司刚好需要对一个服务进行性能测试,刚好也想用locust做一次性能测试,因此也有了这次的locust新手使用分享。
  由于locust的团队一直在维护着locust,因此locust一直会有迭代升级。本文是基于locust-1.4.3版本,如果最新版本和本文的操作流程有所不同,请以官方最新版本为准。
  这里奉上locust官方文档 https://docs.locust.io/en/stable/
  Locust
  介绍
  locust,单词意思--蝗虫,当蝗虫来袭是有多可怕,想必大家都能想象到了。
  locust就像它的名字一样,当启动它的时候,它就能像蝗虫般向请求对象发起“进攻”。
  下载
  pip install locust
  基本用法(1.0 以后的版本)
  1、假设有一个接口/test需要进行性能测试,代码如下:
  # test_pf.py
  from locust import HttpUser, task, between
  class TestTask(HttpUser):
      wait_time = between(0, 0.1)
      host = 'http://localhost.com'  # 参数可以不在程序中定义,可以在locust的Web页面输入
      @task  # task装饰器是locust识别蝗虫任务的标记,同时也可以作为任务和任务之间的权重设置
      def query_distribution(self):
          header = {"Content-Type": "application/json"}
          param = {"param":123}
          url = '/test'
          with self.client.post(url=url, headers=header, json=param, catch_response=True, timeout=10) as response:  # catch_response参数是否在Web页面展示response的日志
              if response.status_code == 200:
                response.success()
             else:
                response.failure(response)
  2、启动locust脚本:
  locust -f test_pf.py
  如果有下面的信息,则表示启动成功:
  >>> locust -f pf_distribution.py 
  local/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
  local/INFO/locust.main: Starting Locust 1.4.3
  注意:locust默认启动端口为8089,启动前需要检查8089端口是否被占用
  3、打开 http://0.0.0.0:8089 展示的是locust提供的UI页面,也就是设置并发数等参数的页面,同时也是观察系统性能数据的页面。
  4、打开页面后,就可以开心的对系统进行“攻击”了。
  这里顺便介绍一下1.0以前的版本脚本写法。
  一些旧一点的版本,locust需要定义两个类才能启动,定义任务的类以及启动locust的类,而在新的版本中被官方合并,这里只是做一个普及,不做深入介绍。
  from locust import HttpLocust, TaskSet, between, task
  class TestTask(TaskSet):
      @task  # task装饰器是locust识别蝗虫任务的标记,同时也可以作为任务和任务之间的权重设置
      def query_distribution(self):
          header = {"Content-Type": "application/json"}
          param = {"param":123}
          url = '/test'
          with self.client.post(url=url, headers=header, json=param, catch_response=True, timeout=10) as response:  # catch_response参数是否在Web页面展示response的日志
              if response.status_code == 200:
                response.success()
             else:
                response.failure(response)
  class Test(HttpLocust):
      task_set = YieldSet
      host = ''   # 参数可以不在程序中定义,可以在locust的Web页面输入
      wait_time = between(0, 0.01)
  高级用法
  这里主要介绍的是locust的分布式启动方式,也就是locust 的master-slave模式。
  什么locust会有一个分布式启动方式?究其原因,是locust默认使用的是单个CPU的系统资源。
  加入有一台4核的服务器,如果使用locust -f test_pf.py启动locust脚本,locust只会调用一个核的能力,也就是这台服务器的CPU使用率最高也才25%,明白了这点,也就大概知道了locust的分布式启动是干什么用的。就是彻底使用服务器的所有资源来产生最大的压力。
  locust分布式启动
  1、启动master节点
  locust -f pf_distribution.py --master
  local/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
  local/INFO/locust.main: Starting Locust 1.4.3
  2、启动slave节点
  locust -f pf_distribution.py --worker
  local/INFO/locust.main: Starting Locust 1.4.3
  3、启动slave节点后,master节点会收到slave节点的启动日志
  local/INFO/locust.runners: Client 'x-MacBook-Pro.local_1a01c69010bb48f5a79e7e802e98f10c' reported as ready. Currently 1 clients ready to swarm.
  /INFO/locust.runners: Client 'wewen-MacBook-Pro.local_935f4c8dc5c14401b31c6961460fa833' reported as ready. Currently 2 clients ready to swarm.
  当关闭master节点后,所有的slave节点都会自动关闭,反过来,关闭一个slave节点,master不会关闭,关闭所有slave节点都关闭后,master节点也不会关闭,它会继续挂起,等待slave节点启动。
  locust命令行启动的更多用法,可以使用locust -h查询。

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号