0

I am working on Locust/pyCharm project to have separate file for each end point with different no of users to achieve different RPS for each endpoint.

In code snippet below if I remove

if __name__ == '__main__':  ApiUser().run()

and run .py file using command like

locust -f .\locustfiles\test.py --host https://something.another.nothing --users 34 --hatch-rate 10

I see locust working as expected.

Sample code below throws error. What am I missing?

from locust import HttpUser, task, between, TaskSet, User
headerJsonContent = {'Content-Type': 'application/json',  'Accept': 'application/json'}
URL2 = "/Auth/report"
host = "https://something.another.nothing"
NoOfUse = 50
class MyBaseTasks(TaskSet):
    @task
    def getData(self):
        self.client.get(URL2 = "/Auth/report", verify=False)
class ApiUser(HttpUser):
    tasks = [MyBaseTasks]
    wait_time = between(0.100, 1.500)

if __name__ == '__main__':
    ApiUser().run()

*ERROR: *super(HttpUser, self).init(*args, kwargs) TypeError: init() missing 1 required positional argument: 'environment'

BeHappy
  • 138
  • 2
  • 17
  • Hmm... your question is a little confusing. I answered the second part of your question (why you get the error), but that should only occur if you try to run the file directly (not via locust -f ...) But how does this relate to RPS? – Cyberwiz Aug 25 '20 at 08:59
  • Using single file, I am able to use Locust. I mentioned RPS to give context for what I am trying to do. Thank you. – BeHappy Aug 25 '20 at 15:27

1 Answers1

1

Users need an environment parameter. Try something like this:

from locust.env import Environment

if __name__ == '__main__':
    env = Environment()
    ApiUser(env).run()
Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • Thank you. This helped. My new error is "Either in the host attribute in the User class, or on the command line using the --host option.")" – BeHappy Aug 25 '20 at 15:24
  • 1
    Set the host attribute inside ApiUser instead of at the global level. – Cyberwiz Aug 26 '20 at 08:36
  • Hmm I tried something like this class ApiUser(HttpUser): host = data.host user = data.userCount hatch-rate = data.hatchRate tasks = [MyUserTasks] where data has values of corresponding config. Looks like I am missing something as I can specify host, but not users and hatch-rate – BeHappy Aug 27 '20 at 00:09
  • 1
    Only host can be specified in code, user count and hatch rate need to come from configuration (command line, config file or env vars). run locust --help for more info – Cyberwiz Aug 27 '20 at 06:33
  • Checked help already. and I am not clear what I am missing in using config file. – BeHappy Aug 27 '20 at 17:44
  • 1
    https://docs.locust.io/en/latest/configuration.html#configuration-file But just to be clear: the normal way to set user count and hatch rate is using command line parameters (-u / -r) – Cyberwiz Aug 27 '20 at 18:26
  • Thank you. I am looking for way to setup No of user/hatchRate per end point. Check out my q https://stackoverflow.com/questions/63622439/multiple-tasks-and-reading-user-and-hatch-rate-from-locust-config-file/63623430#63623430 – BeHappy Aug 28 '20 at 00:39