性能测试-Locust参数篇

Locust性能测试 admin 4个月前 (09-07) 392次浏览 0个评论 扫描二维码

1、循环取可以重复使用的数据

csvData.csv

1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9

locust2.py

from locust import TaskSet, task, HttpUser, HttpLocust, between, tag, constant
import csv

class UserBehavior(TaskSet):

    def on_start(self):
        self.index = 0

    # @tag('getJoke分页查询')
    @task
    def test_visit(self):

        self.index = (self.index + 1 ) % len(self.parent.share_data)
        url = "/getJoke?page=" + self.parent.share_data[self.index] + "&count=10&type=video" + self.parent.share_data[self.index]

        print('visit url: %s' % url, self.parent.share_data)
        self.client.get(url, name='getJoke分页查询')

class WebsiteUser(HttpUser):
    host = 'https://api.apiopen.top'
    # task_set = UserBehavior
    tasks = [UserBehavior]
    # my_data = [1, 3, 5]
    share_data = []

    with open('csvData.csv', newline='') as csvfile:
        # 此方法:当文件不用时会自动关闭文件
        csvReader = csv.reader(csvfile)
        for content in csvReader:
            # print(content)
            share_data.append(content[1])
    # min_wait = 1000
    # max_wait = 3000
    wait_time = between(1, 3)

2、不循环取数据,测试数据唯一

使用队列存储数据

from locust import TaskSet, task, HttpUser
import queue
class UserBehavior(TaskSet):
    @task
    def test_register(self):
        try:
            data = self.parent.user_data_queue.get()
        except queue.Empty:
            print('account data run out, test ended.')
            exit(0)
        print('register with user: {}, pwd: {}'.format(data['username'], data['password']))
        payload = { 'username': data['username'], 'password': data['password'] }
        self.client.post('/register', data=payload)

class WebsiteUser(HttpUser):
    host = 'http://debugtalk.com'
    # task_set = UserBehavior
    tasks = [UserBehavior]
    user_data_queue = queue.Queue()
    for index in range(100):
        data = { "username": "test%04d" % index,
                 "password": "pwd%04d" % index,
                 "email": "test%04d@debugtalk.test" % index,
                 "phone": "186%08d" % index,
                 }
        user_data_queue.put_nowait(data)
    min_wait = 1000
    max_wait = 3000

3、循环取数据,测试数据唯一

from locust import TaskSet, task, HttpLocust
import queue
class UserBehavior(TaskSet):
    @task
    def test_register(self):
        try:
            data = self.parent.user_data_queue.get()
        except queue.Empty:
            print('account data run out, test ended.')
            exit(0)
        print('register with user: {}, pwd: {}'.format(data['username'], data['password']))
        payload = { 'username': data['username'], 'password': data['password'] }
        self.client.post('/register', data=payload)
        self.parent.user_data_queue.put_nowait(data) # 将取出的数据重新写入队列

class WebsiteUser(HttpLocust):
    host = 'http://debugtalk.com'
    # task_set = UserBehavior
    tasks = [UserBehavior]
    user_data_queue = queue.Queue()
    for index in range(100):
        data = { "username": "test%04d" % index,
                 "password": "pwd%04d" % index,
                 "email": "test%04d@debugtalk.test" % index,
                 "phone": "186%08d" % index, }
        user_data_queue.put_nowait(data)
    min_wait = 1000
    max_wait = 3000

codeobj , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:性能测试-Locust参数篇
喜欢 (0)
[a37free@163.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址