Example tests and snippets
Setting a Random User-Agent
LoadForge users support customizing any headers to whatever values you need. In this example, we will create a list of user agents and have our clients randomly select one.
This method can be adapted to any header, or the random method can be used to pick random users, etc.
Code example
Below we define 7 user agents, and then have each client pick one at random when it starts. The client will then keep that user agent for the duration of the test. This is achieved using the on_start
definition.
import time, random
from locust import HttpUser, task, between
from random import randint
USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
]
class QuickstartUser(HttpUser):
wait_time = between(3, 5)
def on_start(self):
self.headers = {
"User-Agent": USER_AGENTS[random.randint(0,len(USER_AGENTS)-1)]
}
self.client.headers = self.headers
@task(1)
def load_page(self):
self.client.get("/example-url")
Locust Test Example
LoadForge is powered by locust, meaning open source locust users can copy this script as well. If you are a locust user, consider importing your script to LoadForge to supercharge your testing!