Skip to content

Instantly share code, notes, and snippets.

@DataGreed
Created April 18, 2019 12:34
Show Gist options
  • Select an option

  • Save DataGreed/5e1b0d2fac87ffb583ee0d77825536f4 to your computer and use it in GitHub Desktop.

Select an option

Save DataGreed/5e1b0d2fac87ffb583ee0d77825536f4 to your computer and use it in GitHub Desktop.
Locust.io trace all web requests and responses
# coding=utf-8
from locust.clients import HttpSession
from locust import HttpLocust
class LoggedHttpSession(HttpSession):
def __init__(self, base_url, debug=True, *args, **kwargs):
"""
Custom HttpSession client that saves all request and responses data for debugging
"""
self.debug = debug
super().__init__(base_url, *args, **kwargs)
def log(self, message):
print(message)
def request(self, method, url, name=None, catch_response=False, **kwargs):
response = super().request(method, url, name=name, catch_response=catch_response, **kwargs)
if self.debug:
self.log("Request: \n{} {} \n{} \n\nResponse: \nStatus Code {} \nHeaders :{} \nBody: {}\n\n".format(method, url, kwargs,
response.status_code,
response.headers,
response.content))
return response
class HttpDebugLocust(HttpLocust):
DEBUG = True
def __init__(self):
super().__init__()
"""
Swaps basic locust HttpSession client with a custom that logs all requests and responses
"""
self.client = LoggedHttpSession(base_url=self.host, debug=self.DEBUG)
class MyLocust(HttpDebugLocust):
"""
Locust that simulates user behavior
"""
task_set = tasksets.SomeTaskSet
min_wait = 1000
max_wait = 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment