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

Python Locust load testing how to upload and submit xml file

我想知道有多少并发用户可以使用 Locustxml 上传文件

蝗虫文件

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

当我 运行 我的 locust 文件时,我收到 405 错误

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

这是一个已经通过 selenium 功能测试的 Flask 应用程序 os =windows 因此斜线

我认为 files 论证并不像您(或我)期望的那样有效。

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

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)

接受答案很好,但文件路径对我不起作用,所以最重要的是,我必须添加:

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()
...