Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper POST file upload (load testing with Locust)

I'm trying to make a load test for Django-based website.

I use Locust 0.7.3 and python 2.7.10

Here I make POST - filling the form and attaching some file:

class WebsiteTasks(TaskSet):
    def on_start(self):
        self.client.get("/")

    @task
    def submit(self):
        response = self.client.get("/submit/")
        csrftoken = response.cookies['csrftoken']
        attach = open('file.pdf', 'rb')

        r = self.client.post("/submit/", {
           'csrfmiddlewaretoken': csrftoken,
           'password': smart_str(u'wkefjgui'),
           'payload': smart_str(u'kjsdgfljdsh'),
           'docfile': attach,
           'commit': smart_str(u'Вкрапить / Embed'),
        })

Everything's seemed to be ok, but on the server's upload folder there's no file!

What I'm doing wrong?

like image 635
Nikolay Matkheev Avatar asked Mar 22 '16 19:03

Nikolay Matkheev


People also ask

Why do locusts test performance?

Locust is an easy to use, scriptable and scalable performance testing tool. You define the behaviour of your users in regular Python code, instead of being stuck in a UI or restrictive domain specific language. This makes Locust infinitely expandable and very developer friendly.


1 Answers

Well, I found the solution and I hope it will be useful for someone:

Here was described how Django handles file: How to send a "multipart/form-data" with requests in python?

And recipe is to define 'files' param in post function:

    r = self.client.post("/submit/", data={
        'csrfmiddlewaretoken': csrftoken,
        'password': smart_str(u'wkefjgui'),
        'payload': smart_str(u'kjsdgfljdsh'),
        'commit': smart_str(u'Вкрапить / Embed'),
         }, files={'docfile': attach})
like image 111
Nikolay Matkheev Avatar answered Oct 26 '22 02:10

Nikolay Matkheev