Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

python - How to output csv in locust when using as library?

I'm using locust as a library in my Python script (from the example in the docs) and I want to output the stats data into a CSV file. I found out that there is a StatsCSVFileWriter class and created it, but I don't know how to "tell" it to write the file at the end.

Here's my code so far:

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()
question from:https://stackoverflow.com/questions/65641918/how-to-output-csv-in-locust-when-using-as-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seem like I only had to spawn the CSV writer I created:

gevent.spawn(csv_writer)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...