自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Locust性能測(cè)試工具核心技術(shù)@task和@events

開發(fā) 開發(fā)工具
Tasks和Events是Locust性能測(cè)試工具的核心技術(shù),有了它們,Locust才能稱得上是一個(gè)性能工具。

[[403128]]

本文轉(zhuǎn)載自微信公眾號(hào)「dongfanger」,作者dongfanger。轉(zhuǎn)載本文請(qǐng)聯(lián)系dongfanger公眾號(hào)。

Tasks和Events是Locust性能測(cè)試工具的核心技術(shù),有了它們,Locust才能稱得上是一個(gè)性能工具。

Tasks

從上篇文章知道,locustfile里面必須要有一個(gè)類,繼承User類,當(dāng)性能測(cè)試開始后,會(huì)產(chǎn)生一個(gè)User類實(shí)例,這就是常說(shuō)的模擬用戶。這些用戶會(huì)選擇task執(zhí)行,休眠一會(huì),再選擇新的task,不斷迭代。

task是Python中的可調(diào)用對(duì)象,它是一項(xiàng)任務(wù),對(duì)于Web系統(tǒng)來(lái)說(shuō),可以是登錄、查詢、下訂單、支付等等。

@task裝飾器

@task是定義task最簡(jiǎn)單直接的方式,比如:

  1. from locust import User, task, constant 
  2.  
  3. class MyUser(User): 
  4.     wait_time = constant(1) 
  5.  
  6.     @task 
  7.     def my_task(self): 
  8.         print("User instance (%r) executing my_task" % self) 

@task有一個(gè)可選參數(shù),用來(lái)設(shè)置task的選擇權(quán)重,比如:

  1. from locust import User, task, between 
  2.  
  3. class MyUser(User): 
  4.     wait_time = between(5, 15) 
  5.  
  6.     @task(3) 
  7.     def task1(self): 
  8.         pass 
  9.  
  10.     @task(6) 
  11.     def task2(self): 
  12.         pass 

task2比task1被選擇的可能性大兩倍。

tasks屬性

除了@task裝飾器,還可以設(shè)置User類的tasks屬性來(lái)定義任務(wù),比如:

  1. from locust import User, constant 
  2.  
  3. def my_task(user): 
  4.     pass 
  5.  
  6. class MyUser(User): 
  7.     tasks = [my_task] 
  8.     wait_time = constant(1) 

注意,my_task()函數(shù)有一個(gè)參數(shù),它是User類的實(shí)例。

tasks可以是一個(gè)列表:

  1. tasks = [my_task1, my_task2, my_task3] 

Locust會(huì)使用Python中的random.choice()從里面隨機(jī)選取。

tasks也可以是一個(gè)字典:

  1. {my_task: 3, another_task: 1} 

后面的int型鍵值代表的是被選擇權(quán)重,這個(gè)字典等價(jià)于列表:

  1. [my_task, my_task, my_task, another_task] 

@tag裝飾器

@tag用來(lái)打標(biāo)記,在運(yùn)行時(shí)選擇哪些task執(zhí)行,哪些task不執(zhí)行。比如:

  1. class MyUser(User): 
  2.     wait_time = constant(1) 
  3.  
  4.     @tag('tag1'
  5.     @task 
  6.     def task1(self): 
  7.         pass 
  8.  
  9.     @tag('tag1''tag2'
  10.     @task 
  11.     def task2(self): 
  12.         pass 
  13.  
  14.     @tag('tag3'
  15.     @task 
  16.     def task3(self): 
  17.         pass 
  18.  
  19.     @task 
  20.     def task4(self): 
  21.         pass 
  • 如果使用--tags tag1,那么只有task1和task2會(huì)被選擇。
  • 如果使用--tags tag2 tag3,那么只有task2和task3會(huì)被選擇。
  • 如果使用--exclude-tags tag3,那么只有task1、task2和task4會(huì)被選擇。

注意,exclude的優(yōu)先級(jí)更高,如果某個(gè)tag既包括又排除,那么會(huì)被排除。

Events

@task定義了性能測(cè)試的執(zhí)行動(dòng)作,@events作為補(bǔ)充,定義了測(cè)試開始前和測(cè)試結(jié)束后的處理。

注意,每個(gè)模擬用戶開始和結(jié)束的處理是使用的User類的on_start()和on_stop()方法。

test_start和test_stop

測(cè)試開始前和測(cè)試結(jié)束后觸發(fā)。示例:

  1. from locust import events 
  2.  
  3. @events.test_start.add_listener 
  4. def on_test_start(environment, **kwargs): 
  5.     print("A new test is starting"
  6.  
  7. @events.test_stop.add_listener 
  8. def on_test_stop(environment, **kwargs): 
  9.     print("A new test is ending"

分布式執(zhí)行時(shí),它們只會(huì)在master節(jié)點(diǎn)生效。

init

init和test_start不同,它會(huì)在每個(gè)Locust進(jìn)程開始時(shí)觸發(fā),分布式執(zhí)行時(shí),每個(gè)節(jié)點(diǎn)(worker進(jìn)程)都會(huì)生效。

  1. from locust import events 
  2. from locust.runners import MasterRunner 
  3.  
  4. @events.init.add_listener 
  5. def on_locust_init(environment, **kwargs): 
  6.     if isinstance(environment.runner, MasterRunner): 
  7.         print("I'm on master node"
  8.     else
  9.         print("I'm on a worker or standalone node"

Events是一項(xiàng)hook技術(shù),在學(xué)習(xí)Locust高級(jí)用法時(shí)再做進(jìn)一步介紹。

Locust項(xiàng)目結(jié)構(gòu)

官方建議如下:

common/

__init__.py

auth.py

config.py

locustfile.py或者locustfiles/

api.py

website.py

requirements.txt

FastHttpUser

從上篇文章可以知道,HttpUser類比User類更常用,它的client屬性是HttpSession類(requests.Session子類)的一個(gè)實(shí)例,可以使用requests發(fā)HTTP請(qǐng)求:

  1. # 使用HttpUser 
  2. from locust import HttpUser,task,constant 
  3.  
  4.  
  5. class MyUser(User): 
  6.     wait_time = constant(1) 
  7.      
  8.     @task 
  9.     def my_task1(self): 
  10.         with self.client.get("https://www.baidu.com/", catch_response=Trueas res: 
  11.             if res.status_code == 200: 
  12.                 print("成功"
  13.             else
  14.                 print("失敗"

但是requests性能是不太好的,如果要產(chǎn)生更高的壓力,建議使用FastHttpUser,性能可以提升5到6倍:

  1. # 使用FastHttpUser 
  2. from locust import task, constant 
  3. from locust.contrib.fasthttp import FastHttpUser 
  4.  
  5.  
  6. class MyUser(FastHttpUser): 
  7.     wait_time = constant(1) 
  8.  
  9.     @task 
  10.     def my_task(self): 
  11.         with self.client.get("https://www.baidu.com/", catch_response=Trueas response: 
  12.             if response.status_code == 200: 
  13.                 print("成功"
  14.             else
  15.                 print("失敗"

由于它們的API不一樣,都有各自的適用場(chǎng)景,所以FastHttpUser不能完全替代HttpUser。

小結(jié)

本文嚴(yán)格來(lái)說(shuō)是上篇文章《locustfile中的User類和HttpUser類》的下篇,介紹了核心技術(shù)Tasks和Events,并給出了官方推薦的項(xiàng)目結(jié)構(gòu),最后介紹了比HttpUser性能更好的FastHttpUser,如果想要更多的并發(fā),可以考慮使用后者。經(jīng)過(guò)這兩篇文章的學(xué)習(xí),已經(jīng)可以開始動(dòng)手實(shí)踐使用Locust進(jìn)行性能測(cè)試了。如果使用locust命令啟動(dòng)后,無(wú)法打開網(wǎng)頁(yè),可以試試加上參數(shù):locust --web-host="127.0.0.1"。

參考資料:

https://zhuanlan.zhihu.com/p/118470760

https://docs.locust.io/en/stable/writing-a-locustfile.html#tasks

https://www.axihe.com/tools/locust/increase-performance.html

 

https://blog.csdn.net/u012002125/article/details/113363768

 

責(zé)任編輯:武曉燕 來(lái)源: dongfanger
相關(guān)推薦

2021-05-07 07:27:51

Python測(cè)試工具

2024-08-23 11:38:05

2025-01-27 11:52:23

2012-08-01 10:50:48

性能測(cè)試測(cè)試架構(gòu)

2010-06-04 16:07:09

Linux 性能測(cè)試工

2010-06-07 14:42:47

Linux性能測(cè)試工具

2010-06-13 17:16:15

Linux性能測(cè)試工具

2025-01-26 11:05:23

2024-03-06 18:09:06

Linux性能工具

2010-06-04 09:12:23

Linux性能測(cè)試工具

2016-09-14 11:09:06

Web工具運(yùn)維

2010-10-15 09:37:14

MySQL性能測(cè)試

2012-12-24 22:54:31

2010-06-10 17:37:08

Linux 性能測(cè)試工

2017-09-19 18:34:16

Mysql數(shù)據(jù)庫(kù)性能測(cè)試

2023-07-13 23:21:02

2011-04-07 13:53:25

Web工具

2022-05-07 14:31:46

物聯(lián)網(wǎng)

2021-05-17 10:44:24

Python 工具編程語(yǔ)言

2009-06-26 10:22:58

JSF測(cè)試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)