当前位置:网站首页>Locust performance test - parameterization, no repetition of concurrent cyclic data sampling
Locust performance test - parameterization, no repetition of concurrent cyclic data sampling
2022-06-28 02:17:00 【wumingxiaoyao】
background
Performance testing , Sometimes someone API Requests can only be processed once for the same data ( Such as user registration ), Or can only be executed sequentially , Multiple users are not allowed to operate on the data at the same time , Different data can be requested concurrently . Therefore, data sampling needs to be handled , Avoid multiple users sampling the same data at the same time .
Python queue
queue yes Python Standard library in , Commonly known as queue , Can directly import quote .
stay Python in , Data between multiple threads is shared , When multiple threads exchange data , Can't guarantee the security and consistency of data , So when multiple threads need to exchange data , The queue appears , Queues can perfectly solve the data exchange between threads , Ensure the security and consistency of data between threads .
The queue will pass the first in first out or first in last out mode , It ensures that a single data will not be accessed by multiple threads at the same time .
queue Modules have three types of queues and constructors :
If maxsize Less than 1 That means the queue length is infinite
FIFO First in first out queue . class queue.Queue(maxsize=0)
LIFO It's like a heap , First in, then out . class queue.LifoQueue(maxsize=0)
The lower the priority queue level, the first to come out . class queue.PriorityQueue(maxsize=0)
queue Common methods in modules :
queue.qsize() Returns the size of the queue
queue.empty() If the queue is empty , return True, conversely False
queue.full() If the queue is full , return True, conversely False
queue.full And maxsize Size correspondence
queue.get([block[, timeout]]) Get the queue ,timeout Waiting time
queue.get_nowait() Quite a queue.get(False)
Queue.put(item, [block[, timeout]]) Written to the queue ,timeout Waiting time
queue.put_nowait(item) Quite a queue.put(item, False)
queue.task_done() After finishing a job ,queue.task_done() Function sends a signal to the queue that the task has completed
queue.join() It essentially means waiting until the queue is empty , Do something else
Locust application
stay HttpUser Class , Generate queue data
class TestLocust(HttpUser):
queue_list = queue.Queue()
for i in range(1, 10):
queue_list.put_nowait(i)
stay task In the method, we use self.parent.queue_list.get() To visit queue Data in . If you want to cycle the queue data , You can put the extracted data back queue in , self.parent.queue_list.put_nowait(data)
class UserBehavior(TaskSet):
@task
def get_root(self):
data = self.parent.queue_list.get()
print("queue data:{}".format(data))
# self.parent.queue_list.put_nowait(data)
The following is a practical example , Let's see Locust Multi user concurrency queue Application of queues . In order to better show the concurrent process , Add some log Information , Every task Interval between 5s, simulation 2 individual user Concurrent .
1. Cyclic data retrieval , The data is not repeated
from locust import TaskSet, task, HttpUser
import os
from locust.user.wait_time import between
import queue
class UserBehavior(TaskSet):
def on_start(self):
print('taskset start')
self.root_index = 0
def on_stop(self):
print('taskset end')
@task
def get_root(self):
print("get_root task")
print("root index : " + str(self.root_index))
self.root_index += 1
if not self.parent.queue_list.empty():
data = self.parent.queue_list.get()
print("queue data:{}".format(data))
response = self.client.get('',name='get_root')
else:
print("no data exist")
exit(0)
if not response.ok:
print(response.text)
response.failure('Got wrong response')
class TestLocust(HttpUser):
# If no wait_time is specified, the next task will be executed as soon as one finishes.
wait_time = between(5,5)
def on_start(self):
print('locust user start')
def on_stop(self):
print('locust user stop')
tasks = [UserBehavior]
host = "https://cn.bing.com"
queue_list = queue.Queue()
for i in range(1, 6):
queue_list.put_nowait(i)
if __name__ == "__main__":
# -u concurrency user number
# -r generate user number per second
# --run-time or -t
os.system("locust -f test_locust.py --headless -u 2 -r 1 --run-time 20s --stop-timeout 5")
Output :
It can be seen that 2 individual user All maintain their own variables root index, however queue The data of is taken out in the order of first in first out , until queue Empty , There's no duplicate data .
locust user start
taskset start
get_root task
root index : 0
queue data:1
locust user start
taskset start
get_root task
root index : 0
queue data:2
get_root task
root index : 1
queue data:3
get_root task
root index : 1
queue data:4
get_root task
root index : 2
queue data:5
get_root task
root index : 2
no data exist
2. Cyclic data retrieval , Duplicate data
take queue Put the retrieved data back queue A party , So the data in the series will not be empty .
from locust import TaskSet, task, HttpUser
import os
from locust.user.wait_time import between
import queue
class UserBehavior(TaskSet):
def on_start(self):
print('taskset start')
self.root_index = 0
def on_stop(self):
print('taskset end')
@task
def get_root(self):
print("get_root task")
print("root index : " + str(self.root_index))
self.root_index += 1
if not self.parent.queue_list.empty():
data = self.parent.queue_list.get()
print("queue data:{}".format(data))
# put the data back to the queue
self.parent.queue_list.put_nowait(data)
response = self.client.get('',name='get_root')
else:
print("no data exist")
exit(0)
if not response.ok:
print(response.text)
response.failure('Got wrong response')
class TestLocust(HttpUser):
# If no wait_time is specified, the next task will be executed as soon as one finishes.
wait_time = between(5,5)
def on_start(self):
print('locust user start')
def on_stop(self):
print('locust user stop')
tasks = [UserBehavior]
host = "https://cn.bing.com"
queue_list = queue.Queue()
for i in range(1, 6):
queue_list.put_nowait(i)
if __name__ == "__main__":
# -u concurrency user number
# -r generate user number per second
# --run-time or -t
os.system("locust -f test_locust.py --headless -u 2 -r 1 --run-time 20s --stop-timeout 5 --logfile log.txt --csv=example")
Output :
It can be seen that 2 individual user All maintain their own variables root index, however queue The data of is taken out in the order of first in first out , And then put the fetched number back to the end of the queue , therefore queue Never empty , Cyclic data retrieval , The data will repeat .
locust user start
taskset start
get_root task
root index : 0
queue data:1
locust user start
taskset start
get_root task
root index : 0
queue data:2
get_root task
root index : 1
queue data:3
get_root task
root index : 1
queue data:4
get_root task
root index : 2
queue data:5
get_root task
root index : 2
queue data:1
get_root task
root index : 3
queue data:2
get_root task
root index : 3
queue data:3
taskset end
locust user stop
taskset end
locust user stop
边栏推荐
- To understand what is synchronous, asynchronous, serial, parallel, concurrent, process, thread, and coroutine
- Cesium Click to obtain longitude and latitude (2D coordinates)
- Take n multiple table names of a database as the values of a column in another table (the range can be a table in another database)
- Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
- [Yocto RM]8 - OpenEmbedded Kickstart (.wks) Reference
- fiddle如何使用代理
- Solve storage problems? WMS warehouse management system solution
- Cesium 抗锯齿(线,边框等)
- I/o limit process and CPU limit process
- Numpy----np.tile()函数解析
猜你喜欢
【嵌入式基础】内存(Cache,RAM,ROM,Flash)
Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
1382. 将二叉搜索树变平衡-常规方法
LMSOC:一种对社会敏感的预训练方法
Appium自动化测试基础 — ADB常用命令(一)
自监督学习与药物发现
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
To understand what is synchronous, asynchronous, serial, parallel, concurrent, process, thread, and coroutine
【牛客討論區】第四章:Redis
Shardingsphere-proxy-5.0.0 establish MySQL read / write separation connection (6)
随机推荐
To understand what is synchronous, asynchronous, serial, parallel, concurrent, process, thread, and coroutine
geojson 格式说明(格式详解)
LMSOC:一种对社会敏感的预训练方法
Prometeus 2.35.0 new features
Differences between cesium polygon extrudedheight and height
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
评价——灰色关联分析
How fiddle uses agents
【sylixos】i2c设备驱动创建和使用
Using redis bitmap to realize personnel online monitoring
[embedded foundation] memory (cache, ram, ROM, flash)
一张图弄懂 MIT,BSD,Apache几种开源协议之间的区别
Is it safe to open an online futures account?
Coscon'22 lecturer solicitation order
Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
Maimai hot post: Why are big factories keen on making wheels?
MySQL十种锁,一篇文章带你全解析
TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language
Implementation of timed tasks in laravel framework
ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)