使用 Python 工具 Locust 进行负载测试

管理员 python使用 Python 工具 Locust 进行负载测试已关闭评论15,209字数 2239阅读7分27秒阅读模式

Locust 是一个用 Python 编写的开源的负载测试工具。 它允许您针对模拟用户行为的 Web 应用程序编写测试,然后按规模运行测试以帮助查找瓶颈或其他性能问题。

安装

安装是使用 Python 常用的工具 pip 完成的:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

$pip install locustio

配置

Locust 最好的功能之一是通过”Plain Old Python”1 完成配置。您只需创建一个名为 locustfile.py 的文件,为您的负载测试任务进行所有配置,并在其中进行测试。文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

下面是 locustfile.py 的一个例子,它定义了一个简单的用户行为,它由一个获取特定网页的“任务”组成:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
   @task
   def get_something(self):
       self.client.get("/something")
class WebsiteUser(HttpLocust):
   task_set = UserBehavior

我们再来添加第二个任务:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

class UserBehavior(TaskSet):
   @task
   def get_something(self):
       self.client.get("/something")
   @task
   def get_something_else(self):
       self.client.get("/something-else")

当上面的 UserBehavior 运行时,Locust 将在每个任务之间随机选择并运行它们。 如果你想为不同的任务定义权重,那么你可以按照下面的方法来加权:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

class UserBehavior(TaskSet):
   @task(2)
   def get_something(self):
       self.client.get("/something")
   @task(1)
   def get_something_else(self):
       self.client.get("/something-else")

权重定义了所有任务执行的比例,所以这里 get_something 在负载测试中的频率会是 get_something_else 的两倍。文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

您也可以编写嵌套的任务,以执行一系列连续的或有特殊顺序的任务。 这使您可以通过多个请求来定义用户操作流。 例如:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

class UserBehavior(TaskSet):
   @task
   def get_something(self):
       self.client.get("/something")
   @task
   def get_something_else(self):
       self.client.get("/something-else")
   @task
   def get_two_things(self):
       self.get_something()
       self.get_something_else()

TaskSet 类可以有选择地声明一个 on_start 函数,当模拟用户开始执行该 TaskSet 类时会调用该函数。 在开始负载测试之前,可以使用它来登录:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

class UserBehavior(TaskSet):
   def on_start(self):
       self.client.post("/login", {
           'username': 'foo', 'password': 'bar'
       })
   @task
   def get_something(self):
       self.client.get("/something")

在本地运行

要运行 Locust,可以在与 locustfile.py 相同的目录下运行 locust 命令:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

$ locust --host=http://localhost:5000

一旦命令运行,Locust 启动一个本地 Web 服务器,您可以在浏览器中访问:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

locust文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

选择用户数量和用户产生速率后,您可以开始测试,这将显示正在运行的测试的实时视图:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

locust文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

分布式运行

在本地运行对于开始使用 Locust 和基本的测试来说是好的,但是如果您只是从本地机器运行它,大多数应用程序将不会收到很大的负载。在分布式模式下运行它几乎是不可避免的。用户可以轻松使用几个云节点来增加负载。文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

安装 Locust 并将 locustfile.py 移动到所有节点后,可以启动“主”节点:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

$ locust --host=http://localhost:5000 --master

然后启动任何 slave 节点,给他们对主节点的引用:文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

$ locust --host=http://localhost:5000 --slave\
 --master-host=192.168.10.100

不足

尽管 Locust 很好用,但是仍有有一些缺点。 首先,对于测试结果来说,统计信息相当糟糕(gen ben bu cun zai),或者说完全应该做得更好(例如,没有图表,并且不能在没有运行多个测试的情况下将增加的故障率与较高的负载相关联)。其次,有时候除了错误的状态外,很难获得错误响应的细节。 最后,做非 HTTP 或非 RESTful 请求的测试可能是会有一定复杂度的(尽管这很少见)。文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

优点

总的来说,Locust 是一个非常有用的负载测试工具,特别是作为一个开源项目。 如果您的代码库是基于 Python 的,由于有机会从现有的代码库中获取数据,模型或业务逻辑,所以这自然是您可以使用的最舒服的工具,但即使您不使用 Python,也可以轻松整合它。文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/ 文章源自运维生存时间-https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
管理员
  • 本文由 发表于 08/12/2017 14:11:00
  • 转载请务必保留本文链接:https://www.ttlsa.com/python/%e4%bd%bf%e7%94%a8-python-%e5%b7%a5%e5%85%b7-locust-%e8%bf%9b%e8%a1%8c%e8%b4%9f%e8%bd%bd%e6%b5%8b%e8%af%95/