Terminate cleanly after an exact number of Task executions have occurred across multiple processes

Following on from a previous question1, I want locust to terminate a run once I’ve performed an exact number of requests across multiple Users and processes. I can track the progress on a per-User basis, but I’m struggling to communicate that to locust in a clean manner where it doesn’t try to spawn a bunch of extra Users in the dying throes of the run (which makes it complex for me to handle work assignment for each user).

Here’s a sketch of what I have at the moment:

  • User class which uses a Distributor (setup via the init event) to know how many requests it should perform in total, and counts how many it’s done:
class MyUser:
    def __init__(self):
        self.id = next(distributors["user_id"])
        self.limit = ... # calculate limit based on user's id.
    @task
    def request(self):
        # perform request
        self.requests += 1
  • After issuing each request, check how many have been issued and if done then stop this user. If all users have been stopped then stop locust – this is inspired by iteration_limit_wrapper:
        ... epilogue of request():
        if self.requests == self.limit:
            runner = self.environment.runner
            if runner.user_count == 1:
                logging.info("Last user stopped, quitting runner")
                if isinstance(runner, WorkerRunner):
                    runner._send_stats()  # send a final report
                # need to trigger this in a separate greenlet, in case
                # test_stop handlers do something async
                gevent.spawn_later(0.1, runner.quit)
            raise StopUser()

This somewhat “works” (locust indeed does stop after the work is done), but in an unclean fashion, as locust sees that some of the specified number of Users no longer exits and attempts to start them again. This results in the Distributor being called more times than I expect (and StopIteration thrown on the master) – running with --users=5 and --processes=2, and modified log format to include process_id:

[2024-05-03 12:41:10,067] localhost/70327/INFO/root: Last user stopped, quitting runner
[2024-05-03 12:41:10,237] localhost/70327/INFO/locust.main: Shutting down (exit code 0)
[2024-05-03 12:41:10,237] localhost/70322/INFO/locust.runners: Sending spawn jobs of 5 users at 5.00 spawn rate to 1 ready workers
[2024-05-03 12:41:10,237] localhost/70322/DEBUG/locust.runners: Sending spawn messages for 5 total users to 1 worker(s)
[2024-05-03 12:41:10,237] localhost/70322/DEBUG/locust.runners: Currently spawned users: {"MyUser": 3} (3 total users)
[2024-05-03 12:41:10,237] localhost/70327/DEBUG/locust.main: Cleaning up runner...
[2024-05-03 12:41:10,267] localhost/70328/DEBUG/locust.runners: Spawning additional {"MyUser": 2} ({"MyUser": 3} already running)...
[2024-05-03 12:41:10,268] localhost/70328/DEBUG/locust.runners: Sending _user_id_request message to master
[2024-05-03 12:41:10,377] localhost/70328/INFO/root: Last user stopped, quitting runner
[2024-05-03 12:41:10,520] localhost/70328/DEBUG/locust.main: Running teardowns...
[2024-05-03 12:41:10,521] localhost/70328/INFO/locust.main: Shutting down (exit code 0)
[2024-05-03 12:41:10,521] localhost/70328/DEBUG/locust.main: Cleaning up runner...
[2024-05-03 12:41:11,238] localhost/70322/INFO/locust.runners: Spawning is complete and report waittime is expired, but not all reports received from workers: {"MyUser": 3} (3 total users)
[2024-05-03 12:41:11,239] localhost/70322/INFO/locust.runners: Worker 'localhost_fb58584ee115427f8fbe19907660dd26' (index 0) quit. 0 workers ready.
[2024-05-03 12:41:11,239] localhost/70322/DEBUG/locust.runners: Received _user_id_request message from worker localhost_c92c47b1ea284949b2baf21ef71ab265 (index 1)
[2024-05-03 12:41:11,240] localhost/70322/INFO/locust.runners: Worker 'localhost_c92c47b1ea284949b2baf21ef71ab265' (index 1) quit. 0 workers ready.
[2024-05-03 12:41:11,241] localhost/70322/INFO/locust.runners: The last worker quit, stopping test.
[2024-05-03 12:41:11,241] localhost/70322/DEBUG/locust.runners: Stopping...
[2024-05-03 12:41:11,241] localhost/70322/DEBUG/locust.runners: Quitting...
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "/Users/dave/repos/pinecone-io/VSB/.venv/lib/python3.11/site-packages/locust_plugins/distributor.py", line 38, in _master_next_and_send
    item = next(self.iterator)
           ^^^^^^^^^^^^^^^^^^^
StopIteration
2024-05-03T11:41:11Z <Greenlet at 0x165afc360: <bound method Distributor._master_next_and_send of <locust_plugins.distributor.Distributor object at 0x1071aa050>>(0, 'localhost_c92c47b1ea284949b2baf21ef71ab2)> failed with StopIteration

The above is somewhat verbose (I wasn’t sure exactly what’s relavent), but the point of note is we see additional Users spawned by locust process 703222 after we have finished with all users and called quit on process 70327:

[2024-05-03 12:41:10,067] localhost/70327/INFO/root: Last user stopped, quitting runner
[2024-05-03 12:41:10,237] localhost/70327/INFO/locust.main: Shutting down (exit code 0)
[2024-05-03 12:41:10,237] localhost/70322/INFO/locust.runners: Sending spawn jobs of 5 users at 5.00 spawn rate to 1 ready workers
...
[2024-05-03 12:41:10,267] localhost/70328/DEBUG/locust.runners: Spawning additional {"MyUser": 2} ({"MyUser": 3} already running)...
...
[2024-05-03 12:41:10,519] localhost/70328/DEBUG/locust.runners: Stopping all users (called from .venv/lib/python3.11/site-packages/locust/runners.py:411)

Q: Is there a mechanism to tell locust to only spawn a given number of Users and not start any more after they finish, or perhaps a more explicit way to say “this user has successfully finished, don’t respawn” other than raise StopUser()?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật