Full‑Chain Performance Testing for E‑Commerce Using Python and Locust
This article explains why full‑link performance testing is essential for e‑commerce systems, provides step‑by‑step Python Locust scripts to simulate user actions such as login, product browsing and ordering, shows how to run the tests, and demonstrates multi‑domain testing with weighted tasks.
Full‑link performance testing helps discover and solve bottlenecks, prevent system crashes during traffic spikes, improve user experience, ensure data security, and reduce operational costs for e‑commerce platforms.
First, install the Locust library:
pip install locustioThen create a Locust class that defines user behavior and request load for the entire purchase flow:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(5, 15)
# User login
@task
def login(self):
self.client.post("/login", {"username": "test", "password": "test"})
# Product upload
@task
def upload_product(self):
self.client.post("/product/upload", {"name": "Test Product", "price": "10.00"})
# Browse product
@task
def browse_product(self):
self.client.get("/product/1")
# Add to cart
@task
def add_to_cart(self):
self.client.post("/cart/add", {"product_id": "1"})
# Place order
@task
def place_order(self):
self.client.post("/order/place", {})
# Pay for order
@task
def pay_for_order(self):
self.client.post("/order/pay", {})
# Check stock
@task
def check_stock(self):
self.client.get("/stock/1")Each @task method represents a distinct user action; the wait_time setting controls the pause between successive tasks.
Run the load test with the following command, specifying the host URL of the target web server:
locust -f locustfile.py --host=http://example.comLocust will provide real‑time statistics and reports, allowing you to assess system performance under load. In practice, more factors such as network latency, data storage, and API calls should be incorporated into the test scripts.
To test multiple domains, define separate tasks for each domain and assign probabilities using the @task decorator:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(5, 15)
@task(1)
def test_domain1(self):
self.client.get("http://domain1.com")
@task(1)
def test_domain2(self):
self.client.get("http://domain2.com")Adjust the weight parameter to control the proportion of traffic each domain receives; for example, giving domain1 a weight of 2 and domain2 a weight of 1 makes domain1 receive twice as many requests as domain2.
@task(2)
def test_domain1(self):
self.client.get("http://domain1.com")
@task(1)
def test_domain2(self):
self.client.get("http://domain2.com")This configuration enables flexible, multi‑domain load testing using Locust.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.