python - 用作库时如何在locust中输出csv?

标签 python csv locust

我在 Python 脚本中使用 locust 作为库(来自文档中的示例),我想将统计数据输出到 CSV 文件中。我发现有一个 StatsCSVFileWriter类并创建它,但我不知道如何“告诉”它在最后写入文件。
到目前为止,这是我的代码:

def start_locust(time_hh: int, time_mm: int, user: int, spawn_rate: int):
    # setup Environment and Runner
    env = Environment(user_classes=[User])
    env.create_local_runner()

    # CSV writer
    stats_path = os.path.join(os.getcwd(), "data")
    csv_writer = StatsCSVFileWriter(
        environment=env,
        base_filepath=stats_path,
        full_history=True,
        percentiles_to_report=[90.0, 95.0]
    )

    # start a WebUI instance
    env.create_web_ui(host="127.0.0.1", port=8089, stats_csv_writer=csv_writer)

    # start a greenlet that periodically outputs the current stats
    gevent.spawn(stats_printer(env.stats))

    # start a greenlet that saves current stats to history
    gevent.spawn(stats_history, env.runner)

    # start the test
    env.runner.start(user_count=user, spawn_rate=spawn_rate)

    # stop the runner in a given time
    time_in_seconds = (time_hh * 60 * 60) + (time_mm * 60)
    gevent.spawn_later(time_in_seconds, lambda: env.runner.quit())

    # wait for the greenlets
    env.runner.greenlet.join()

    # stop the web server for good measures
    env.web_ui.stop()

最佳答案

似乎我只需要生成我创建的 CSV 编写器:gevent.spawn(csv_writer)

关于python - 用作库时如何在locust中输出csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65641918/

相关文章:

Python 图像处理线程

android - android与电脑sl4a的socket通信

python - 为 Python LocustIO 格式化 Json

powershell - 在Powershell中将JSON转换为Csv列

python - 蝗虫 : How to make locust run for a specific amount of time

python - 从两个不同的 Linux 发行版运行 python locust 脚本时出错

python - 组合框不显示在 tkinter 中

python - Pandas 多重索引 : iterate rows and add specific values to create a new variable

python - 在不删除逗号的情况下将数据帧写入 csv

mysql - 将查询结果导出为 csv 文件,并在 mysql 中使用内连接