python locust_python locust 性能测试:locust参数-保证并发测试数据唯一性,循环取数据...

from locust import TaskSet, task, HttpLocust

import queue

class UserBehavior(TaskSet):

@task

def test_register(self):

try:

# get_nowait() 取不到数据直接崩溃;get() 取不到数据会一直等待

data = self.locust.user_data_queue.get_nowait() # 取值顺序 ‘username‘: ‘test0000‘、‘username‘: ‘test0001‘、‘username‘: ‘test0002‘...

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‘]

}

r = self.client.post(‘/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F‘, data=payload)

self.locust.user_data_queue.put_nowait(data) # 把取出来的数据重新加入队列

assert r.status_code == 200

class WebsiteUser(HttpLocust):

host = ‘https://passport.cnblogs.com‘

task_set = 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

参考:http://debugtalk.com/post/head-first-locust-advanced-script/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值