1

Reading the locust manuals, there is an example on how to use locust as library here https://docs.locust.io/en/2.0.0/use-as-lib.html , I'm rewriting the code here for conveninece:

import gevent
from locust import HttpUser, task, between
from locust.env import Environment
from locust.stats import stats_printer, stats_history
from locust.log import setup_logging

setup_logging("INFO", None)


class User(HttpUser):
    wait_time = between(1, 3)
    host = "https://docs.locust.io"

    @task
    def my_task(self):
        self.client.get("/")

    @task
    def task_404(self):
        self.client.get("/non-existing-path")


# setup Environment and Runner
env = Environment(user_classes=[User])
env.create_local_runner()

# start a WebUI instance
env.create_web_ui("127.0.0.1", 8089)

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

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

# start the test
env.runner.start(1, spawn_rate=10)

# in 60 seconds stop the runner
gevent.spawn_later(60, lambda: env.runner.quit())

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

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

I copy and pasted the example to my IDE and when I execute the code I see this output:

Type Name # reqs # fails Avg Min Max Med req/s failures/s
-------- ---------------------------------- ------- ------- ------- --------
Aggregated 0 0(0.00%) 0 0 0 0 0.00 0.00
Type Name # reqs # fails Avg Min Max Med req/s failures/s
-------- ---------------------------------- ------- ------- ------- --------
Aggregated 0 0(0.00%) 0 0 0 0 0.00 0.00
Type Name # reqs # fails Avg Min Max Med req/s failures/s
-------- ---------------------------------- ------- ------- ------- --------
Aggregated 0 0(0.00%) 0 0 0 0 0.00 0.00

So really no operation was done. Also during execution I was suggested to open the UI which I did and UI shows nothing.

I would expect that an example posted in a user manual should run with no need of modifications or changes.

1 Answer 1

1

OK,I overlooked the fact that I'm behind a proxy. Once I realized that, I updated the following part of the above code like this:

    @task
    def my_task(self):
        self.client.trust_env = True
        self.client.get("/")

    @task
    def task_404(self):
        self.client.trust_env = True
        self.client.get("/non-existing-path")

After that I was able to see the expected output:

Type Name # reqs # fails Avg Min Max Med req/s failures/s
GET / 11 0(0.00%) 222 165 632 180 0.20 0.00
GET /non-existing-path 15 15(100.00%) 137 82 739 91 0.30 0.30
-------- ---------------------------------- ------- ------- ------- --------
Aggregated 26 15(57.69%) 173 82 739 100 0.50 0.30
Type Name # reqs # fails Avg Min Max Med req/s failures/s
GET / 11 0(0.00%) 222 165 632 180 0.20 0.00
GET /non-existing-path 16 16(100.00%) 134 82 739 91 0.30 0.30
-------- ---------------------------------- ------- ------- ------- --------
Aggregated 27 16(59.26%) 170 82 739 100 0.50 0.30

Also when I opened the UI as suggested at execution time, I was able to see the stats of the execution.

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.