Triggering Traces with Locust

Locust is an easy to use, scriptable (in Python) and scalable performance testing tool.

When running a load testing scenario with Locust you can regularly trigger Tideways callgraphs by sending the profiling header alongside HTTP requests.

Embed this code into your locustfile.py to create a new HttpTidewaysUser abstract class that you can use to load the relevant code.

import requests
import time
import random
import hashlib
import hmac
from locust import events

@events.init_command_line_parser.add_listener
def _(parser):
    parser.add_argument("--tideways-apikey", type=str, env_var="LOCUST_TIDEWAYS_APIKEY", default="", help="The API Key to trigger Tideways callgraph traces with")
    parser.add_argument("--tideways-trace-rate", type=int, env_var="LOCUST_TIDEWAYS_TRACE_RATE", default=1, help="The sample rate for triggering callgraph traces")

class HttpTidewaysUser(HttpUser):
    """
    provides a user that can trigger callgraph traces
    """

    abstract = True

    def tidewaysProfilingHeaders(self):
        if random.randint(1, 100) > self.environment.parsed_options.tideways_trace_rate:
            return {}

        apiKey = self.environment.parsed_options.tideways_apikey

        if len(apiKey) == 0:
            return {}

        m = hashlib.md5()
        m.update(apiKey.encode("utf-8"))
        profilingHash = m.hexdigest()
        validUntil = int(time.time())+120
        hm = hmac.new(str.encode(profilingHash), digestmod="sha256")
        hm.update(("method=&time=" + str(validUntil) + "&user=").encode("utf-8"))
        token = hm.hexdigest()
        header = "method=&time=" + str(validUntil) + "&user=&hash=" + token

        return {"X-Tideways-Profiler": header}

Then when building concrete users, pass the HTTP headers like in this example:

class ExampleUser(HttpTidewaysUser):
   def register(self):
      response = self.client.get('/account/register', name='register', headers=self.tidewaysProfilingHeaders())

You can then run Locust with either the console setting --tideways-apikey= or with the environment variable LOCUST_TIDEWAYS_APIKEY.

By default every 100th request is made with callgraph headers, use the --tideways-trace-rate or environment variable LOCUST_TIDEWAYS_TRACE_RATE to increase this from 1 up to 100.

Still need help? Email [email protected]