51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2092|回复: 2

服务器web性能测试之Locust

[复制链接]

该用户从未签到

发表于 2019-2-11 16:07:13 | 显示全部楼层 |阅读模式
本帖最后由 巴黎的灯光下 于 2019-2-11 16:09 编辑

简介
Locust 是一个开源负载测试工具。使用 Python 代码定义用户行为,也可以仿真百万个用户。
Locust 是非常简单易用,分布式,用户负载测试工具。Locust主要为网站或者其他系统进行负载测试,能测试出一个系统可以并发处理多少用户。
Locust 是完全基于时间的,因此单个机器支持几千个并发用户。相比其他许多事件驱动的应用,Locust 不使用回调,而是使用对协程支持比较完善的gevent(基于IO切换)。

特点
  • 完全使用纯Python代码编写用户测试场景,不需要任何配置文件。
  • 分布式&可伸缩 - 支持成千上万的用户
  • 基于webui(自带)
  • 可以测试任意系统

安装
  1. pip install locustio

  2. 或者

  3. easy_install locustio
复制代码
检查是否安装成功;可以通过“locust –help”来检测。

文档说明

官方文档:http://docs.locust.io/en/latest/installation.html


简单示例

test.py

  1. from locust import web, HttpLocust, TaskSet, events,task
  2. import random, traceback

  3. # 增加新的web界面
  4. @web.app.route("/hi")
  5. def my_added_page():
  6.         return "hello locust..."


  7. # 钩子函数
  8. def on_request_success(request_type, name, response_time, response_length):
  9.     print 'Type: %s, Name: %s, Time: %fms, Response Length: %d' % \
  10.             (request_type, name, response_time, response_length)

  11. def on_request_failure(request_type, name, response_time, exception):
  12.     print 'Type: %s, Name: %s, Time: %fms, Reason: %r' % \
  13.             (request_type, name, response_time, exception)

  14. events.request_success += on_request_success
  15. events.request_failure += on_request_failure

  16. class UserBehavior(TaskSet):
  17.     def on_start(self):
  18.         """ on_start is called when a Locust start before any task is scheduled """
  19.         self.login()

  20.     def login(self):
  21.         with self.client.post("/login", {"username":"ellen_key", "password":"education"}, catch_response = True) as r:
  22.         if random.choice([0, 1]):
  23.                 r.success()
  24.         else:
  25.             r.failure('0')

  26.     @task(2)
  27.     def index(self):
  28.         self.client.get("/")

  29.     @task(1)
  30.     def profile(self):
  31.         self.client.get("/profile")

  32. class WebsiteUser(HttpLocust):
  33.     # 指向一个TaskSet类,TaskSet类定义了每个用户的行为
  34.     task_set = UserBehavior
  35.     # 请求等待最小时间min_wait和请求等待最大时间max_wait
  36.     min_wait = 5000
  37.     max_wait = 9000
  38.     host = http://127.0.0.1
复制代码


单机

终端输入:

  1. locust -f test.py
复制代码

webui访问地址:localhost:8089

ui界面需要填写两个参数:

number of users to simulate:模拟用户的数量

Hatch rate (users spawned/second):表示产生模拟用户的速度,每秒启动多少个用户


分布式

master机终端输入:

  1. locust -f test.py --master --master-port=8888(默认的是8080端口)
复制代码

salve机终端输入:

  1. locust -f test.py --slave --master-host=<master-IP> --master-bind-host=8888
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-28 17:40 , Processed in 0.073427 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表