Operations 5 min read

Implementing Shared Account Data and Stop‑Event Hooking in Locust for Load Testing

This article introduces Locust, a Python‑based load‑testing framework, explains its origins, and provides a detailed solution for sharing account data among users and hooking the stop event to restore data, including code snippets and event‑handling explanations.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Implementing Shared Account Data and Stop‑Event Hooking in Locust for Load Testing

Locust is a popular, Python‑based load‑testing framework that leverages coroutines (gevent) to simulate high‑concurrency HTTP requests, offering a programmable and extensible alternative to tools like LoadRunner, JMeter, and Tsung.

The article begins with a brief introduction and background, noting that the author created Locust after finding existing tools cumbersome, emphasizing its lightweight design and the metaphor of locusts representing many simultaneous connections.

Before using Locust, readers should be familiar with Python and coroutine concepts, as Locust’s scheduling relies on Python’s generator‑based asynchronous model.

The core problem addressed is how to share a limited set of account credentials among many Locust users: each user should take an account from a shared pool, and when the test stops, the used accounts must be returned to the pool for the next run.

To solve this, the article examines Locust’s source code, focusing on the share_data structure declared in a custom Locust class, the on_start method for per‑user initialization, and the index task that performs actions using the assigned account.

When the web UI’s stop button (http://127.0.0.1:8089) is clicked, the shared data becomes incomplete because accounts remain allocated. The solution is to hook the events.locust_stop_hatching event, which is triggered on each stop, and in the handler re‑insert the released accounts back into share_data.

The article shows where the stop event is emitted in web.py and runners.locust_runner.stop, and demonstrates adding a listener via events.locust_stop_hatching += self.onLocustFinished. It warns that the event may fire multiple times (once per request) and that the data structure should handle duplicate insertions safely.

Overall, the guide provides a practical example of extending Locust to manage shared resources across test runs, illustrating both the conceptual approach and the concrete code modifications required.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonPerformance TestingLocustEvent Hookshared data
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.