0
votes
#
# run command: locust --host=localhost:8000
#
import inspect
import time
from settings import CONFIG
from locust import Locust, TaskSet, task, events
from lib.usermodule import user_create_service

def stopwatch(func):
    def wrapper(*args, **kwargs):
        # get task's function name
        previous_frame = inspect.currentframe().f_back
        _, _, task_name, _, _ = inspect.getframeinfo(previous_frame)

        start = time.time()
        result = None
        try:
            result = func(*args, **kwargs)
        except Exception as e:
            total = int((time.time() - start) * 1000)
            events.request_failure.fire(request_type="TYPE",
                                        name=task_name,
                                        response_time=total,
                                        exception=e)
        else:
            total = int((time.time() - start) * 1000)
            events.request_success.fire(request_type="TYPE",
                                        name=task_name,
                                        response_time=total,
                                        response_length=0)
        return result

    return wrapper


class GRPCMyClient:
    def __init__(self):
        self.usermodule_hostport = CONFIG['grpc']['mymodule_url']

    @stopwatch
    # this is the method we want to measure
    def user_create_service(self, service_name):
        user_create_service(service_name)
        return True


class GRPCMyLocust(Locust):
    def __init__(self):
        super(GRPCMyLocust, self).__init__()
        self.client = GRPCMyClient()


class GRPCMyTasks(TaskSet):
    @task
    def user_create_service(self):
        self.client.user_create_service("koustubh-api-test")


class GRPCMyUser(GRPCMyLocust):
    task_set = GRPCMyTasks
    min_wait = 1000
    max_wait = 1000

I have the above locust file. When I run this file using

locust -f mylocustfile.py

and access localhost:8089

And set the

`Number of users to simulate -> 1`
`Hatch rate (users spawned/second) -> 1`

and start the test. The number of requests keep increasing at the rate of 2 per second enter image description here

I would have expected the maximum requests to be only 1, since both the total number of users and hatch rate is set to 1. Am I missing something?

1

1 Answers

0
votes

When you use Locust Web, it continues to hatch, unless you click on the Stop button, irrespective of the #users specified. To overcome this, there are two options

  1. Continue to use the web, But In your code, you can call self.on_stop() when the required number of users have hatched.

OR

  1. You can use the non-web version of Locust and then you can use the run time option in the locust command:

locust -f --no-web -c 1000 -r 100 --run-time 1h30m

The locust will stop after the specified amount of time