当前位置:网站首页>Locust performance test 3 (high concurrency, parameter correlation, assembly point)

Locust performance test 3 (high concurrency, parameter correlation, assembly point)

2022-07-07 09:08:00 Song_ Lun

High concurrency

FastHttpUser class
locust Of HTTP The client defaults to using requests Modular implemented , But in large-scale concurrency http When asked ,requests Modules are not the optimal implementation , To solve the problem of concurrent resource consumption ,locust It is recommended to use faster HTTP client The implementation of the , It has been used. geventhttpclient Instead of requests. It can enhance 5-6 Times the amount of concurrency .

The way to use it is to use FastHttpUser Instead of HttpUser

from locust import TaskSet, task, between
from locust.contrib.fasthttp import FastHttpUser
import json

class MyTasks(TaskSet):
    url = '/user/login'
    headers = {
    'Content-Type': 'application/json'}


    def setup(self):
        print(" Task initialization ")

    def teardown(self):
        print(" End of the task ")

    def on_start(self):
        print(" User initialization -- Sign in ")


    def on_stop(self):
        print(" end ")


    @task
    def post_login(self):

        print(' The user login ...')

class User(FastHttpUser):
    tasks = [MyTasks]
    host = "http://www.baidu.com"
    wait_time = between(2,2)

    def setup(self):
        print("Locust  initialization ")

    def teardown(self):
        print("Locust  end ")


if __name__ == '__main__':
    import os
    os.system("locust -f Fitest.py")

Use under the same concurrency conditions FastHttpUser It can effectively reduce the resource consumption of the load machine, so as to achieve greater http request .

Parameter association

Parameter correlation is often used in interface testing , such as token Pass on 、 Interface parent-child Association, etc .
Authorization Transmission and Association

from locust import HttpUser, TaskSet, task, between
import os
import json

#  The task class 
#  Need to inherit Locust class 
class MyTasks(TaskSet):
    url = '/user/login'
    headers = {
    'Content-Type': 'application/json'}

    def setup(self):
        print(" Task initialization ")

    def teardown(self):
        print(" End of the task ")

    def on_start(self):
    	#  Login is performed only once   obtain token
        global Authorization	#  Definition global keyword 
        
        print(" User initialization -- Log in to get token")
        self.data = {
    "username":"admin","password":"123456"}

        respon = self.client.post(self.url, headers = self.headers, data=json.dumps(self.data), name=' The user login ', verify=False, timeout=10)
        resp_dict = respon.json()
        # print(f' The response data is :{resp_dict}')
        if respon.status_code == 200:
        	#  success 
            print(resp_dict['msg'])
			#  After successful login   determine token Is in data It's still headers Inside 
            Authorization = respon.headers['Authorization']
            print(f'Authorization The data is :{
      Authorization}')
            return Authorization
        else:
        	#  Failure 
            respon.failure(resp_dict['msg'])


    def on_stop(self):
        print(" user -- end ")

    @task
    def post_login(self):
        print(' Custom function execution ...')
        print(Authorization)

# Locust class 
class User(HttpUser):
    tasks = [MyTasks]
    host = 'http://www.baidu.com'
    wait_time = between(2, 2)


if __name__ == '__main__':
    os.system("locust -f test.py")

It can be seen that The custom function has been output token value
 Insert picture description here

Marshal Point

Rendezvous points are used to truly simulate online user operations . In the actual pressure test scenario , It is often necessary to centralize concurrent testing at a certain point in time ( Such as : Second kill scene 、 Shopping cart payment, etc ), At this time, we need to use the assembly point , Let all users complete the initialization and launch the pressure , stay locust Can be used in gevent in Semaphore Realization .

Semaphore Object manages a counter , This counter is composed of acquire() Call decrement , And from each release() Call increments . The counter will never fall below zero , When acquire() When the counter is found to be zero , Thread blocking , Wait for another thread to call release()

SequentialTaskSet Marshal Point

Scene simulation : A shopping system , Beginning of the test 5 After users start, they first test the behavior of browsing products for a long time 3 Time , then 5 The user is testing the payment order behavior once , And then it is the pressure test of creating orders 2 Time , Finally, test the behavior of quitting browsing the product 2 Time , Cycle through the above steps

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : test.py
# @Software: PyCharm
import datetime
from locust import TaskSet, task, events, SequentialTaskSet, HttpUser, between
from gevent._semaphore import Semaphore


#  Create assembly point , When locust Triggered when the instance generation is completed ( namely 10 The user has started )
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kwargs):
    all_locusts_spawned.release()

events.spawning_complete.add_listener(on_hatch_complete)

def login(self):
    #  User login function 
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')+' The user login !')

def logout(self):
    #  The user exits the function 
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')+' User exits ')

class Sequential(SequentialTaskSet):
    def on_start(self):
        #  Only once per thread 
        login(self)

    def on_stop(self):
        #  Only once per thread 
        logout(self)


    @task(3)	#  Set the number of execution 
    def BrowseGoods(self):
        all_locusts_spawned.wait()  #  The assembly point waits for concurrency 
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') + ' Browse products ')


    @task(1)
    def PayOrder(self):
        all_locusts_spawned.wait()
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') + ' Payment order ')

    @task(2)
    def CreateOrder(self):
        all_locusts_spawned.wait()
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') + ' Create order ')


    @task(2)
    def leave(self):
        all_locusts_spawned.wait()
        print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') + ' Exit browsing the product ')


class User(HttpUser):
    tasks = [Sequential]
    host = 'https://www.baidu.com/'
    wait_time = between(2, 2)


if __name__ == '__main__':
    import os
    os.system("locust -f test2.py")
[2021-11-25 16:08:34,289] songpeilunMacBook-Pro.local/INFO/locust.main: Starting web interface at 			     http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2021-11-25 16:08:34,298] songpeilunMacBook-Pro.local/INFO/locust.main: Starting Locust 1.4.3
[2021-11-25 16:08:34,310] songpeilunMacBook-Pro.local/INFO/root: Terminal was not a tty. Keyboard input disabled
[2021-11-25 16:08:58,262] songpeilunMacBook-Pro.local/INFO/locust.runners: Spawning 5 users at the rate 1 users/s (0 users already running)...
2021-11-25 16:08:58.263733 The user login !
2021-11-25 16:08:59.268567 The user login !
2021-11-25 16:09:00.272120 The user login !
2021-11-25 16:09:01.272749 The user login !
[2021-11-25 16:09:02,274] songpeilunMacBook-Pro.local/INFO/locust.runners: All users spawned: User: 5 (5 total running)
2021-11-25 16:09:02.274811 The user login !
2021-11-25 16:09:02.274854 Browse products 
2021-11-25 16:09:02.274903 Browse products 
2021-11-25 16:09:02.274937 Browse products 
2021-11-25 16:09:02.274965 Browse products 
2021-11-25 16:09:02.274992 Browse products 
2021-11-25 16:09:04.279398 Browse products 
2021-11-25 16:09:04.279629 Browse products 
2021-11-25 16:09:04.279756 Browse products 
2021-11-25 16:09:04.279873 Browse products 
2021-11-25 16:09:04.280002 Browse products 
2021-11-25 16:09:06.279966 Browse products 
2021-11-25 16:09:06.280190 Browse products 
2021-11-25 16:09:06.280371 Browse products 
2021-11-25 16:09:06.280513 Browse products 
2021-11-25 16:09:06.280594 Browse products 
2021-11-25 16:09:08.282440 Payment order 
2021-11-25 16:09:08.282639 Payment order 
2021-11-25 16:09:08.282741 Payment order 
2021-11-25 16:09:08.282831 Payment order 
2021-11-25 16:09:08.282914 Payment order 
2021-11-25 16:09:10.285356 Create order 
2021-11-25 16:09:10.285558 Create order 
2021-11-25 16:09:10.285665 Create order 
2021-11-25 16:09:10.285755 Create order 
2021-11-25 16:09:10.285840 Create order 
2021-11-25 16:09:12.290560 Create order 
2021-11-25 16:09:12.290785 Create order 
2021-11-25 16:09:12.290912 Create order 
2021-11-25 16:09:12.291022 Create order 
2021-11-25 16:09:12.291130 Create order 
2021-11-25 16:09:14.294475 Exit browsing the product 
2021-11-25 16:09:14.294686 Exit browsing the product 
2021-11-25 16:09:14.294793 Exit browsing the product 
2021-11-25 16:09:14.294885 Exit browsing the product 
2021-11-25 16:09:14.294971 Exit browsing the product 
2021-11-25 16:09:16.299798 Exit browsing the product 
2021-11-25 16:09:16.300008 Exit browsing the product 
2021-11-25 16:09:16.300105 Exit browsing the product 
2021-11-25 16:09:16.300189 Exit browsing the product 
2021-11-25 16:09:16.300268 Exit browsing the product 
[2021-11-25 16:09:17,086] songpeilunMacBook-Pro.local/INFO/locust.runners: Stopping 5 users
[2021-11-25 16:09:17,087] songpeilunMacBook-Pro.local/INFO/locust.runners: 5 Users have been stopped, 0 still running
2021-11-25 16:09:17.087386 User exits 
2021-11-25 16:09:17.087482 User exits 
2021-11-25 16:09:17.087517 User exits 
2021-11-25 16:09:17.087543 User exits 
2021-11-25 16:09:17.087566 User exits 
原网站

版权声明
本文为[Song_ Lun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130628527112.html