Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

load testing - Python Locust Custom Client Function has no attribute events

I am trying to write a custom client using locust, following the documentation found here: https://docs.locust.io/en/stable/testing-other-systems.html

I am 90% of the way there, but am unable to properly fire the success and failure events. I keep seeing the following error:

` File "/Users/n1531435/PycharmProjects/LoadTesting/locust_files/sgqlc_experimental.py", line 29, in wrapper self._locust_environment.events.request_success.fire( AttributeError: 'function' object has no attribute 'events'

The object in question is the _locust_environment attribute, but I don't understand how/where it is being set to a function value. The locust docs give almost no information on how to use this. When I print the _locust_environment I get the following: <function SGQLCClient.__getattribute__.<locals>.wrapper at 0x10c08ab80>

How do I set up the environment properly so that we can fire and track events?

Here is my locustfile:

import time
import test
from locust import task, User, between


class SGQLCClient(test.SampleSGLQCTests):
    """
    Simple, sglqc client implementation fires locust events on request_success and request_failure, so that all requests
    gets tracked in locust's statistics.
    """

    _locust_environment = None

    def __getattribute__(self, name):
        func = test.SampleSGLQCTests.__getattribute__(self, name)

        def wrapper(*args, **kwargs):
            start_at = time.monotonic()
            passing = func(*args, **kwargs)
            print(self._locust_environment)
            end_at = time.monotonic()
            final_time = end_at - start_at
            if passing:
                self._locust_environment.events.request_success.fire(
                    request_type="graphql", name="run script", response_time=final_time, response_length=0
                )
            else:
                self._locust_environment.events.request_failure.fire(
                    request_type="graphql", name="run script", response_time=final_time, response_length=0,
                    exception="testing"
                )
        return wrapper


class LocustSGQLCUser(User):
    abstract = True

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.client = SGQLCClient()
        self.client._locust_environment = self.environment


class SGQLCUser(LocustSGQLCUser):
    host = "Graphql Client"

    wait_time = between(0.1, 1)

    # the number here is weighting.  Higher numbers will run in greater proportion to lower ones
    @task(5)
    def load_test_renters_zip_codes(self):
        self.client.run_renters_zip_code()

    @task(10)
    def load_test_auto_zip_codes(self):
        self.client.run_auto_zip_code()

    @task(5)
    def load_test_small_business_zip_codes(self):
        self.client.run_small_business_zip_code()
question from:https://stackoverflow.com/questions/65905145/python-locust-custom-client-function-has-no-attribute-events

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not entirely sure what you're trying to do here with your code. It looks like you're overwriting __getattribute__ in your class. That may not be what you want to do. But that's how _locust_environment is returning a function. Since _locust_environment is in your class, your __getattribute__ is called and that is returning your wrapper function as a function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...