背景:
使用默認(rèn)的locust啟動命令進(jìn)行壓測時,盡管已經(jīng)將用戶數(shù)設(shè)置大比較大(400),但是壓測的時候RPS一直在100左右。需要增加壓測的壓力。
問題原因:
如果你是通過命令行啟動的或者參考之前文章的啟動方式:
-
命令行:
-
locust 庫方法:
`
因為create_local_runner
會創(chuàng)建一個LocalRunner
,這個runner只有一個WorkerNode
解決方式:
-
單機(jī):充分利用CPU每一個核心
-
os_start(True),指定參數(shù)
True``則充分利用cpu的性能,否則就和之前調(diào)用create_local_runner
一樣。 使用os、multiprocessing完成, 模擬的是命令行啟動locust的操作.。 -
local_start(True)
,使用locust的庫方法,根據(jù)CPU的核心數(shù)、線程數(shù)創(chuàng)建WorkerNode
-
-
多機(jī):利用多臺機(jī)器的能力提高測試壓力文章來源:http://www.zghlxwxcb.cn/news/detail-799783.html
-
slave_start(master_ip, master_port=5557)
,指定主機(jī)的IP及端口號。
# -*- coding:UTF-8 -*- """ @ProjectName : pyExamples @FileName : locust_demo @Description : @Time : 2024/1/4 下午11:30 @Author : Qredsun """ import os import socket import psutil from multiprocessing import Process from locust import HttpUser, events, task, between from locust.env import Environment class UserRun(HttpUser): wait_time = between(min_wait=0.1, max_wait=0.2) # 設(shè)置task運行間隔 @task # 裝飾器,說明下面是一個任務(wù) def getuser_(self): url = 'https://analytics.cnblogs.com/api/v1/reports' # 接口請求的URL地址 payload = {"blogId": 485117, "postId": 10365033, "url": "https://www.cnblogs.com/happyyangyanghappy/p/10365033.html", "resolution": "1920x1080", "referrer": "https://www.ecosia.org/", "createdAt": "2024-01-04T16:17:52.241Z"} with self.client.post(url, json=payload, catch_response=True) as rsp: if rsp.status_code == 200: rsp.success() else: rsp.failure(f'接口調(diào)用失?。?/span>{rsp.json()}') def current_ip(): ip = None interfaces = psutil.net_connections(kind='inet4') for interface in interfaces: if interface.type == socket.SocketKind.SOCK_STREAM and interface.status is not None and bool(interface.raddr): if interface.raddr.ip != "127.0.0.1": print(interface.laddr.ip) ip = interface.laddr.ip break return ip def local_start(multiprocess=False, master_ip=None): # 使用locust庫啟動 web_host = current_ip() web_port = 8089 runners = [] master_env = Environment(user_classes=[UserRun], events=events) if multiprocess: # 主節(jié)點 master_ip = web_host master_port = 5557 master_runner = master_env.create_master_runner(master_bind_host=master_ip, master_bind_port=master_port) # 工作節(jié)點數(shù)量 process_num = psutil.cpu_count() # slave for _ in range(process_num): env = Environment(user_classes=[UserRun], events=events) slave_runner = env.create_worker_runner(master_host=master_ip, master_port=master_port) runners.append(slave_runner) else: master_runner = master_env.create_local_runner() runners.append(master_runner) web_ui = master_env.create_web_ui(host=web_host, port=web_port) runners.append(web_ui) master_env.events.init.fire(environment=master_env, runner=master_runner, web_ui=web_ui) for runner in runners: runner.greenlet.join() def os_start(multiprocess=False): # 使用os庫啟動 web_host = current_ip() web_port = 8089 master_ip = web_host master_port = 5557 master_cmd = f"locust -f {os.path.basename(__file__)} --web-host {web_host} --web-port {web_port} --run-time 180s" process_num = psutil.cpu_count(logical=True) process_list = [] if multiprocess: if os.name == "nt": # Windows 系統(tǒng) master_cmd += f" --master --master-bind-host {master_ip} --master-bind-port {master_port}" process_list.append(Process(target=os.system, args=(master_cmd,))) slave_cmd = f"locust -f {os.path.basename(__file__)} --worker --master-host {master_ip} --master-port {master_port}" for _ in range(process_num): process_list.append(Process(target=os.system, args=(slave_cmd,))) else: # linux master_cmd += f" --master-host {master_ip} --master-port {master_port} --processes -1" process_list.append(Process(target=os.system, args=(master_cmd,))) else: process_list.append(Process(target=os.system, args=(master_cmd,))) for p in process_list: p.start() for p in process_list: p.join() def slave_start(master_ip, master_port=5557): # 使用locust庫啟動 runners = [] # 工作節(jié)點數(shù)量 process_num = psutil.cpu_count() # slave for _ in range(process_num): env = Environment(user_classes=[UserRun], events=events) slave_runner = env.create_worker_runner(master_host=master_ip, master_port=master_port) runners.append(slave_runner) for runner in runners: runner.greenlet.join() if __name__ == '__main__': # os_start(True) local_start(True)
-
工作效果:
文章來源地址http://www.zghlxwxcb.cn/news/detail-799783.html
到了這里,關(guān)于locust快速入門--使用分布式提高測試壓力的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!