蝗虫请求统计 [英] Locust Request Statistics

查看:83
本文介绍了蝗虫请求统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 locust 进行一些性能测试.我更熟悉 Python,发现 locust 比 JMeter JMX 更容易阅读.

I'm considering using locust for some of my performance tests. I'm more familiar with Python and find locust much easier to read than JMeter JMX.

我习惯于使用 JMeter 做的一件事是通过多次运行生成我自己的平均、90pct、95pct 和 99pct 报告.为此,我编写了一个脚本来解析 JMeter 日志,其中包含有关每个请求的信息(响应时间、有效负载大小等),然后将所有运行组合到一个数据集中并生成平均值和百分位数.

One thing I'm accustomed to doing with JMeter is generating my own average, 90pct, 95pct, and 99pct reports from multiple runs. To do this, I've written a script that parses the JMeter logs which contain information on every request (response time, payload size, etc.), and then combine all the runs into a single data set and generate average and percentiles.

我似乎找不到在 locust 中获得这种级别的详细日志记录的选项.我试过 --logfile= 但该文件不包含任何关于单个请求的内容.我试过 --csv= 并且输出只包含摘要信息 - 在尝试确定运行组合中的百分位数时无法使用.

I can't seem to find an option to get this level of detailed logging in locust. I've tried --logfile= but the file contains nothing about individual requests. I've tried --csv= and the output just contains summary information - which can't be used when trying to determine the percentiles in a combination of runs.

有没有办法获取每个请求的详细日志信息?

Is there a way to get detailed log information on each request?

推荐答案

我不确定这是否是最简单的方法,但您可以使用 蝗虫事件挂钩机制.

I'm not sure if this is the easiest way but you could use locust event hooks mechanism.

让我们从命令行开始python内置http服务器:

Let's start in the command line python built-in http server:

python -m http.server

并使用以下内容创建文件 example.py:

and create file example.py with this content:

#!/usr/bin/env python
from locust import HttpUser, TaskSet, task, events

stat_file = open('stats.csv', 'w')

class UserBehavior(TaskSet):
    """ Defines user behaviour in traffic simulation """

    @task()
    def index(self):
        self.client.get("/")


class WebsiteUser(HttpUser):
    """ Defines user that will be used in traffic simulation """
    tasks = {UserBehavior:2}
    min_wait = 3000
    max_wait = 5000


# hook that is fired each time the request ends up with success
@events.request_success.add_listener
def hook_request_success(request_type, name, response_time, response_length, **kw):
    stat_file.write(request_type + ";" + name + ";" + str(response_time) + ";" + str(response_length) + "\n")


@events.quitting.add_listener
def hook_quitting(environment, **kw):
    stat_file.close()

现在在 example.py 所在的同一个文件夹中从命令行运行:

Now in the same folder where example.py lives run from the command line:

locust -f example.py --headless -u 10 -r 1 --host=http://localhost:8000

如果您在一段时间后停止它,您将在 stats.csv 文件中找到每个成功请求的详细信息.

If you stop it after a while you will find in the stats.csv file details of each successful request.

这篇关于蝗虫请求统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆