0

I have been trying to integrate locust with influx db and while doing so I have implement EventHook, facing error "TypeError: individual_success_handle() missing 4 required positional arguments: 'request_type', 'name', 'response_time', and 'response_length'" after running my locustfile.py The code is attached below:

from locust import HttpUser, task, TaskSet, events
import json
import datetime
import pytz
from influxdb import InfluxDBClient
import socket

hostname = socket.gethostname()
client = InfluxDBClient(host="localhost", port="8086")
client.switch_database('DemoDB')


def individual_success_handle(request_type, name, response_time, response_length, **kwargs):
    SUCCESS_TEMPLATE = '[{"measurement": "%s","tags": {"hostname":"%s","requestName": "%s","requestType": "%s",' \
                       '"status":"%s"' \
                       '},"time":"%s","fields": {"responseTime": "%s","responseLength":"%s"}' \
                       '}]'
    json_string = SUCCESS_TEMPLATE % (
        "ResponseTable", hostname, name, request_type, "success", datetime.datetime.now(tz=pytz.UTC), response_time,
        response_length)
    client.write_points(json.loads(json_string), time_precision='ms')


def individual_fail_handle(request_type, name, response_time, response_length, exception, **kwargs):
    FAIL_TEMPLATE = '[{"measurement": "%s","tags": {"hostname":"%s","requestName": "%s","requestType": "%s",' \
                    '"exception":"%s","status":"%s"' \
                    '},"time":"%s","fields": {"responseTime": "%s","responseLength":"%s"}' \
                    '}]'
    json_string = FAIL_TEMPLATE % (
        "ResponseTable", hostname, name, request_type, exception, "fail", datetime.datetime.now(tz=pytz.UTC),
        response_time, response_length)
    client.write_points(json.loads(json_string), time_precision='ms')


# my_event.add_listener(individual_success_handle);
# my_event.add_listener(individual_fail_handle);
events.request_success += individual_success_handle()
events.request_failure += individual_fail_handle()


class UserBehavior(TaskSet):
    # def on_start(self):
    """ on_start is called when a Locust start before
            any task is scheduled
        """

    @task(1)
    def profile(self):
        self.client.get("/help")

    @task(2)
    def profile(self):
        self.client.get("/pilot")


class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    min_wait = 5000
    max_wait = 9000

As I'm trying to push the values dynamically I'm not really sure what I should be giving in parameters.

Sayen
  • 65
  • 1
  • 11

1 Answers1

0

Remove the parentheses here:

events.request_success += individual_success_handle()
events.request_failure += individual_fail_handle()

You end up calling the method right away (which crashes, because you don't supply any parameters, and even if it had worked you would have added its return value to the success/failure hook, instead of the method itself)

You should be able to just copy paste from

https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/listeners.py

Just change so that you write to Influx instead of Postgres.

You'll want to buffer your events and send them in batches, as Influx (and even your load gen) might not be happy with thousands of requests/s. That's also implemented in locust-plugins.

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • thanks for replying. I have done some changes on the above code and now I'm facing some error /ERROR/locust.main: No User class found! have changed the parenthesis with events.request_success.add_listener(individual_success_handle) events.request_failure.add_listener(individual_fail_handle) – Sayen Jun 26 '20 at 09:28
  • huh. strange. are you on latest locust (1.0.3)? Try moving the @task:s into WebsiteUser (this ability was added in 1.0) – Cyberwiz Jun 26 '20 at 09:31
  • yes, I'm using locust 1.0.3 tried moving tasks under WebsiteUser and still facing the same issue. – Sayen Jun 26 '20 at 10:01
  • I would suggest commenting out everything until it works and then start uncommenting :) – Cyberwiz Jun 26 '20 at 10:37
  • 1
    I solved the issue, i was trying to import locust from another package meanwhile it was present in the same folder only. Silly mistake!! Anyways thanks for assisting here. @Cyberwiz – Sayen Jun 26 '20 at 11:03