Python Locust负载测试如何上传和提交xml文件

时间:2018-11-05 18:59:21

标签: python-3.x flask locust

我正在尝试找出有多少个并发用户可以使用Locust上传xml文件

蝗虫文件

from locust import HttpLocust, TaskSet, task

class HttpSession(TaskSet):
        @task
        def post_img(self):
            headers = {'1': '1', '2': '2'}
            test_file = 'c:\\xmlfolder\a.xml'
            url='/uploadxml'
            self.client.request('POST', '/upload', files={'file': open(test_file, 'rb')}, headers=headers)

class WebsiteUser(HttpLocust):
    host = 'http://localhost:5000'
    task_set = HttpSession
    min_wait = 1000
    max_wait = 3000

运行蝗虫文件时,出现405错误

理想情况下,我想给它至少3个或更多xml文件,并启动3个/ upload会话,然后上传3个不同的xml文件,我在做什么错呢?

这是一个烧瓶应用,已通过硒功能测试 os = windows,因此是斜线

2 个答案:

答案 0 :(得分:0)

我认为files参数不能按您(或我)的预期工作。

我复制了您的代码,并查看了Django应用收到的内容。数据的文件名放在XML数据之前。我更改了蝗虫任务,将数据加载到变量中并将数据传递给请求:

class HttpSession(TaskSet):
    @task
    def post_img(self):                
        headers = {'content-type': 'application/xml'}
        with open('request_data.xml', 'r') as xml_fh:
            xml_data = xml_fh.read()
        self.client.request(
            method='POST',
            url='/upload',
            data=xml_data,
            headers=headers)

答案 1 :(得分:0)

接受的答案很好,但是文件路径对我来说不起作用,因此在此之上,我必须添加:

import os

然后

...
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'request_data.xml')
with open('request_data.xml', 'r') as xml_fh:
           xml_data = xml_fh.read()
...
相关问题