Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Small errors in docs #2253

Closed
jscanlannyc opened this issue Nov 15, 2022 · 4 comments
Closed

docs: Small errors in docs #2253

jscanlannyc opened this issue Nov 15, 2022 · 4 comments
Labels

Comments

@jscanlannyc
Copy link
Contributor

Hi,

Thank you all so much for all your hard work on this project. I recently used this for a demonstration and was amazed by its power an elegance, and would like to give back however I can.

I encountered two small issues in the docs which that I would like to address. I will gladly issue a pull request for the bugs and changes mentioned in this issue. I felt it was courtesy to raise this issue first before issuing the pull request to ensure it would be acknowledged and accepted.

Describe the bug

https://docs.locust.io/en/stable/extending-locust.html#run-a-background-greenlet

There are two python errors in the code provided on this page, which is generated by https://github.com/locustio/locust/blob/master/docs/extending-locust.rst:

Bug 1

The first example is as follows:

from locust import events

@events.request.add_listener
def my_request_handler(request_type, name, response_time, response_length, response,
                       context, exception, start_time, url, **kwargs):
    if exception:
        print(f"Request to {name} failed with exception {exception}")
    else:
        print(f"Successfully made a request to: {name})
        print(f"The response was {response.text}")

The "Successfully made a request to" f-string is invalid. Running this sample code will raise an error at runtime.

Bug 2

In Running a background greenlet in

from locust import events
from locust.runners import STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP, MasterRunner, LocalRunner

def checker(environment):
    while not environment.runner.state in [STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP]:
        time.sleep(1)
        if environment.runner.stats.total.fail_ratio > 0.2:
            print(f"fail ratio was {environment.runner.stats.total.fail_ratio}, quitting")
            environment.runner.quit()
            return


@events.init.add_listener
def on_locust_init(environment, **_kwargs):
    # dont run this on workers, we only care about the aggregated numbers
    if isinstance(environment.runner, MasterRunner) or isinstance(environment.runner, LocalRunner):
        gevent.spawn(checker, environment)

This sample code will fail in on_locust_init as it does not import gevent before using it. Running this code will raise an error at runtime.

Expected behavior

The code should be copy-and-pasteable from the docs without much modification to work. In both instances these are whole self-contained tasks and not classes.

Actual behavior

The code fails when copy-pasting into a new locust task file.

Steps to reproduce

  1. After installing locust, copy and paste the code into a locustfile.py and run locust.

Suggested Fixes

Bug 1

Change invalid f-string to:

print(f"Successfully made a request to: {name}")

Bug 2

Import gevent, which gets installed as a dependency of locust.

import gevent
from locust import events
from locust.runners import STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP, MasterRunner, LocalRunner

I again will gladly volunteer to make this change and issue a pull request.

Environment

  • OS: Mac OS Big Sur 11.6.8
  • Python version: 3.10.8
  • Locust version: 2.13.0
  • Locust command line that you ran: locust
  • Locust file contents (anonymized if necessary):
@cyberw
Copy link
Collaborator

cyberw commented Nov 15, 2022

Looking forward to the PR! Thanks!

@jscanlannyc
Copy link
Contributor Author

PR: #2254

@jscanlannyc
Copy link
Contributor Author

Hi,

Closing out this issue. Thanks for taking my contribution.

@cyberw
Copy link
Collaborator

cyberw commented Dec 5, 2022

👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants