Library snippets are Python files that LoadForge distributes to all runners so you can import them from your Locust tests. Create a module like custom_logger.py and then import it with import custom_logger at the top of your locustfile.

What libraries are for

  • Reusable helpers (auth, signing, payload builders)
  • Constants and configuration values
  • Custom loggers and instrumentation
  • Domain-specific client wrappers
Library files must end with .py. They are team-scoped and available to every worker at runtime.

Using a library in a test

# In Library: custom_logger.py
def log_ok(client, name):
    client.environment.events.request.fire(
        request_type="CUSTOM",
        name=name,
        response_time=0,
        response_length=0,
        exception=None,
        context={},
    )
# In your locustfile
from locust import HttpUser, task, between
import custom_logger

class User(HttpUser):
    wait_time = between(1, 3)

    @task
    def index(self):
        r = self.client.get("/")
        if r.ok:
            custom_logger.log_ok(self.client, "index")

Structure and imports

  • One module per file, e.g., utils.pyimport utils
  • Subpackages are not supported; keep modules flat
  • Avoid long import times; module code runs on import

Best practices

  • Keep helpers pure and side-effect free when possible
  • Version modules via names (e.g., auth_v2.py) if making breaking changes
  • Add docstrings and clear function names for readability
  • See API: POST /api/v2/library to create modules programmatically
  • See API: GET /api/v2/library to list modules