经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python轻量级性能工具-Locust
来源:cnblogs  作者:yetangjian  时间:2023/5/4 9:20:02  对本文有异议

Locust基于python的协程机制,打破了线程进程的限制,可以能够在一台测试机上跑高并发

性能测试基础

  1.快慢:衡量系统的处理效率:响应时间

  2.多少:衡量系统的处理能力:单位时间内能处理多少个事务(tps)

性能测试根据测试需求最常见的分为下面三类

  1 负载测试load testing

    不断向服务器加压,值得预定的指标或者部分系统资源达到瓶颈,目的是找到系统最大负载的能力

  2 压力测试

    通过高负载持续长时间,来验证系统是否稳定

  3 并发测试:

    同时像服务器提交请求,目的发现系统是否存在事务冲突或者锁升级的现象

性能负载模型

locust安装

安装存在问题,可以通过豆瓣源下载

  1. pip install locust

locust模板

基本上多数的场景我们都可以基于这个模板read.py去做修改

  1. from locust import HttpUser, TaskSet, task, tag, events
  2. # 启动locust时运行
  3. @events.test_start.add_listener
  4. def setup(environment, **kwargs):
  5. # print("task setup")
  6.  
  7. # 停止locust时运行
  8. @events.test_stop.add_listener
  9. def teardown(environment, **kwargs):
  10. print("task teardown")
  11. class UserBehavor(TaskSet):
  12. #虚拟用户启用task运行
  13. def on_start(self):
  14. print("start")
  15. locusts_spawned.wait()
  16. #虚拟用户结束task运行
  17. def on_stop(self):
  18. print("stop")
  19. @tag('test1')
  20. @task(2)
  21. def index(self):
  22. self.client.get('/yetangjian/p/17320268.html')
  23. @task(1)
  24. def info(self):
  25. self.client.get("/yetangjian/p/17253215.html")
  26. class WebsiteUser(HttpUser):
  27. def setup(self):
  28. print("locust setup")
  29. def teardown(self):
  30. print("locust teardown")
  31. host = "https://www.cnblogs.com"
  32. task_set = task(UserBehavor)
  33. min_wait = 3000
  34. max_wait = 5000

注:这里我们给了一个webhost,这样我们可以直接在浏览器中打开locust

 集合点lr_rendezvous

当然我们可以把集合点操作放入上述模板的setup中去运行起来

  1. locusts_spawned = Semaphore()
  2. locusts_spawned.acquire()
  3. def on_hatch_complete(**kwargs):
  4. """
  5. select_task类的钩子函数
  6. :param kwargs:
  7. :return:
  8. """
  9. locusts_spawned.release()
  10. events.spawning_complete.add_listener(on_hatch_complete)
  11. n = 0
  12. class UserBehavor(TaskSet):
  13. def login(self):
  14. global n
  15. n += 1
  16. print(f"第{n}个用户登陆")
  17. def on_start(self):
  18. self.login()
  19. locusts_spawned.wait()
  20. @task
  21. def test1(self):
  22. #catch_response获取返回
  23. with self.client.get("/yetangjian/p/17253215.html",catch_response=True):
  24. print("查询结束")
  25. class WebsiteUser(HttpUser):
  26. host = "https://www.cnblogs.com"
  27. task_set = task(UserBehavor)
  28. wait_time = between(1,3)
  29. if __name__ == '__main__':
  30. os.system('locust -f read.py --web-host="127.0.0.1"')

比较常见的用法

在上面两个例子中我们已经看到了一些,例如装饰器events.test_start.add_listenerevents.test_stop.add_listener用来在负载测试前后进行一些操作,又例如on_start、on_stop,在task执行前后运行,又例如task,可以用来分配任务的权重

 等待时间

  1. # wait between 3.0 and 10.5 seconds after each task
  2. #wait_time = between(3.0, 10.5)
  3. #固定时间等待
  4. # wait_time = constant(3)
  5. #确保每秒运行多少次
  6. constant_throughputtask_runs_per_second
  7. #确保每多少秒运行一次
  8. constant_pacing(wait_time)

同样也可以在User类下发重写wait_time来达到自定义

tag标记

  1. @tag('test1')
  2. @task(2)
  3. def index(self):
  4. self.client.get('/yetangjian/p/17320268.html')

通过对任务打标记,就可以在运行时候执行运行某一些任务:

  1. #只执行标记test1
  2. os.system('locust -f read.py --tags test1 --web-host="127.0.0.1"')
  3. #不执行标记过的
  4. os.system('locust -f read.py --exclude-tags --web-host="127.0.0.1"')
  5. #除去test1执行所有
  6. os.system('locust -f read.py --exclude-tags test1 --web-host="127.0.0.1"')

 自定义失败

  1. #定义响应时间超过0.1就为失败
  2. with self.client.get("/yetangjian/p/17253215.html", catch_response=True) as response:
  3. if response.elapsed.total_seconds() > 0.1:
  4. response.failure("Request took too long")
  5. #定义响应码是200就为失败
  6. with self.client.get("/yetangjian/p/17320268.html", catch_response=True) as response:
  7. if response.status_code == 200:
  8. response.failure("响应码200,但我定义为失败")

 自定义负载形状

自定义一个shape.py通过继承LoadTestShape并重写tick

这个形状类将以100块为单位,20速率的增加用户数,然后在10分钟后停止负载测试(从运行开始的第51秒开始user_count会round到100)

  1. from locust import LoadTestShape
  2. class MyCustomShape(LoadTestShape):
  3. time_limit = 600
  4. spawn_rate = 20
  5.  
  6. def tick(self):
  7. run_time = self.get_run_time()
  8. if run_time < self.time_limit:
  9. # User count rounded to nearest hundred.
  10. user_count = round(run_time, -2)
  11. return (user_count, self.spawn_rate)
  12. return None

运行图如下所示

通过命令行去触发

  1. os.system('locust -f read.py,shape.py --web-host="127.0.0.1"')

不同时间阶段的例子

  1. from locust import LoadTestShape
  2. class StagesShapeWithCustomUsers(LoadTestShape):
  3. stages = [
  4. {"duration": 10, "users": 10, "spawn_rate": 10},
  5. {"duration": 30, "users": 50, "spawn_rate": 10},
  6. {"duration": 60, "users": 100, "spawn_rate": 10},
  7. {"duration": 120, "users": 100, "spawn_rate": 10}]
  8. def tick(self):
  9. run_time = self.get_run_time()
  10. for stage in self.stages:
  11. if run_time < stage["duration"]:
  12. tick_data = (stage["users"], stage["spawn_rate"])
  13. return tick_data
  14. return None

 

原文链接:https://www.cnblogs.com/yetangjian/p/17366389.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号