Warning
This version comes with non backward compatible changes to the API. Anyone who is currently using existing locust scripts and want to upgrade to 0.6 should read through these changes.
Locust classes does no longer control task scheduling and execution. Therefore, you no longer define tasks within Locust classes, instead the Locust class has a task_set attribute which should point to a TaskSet class. Tasks should now be defined in TaskSet classes, in the same way that was previously done in Locust and SubLocust classes. TaskSets can be nested just like SubLocust classes could.
So the following code for 0.5.1:
class User(Locust):
min_wait = 10000
max_wait = 120000
@task(10)
def index(self):
self.client.get("/")
@task(2)
class AboutPage(SubLocust):
min_wait = 10000
max_wait = 120000
def on_init(self):
self.client.get("/about/")
@task
def team_page(self):
self.client.get("/about/team/")
@task
def press_page(self):
self.client.get("/about/press/")
@task
def stop(self):
self.interrupt()
Should now be written like:
class BrowsePage(TaskSet):
@task(10)
def index(self):
self.client.get("/")
@task(2)
class AboutPage(TaskSet):
def on_init(self):
self.client.get("/about/")
@task
def team_page(self):
self.client.get("/about/team/")
@task
def press_page(self):
self.client.get("/about/press/")
@task
def stop(self):
self.interrupt()
class User(Locust):
min_wait = 10000
max_wait = 120000
task_set = BrowsePage
Each TaskSet instance gets a locust attribute, which refers to the Locust class.
Locust’s own HttpBrowser class (which was typically accessed through self.client from within a locust class) has been replaced by a thin wrapper around the requests library (http://python-requests.org). This comes with a number of advantages. Users can now take advantage of a well documented, well written, fully fledged library for making HTTP requests. However, it also comes with some small API changes wich will require users to update their existing load testing scripts.
The HTTP client now sends headers for accepting gzip encoding by default. The –gzip command line argument has been removed and if someone want to disable the Accept-Encoding that the HTTP client uses, or any other HTTP headers you can do:
class MyWebUser(Locust):
def on_start(self):
self.client.headers = {"Accept-Encoding":""}
Because of the switch to using python-requests in the HTTP client, the API for the client has also gotten a few changes.
Additionally to the get, post, put, delete and head methods, the HttpSession class now also has patch and options methods.
All arguments to the HTTP request methods, except for url and data should now be specified as keyword arguments. For example, previously one could specify headers using:
client.get("/path", {"User-Agent":"locust"}) # this will no longer work
And should now be specified like:
client.get("/path", headers={"User-Agent":"locust"})
In general the whole HTTP client is now more powerful since it leverages on python-requests. Features that we’re now able to use in Locust includes file upload, SSL, connection keep-alive, and more. See the python-requests documentation for more details.
The new HttpSession class’ methods now return python-request Response objects. This means that accessing the content of the response is no longer made using the data attribute, but instead the content attribute. The HTTP response code is now accessed through the status_code attribute, instead of the code attribute.
When doing HTTP requests using the catch_response argument, the context manager that is returned now provides two functions, success and failure that can be used to manually control what the request should be reported as in Locust’s statistics.
A Response class that also acts as a context manager that provides the ability to manually control if an HTTP request should be marked as successful or a failure in Locust’s statistics
This class is a subclass of Response with two additional methods: success and failure.
Report the response as successful
Example:
with self.client.get("/does/not/exist", catch_response=True) as response:
if response.status_code == 404:
response.success()
Report the response as a failure.
exc can be either a python exception, or a string in which case it will be wrapped inside a CatchResponseError.
Example:
with self.client.get("/", catch_response=True) as response:
if response.content == "":
response.failure("No data")
The allow_http_error argument of the HTTP client’s methods has been removed. Instead one can use the catch_response argument to get a context manager, which can be used together with a with statement.
The following code in the previous Locust version:
client.get("/does/not/exist", allow_http_error=True)
Can instead now be written like:
with client.get("/does/not/exist", catch_response=True) as response:
response.success()
- A file name is now set (using content-disposition header) when downloading stats.
- The order of the column headers for request stats was wrong.
- Thanks Benjamin W. Smith, Jussi Kuosa and Samuele Pedroni!