pythonlocust

Aggregate Locust test results runtime


I have two apparantly similar tests in Locust, but one of them writes every call two the web-gui, while the other one aggregates the calls. I want both tests to do as the latter aka aggregate.

Locust GUI

This test does not aggregate the calls:

class LoadValues(FastHttpUser):
host = server_name

def _run_read_ts(self, series_list, resolution, start, end):
    TSIDs = ",".join(series_list)
    resp = self.client.get(f'/api/loadValues?tsIds={TSIDs}&resolution={resolution}'
                           f'&startUtc={set_from_date()}&endUtc={set_to_date()}',
                           headers={'X-API-KEY': 'AKjCg9hTcYQ='})

    print("Response status code:", resp.status_code)

@task
def test_get_ts_1(self):
    self._run_read_ts(random.sample(TSIDs, 1), 'PT15M', set_from_date(), set_to_date())

But this test do aggregate:

class SaveValuesUser(FastHttpUser):
host = server_name

# wait_time = between(10, 15)

@task(2)
def save_list_values(self):
    data_list = []

    # for i, (ts_id, ts_value) in enumerate(random.sample(TS_IDs.items(), random.randint(1, 25))):
    for i, (ts_id, ts_value) in enumerate(random.sample(TS_IDs.items(), 100)):
        data = get_data(ts_id, from_date, to_date, set_value())
        data_list.append(data)

    json_data = json.dumps(data_list, indent=2)
    self.save_values(json_data)

def save_values(self, json_data):
    print(type(json_data))
    print(json_data)
    # Make the PUT request with authentication:
    response = self.client.put("/api/SaveValues", data=json_data, headers=headers)
    # Check the response:
    if response.status_code == 200:
        print("SaveValues successful!")
        print("Response:", response.json())
    else:
        print("SaveValues failed.")
        print("Response:", response.text)

Solution

  • By default, Locust uses the URL as the Name it aggregates things under. Unique URL == unique stats. Notice that your endUtc URL parameter is different for each call, hence they're not being aggregated. You can override this behavior using the name parameter when making your calls.

    resp = self.client.get(f'/api/loadValues?tsIds={TSIDs}&resolution={resolution}'
                           f'&startUtc={set_from_date()}&endUtc={set_to_date()}',
                           headers={'X-API-KEY': 'AKjCg9hTcYQ='}, name='api/loadValues')