使用Locust对同时从CSV加载的多个URL进行负载测试

时间:2020-03-09 04:03:17

标签: linux ubuntu load-testing web-testing locust

请参阅下面的我的locusfile.py

from locust import HttpLocust, TaskSet, between, task
import csv

class UserBehavior(TaskSet):
    @task(1)
    def index(l):
        with open ('topURL.csv') as csvfile:
            readCSV = csv.reader(csvfile, delimiter=',')
            for row in readCSV:
                l.client.get("%s" % (row[0]))

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    wait_time = between(5.0, 9.0)

当我执行此脚本时,Locust能够运行而没有任何错误。但是,它将遍历每一行并仅对最新的URL进行负载测试。当它读取下一个URL时,不再对前一个URL进行负载测试。我想要的是Locust从CSV逐行读取时并发地加载越来越多的URL。

修改
我通过设置wait_time = between(0.0, 0.0)

来实现部分并发

2 个答案:

答案 0 :(得分:1)

尝试在设置时用csv数据填充数组,然后从中随机选择。 像

    def fill_array():
        with open('topURL.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            urls.append(row[0])

然后

    @task(1)
    def index(l):
        l.client.get("%s" % (random.choice(urls)))

更多有关设置的信息: https://docs.locust.io/en/stable/writing-a-locustfile.html#setups-teardowns-on-start-and-on-stop

答案 1 :(得分:0)

您可以尝试以下方法:

global USER_CREDENTIALS
USER_CREDENTIALS = list(readCSV)

完成后,您将能够为每个虚拟用户/迭代引用每一行

参考:

相关问题