日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Locustfile中的User類和HttpUser類

[[399412]]

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

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),攸縣企業(yè)網(wǎng)站建設(shè),攸縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,攸縣網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,攸縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

locustfile是什么?

locustfile是Locust性能測試工具的用戶腳本,描述了單個(gè)用戶的行為。

locustfile是個(gè)普通的Python模塊,如果寫作locustfile.py,那么路徑切換到文件所在目錄,直接執(zhí)行命令就能運(yùn)行:

 
 
 
 
  1. $ locust 

如果換個(gè)名字,那么只能通過-f參數(shù)指定文件名運(yùn)行:

 
 
 
 
  1. $ locust -f locust_files/my_locust_file.py 

與一般Python模塊不同的是:locustfile必須至少定義一個(gè)類,且繼承自User類。

User類

User類表示性能測試的模擬用戶,Locust會(huì)在運(yùn)行時(shí)創(chuàng)建User類的實(shí)例。

wait_time屬性

設(shè)置等待時(shí)間,默認(rèn)值不等待,立即執(zhí)行。

Locust支持4種方式設(shè)置wait_time屬性。

為了便于理解實(shí)際意義,我把源碼貼在了下面。

  • constant函數(shù),常量,任務(wù)執(zhí)行完畢等待X秒開始下一任務(wù)。
 
 
 
 
  1. def constant(wait_time): 
  2.     """ 
  3.     Returns a function that just returns the number specified by the wait_time argument 
  4.  
  5.     Example:: 
  6.  
  7.         class MyUser(User): 
  8.             wait_time = constant(3) 
  9.     """ 
  10.     return lambda instance: wait_time 
  • between函數(shù),區(qū)間隨機(jī)值,任務(wù)執(zhí)行完畢等待X-Y秒(中間隨機(jī)取值)開始下一任務(wù)。
 
 
 
 
  1. def between(min_wait, max_wait): 
  2.     """ 
  3.     Returns a function that will return a random number between min_wait and max_wait. 
  4.  
  5.     Example:: 
  6.  
  7.         class MyUser(User): 
  8.             # wait between 3.0 and 10.5 seconds after each task 
  9.             wait_time = between(3.0, 10.5) 
  10.     """ 
  11.     return lambda instance: min_wait + random.random() * (max_wait - min_wait) 
  • constant_pacing函數(shù),自適應(yīng),若任務(wù)耗時(shí)超過該時(shí)間,則任務(wù)結(jié)束后立即執(zhí)行下一任務(wù);若任務(wù)耗時(shí)不超過該時(shí)間,則等待達(dá)到該時(shí)間后執(zhí)行下一任務(wù)。
 
 
 
 
  1. def constant_pacing(wait_time): 
  2.     """ 
  3.     Returns a function that will track the run time of the tasks, and for each time it's 
  4.     called it will return a wait time that will try to make the total time between task 
  5.     execution equal to the time specified by the wait_time argument. 
  6.  
  7.     In the following example the task will always be executed once every second, no matter 
  8.     the task execution time:: 
  9.  
  10.         class MyUser(User): 
  11.             wait_time = constant_pacing(1) 
  12.             @task 
  13.             def my_task(self): 
  14.                 time.sleep(random.random()) 
  15.  
  16.     If a task execution exceeds the specified wait_time, the wait will be 0 before starting 
  17.     the next task. 
  18.     """ 
  19.  
  20.     def wait_time_func(self): 
  21.         if not hasattr(self, "_cp_last_run"): 
  22.             self._cp_last_wait_time = wait_time 
  23.             self._cp_last_run = time() 
  24.             return wait_time 
  25.         else: 
  26.             run_time = time() - self._cp_last_run - self._cp_last_wait_time 
  27.             self._cp_last_wait_time = max(0, wait_time - run_time) 
  28.             self._cp_last_run = time() 
  29.             return self._cp_last_wait_time 
  30.  
  31.     return wait_time_func 
  • 自定義wait_time方法,比如每次等待時(shí)間1秒2秒3秒遞增:
 
 
 
 
  1. class MyUser(User): 
  2.     last_wait_time = 0 
  3.  
  4.     def wait_time(self): 
  5.         self.last_wait_time += 1 
  6.         return self.last_wait_time 
  7.  
  8.     ... 

weight屬性

設(shè)置創(chuàng)建類實(shí)例的權(quán)重,默認(rèn)每個(gè)類創(chuàng)建相同數(shù)量的實(shí)例。

locustfile中可以有多個(gè)繼承了User類的類。

命令行可以指定運(yùn)行哪些類:

 
 
 
 
  1. $ locust -f locust_file.py WebUser MobileUser 

通過weight屬性可以讓類更大概率創(chuàng)建實(shí)例,比如:

 
 
 
 
  1. class WebUser(User): 
  2.     weight = 3 
  3.     ... 
  4.  
  5. class MobileUser(User): 
  6.     weight = 1 
  7.     ... 

WebUser類比MobileUser類多三倍概率創(chuàng)建實(shí)例。

host屬性

設(shè)置URL前綴。

一般是在Locust的Web UI或者命令行,通過--host指定URL前綴。如果沒有通過--host指定,并且類中設(shè)置了host屬性,那么類的host屬性才會(huì)生效。

environment屬性

對用戶運(yùn)行環(huán)境的引用。

比如在task方法中通過environment屬性終止運(yùn)行:

 
 
 
 
  1. self.environment.runner.quit() 

注意,單機(jī)會(huì)終止所有運(yùn)行,分布式只會(huì)終止單個(gè)worker節(jié)點(diǎn)。

on_start和on_stop方法

測試前初始化和測試后清理。

HttpUser類

開篇文章的示例腳本,沒有繼承User類,而是繼承了它的子類HttpUser:

它比User類更常用,因?yàn)樗砑恿艘粋€(gè)client屬性,用來發(fā)送HTTP請求。

client屬性/HttpSession

HttpUser類的client屬性是HttpSession類的一個(gè)實(shí)例:

HttpSession是requests.Session的子類,requests就是常用來做接口測試的那個(gè)requests庫:

HttpSession沒有對requests.Session做什么改動(dòng),主要是傳遞請求結(jié)果給Locust,比如success/fail,response time,response length,name。

示例:

 
 
 
 
  1. response = self.client.post("/login", {"username":"testuser", "password":"secret"}) 
  2. print("Response status code:", response.status_code) 
  3. print("Response text:", response.text) 
  4. response = self.client.get("/my-profile") 

由于requests.Session會(huì)暫存cookie,所以示例中登錄/login請求后可以繼續(xù)請求/my-profile。

斷言響應(yīng)結(jié)果

可以使用with語句和catch_response參數(shù)對響應(yīng)結(jié)果進(jìn)行斷言:

 
 
 
 
  1. with self.client.get("/", catch_response=True) as response: 
  2.     if response.text == "Success": 
  3.         response.success() 
  4.     elif response.text != "Success": 
  5.         response.failure("Got wrong response") 
  6.     elif response.elapsed.total_seconds() > 0.5: 
  7.         response.failure("Request took too long") 

或者直接拋出異常:

 
 
 
 
  1. from locust.exception import RescheduleTask 
  2. ... 
  3. with self.client.get("/does_not_exist/", catch_response=True) as response: 
  4.     if response.status_code == 404: 
  5.         raise RescheduleTask() 

name參數(shù)

name參數(shù)用于把不同api按同一分組進(jìn)行統(tǒng)計(jì),比如:

 
 
 
 
  1. for i in range(10): 
  2.     self.client.get("/blog?id=%i" % i, name="/blog?id=[id]") 

會(huì)按/blog/?id=[id]統(tǒng)計(jì)1條數(shù)據(jù),而不是分成10條數(shù)據(jù)。

HTTP代理

Locust默認(rèn)設(shè)置了requests.Session的trust_env為False,不查找代理,以提高運(yùn)行性能。如果需要可以設(shè)置locust_instance.client.trust_env為True。

示例代碼

請求REST API并斷言:

 
 
 
 
  1. from json import JSONDecodeError 
  2. ... 
  3. with self.client.post("/", json={"foo": 42, "bar": None}, catch_response=True) as response: 
  4.     try: 
  5.         if response.json()["greeting"] != "hello": 
  6.             response.failure("Did not get expected value in greeting") 
  7.     except JSONDecodeError: 
  8.         response.failure("Response could not be decoded as JSON") 
  9.     except KeyError: 
  10.         response.failure("Response did not contain expected key 'greeting'") 

小結(jié)

locustfile是個(gè)普通Python模塊,必須繼承User類或其子類HttpUser等。本文對User類和HttpUser類的屬性和方法進(jìn)行了介紹,使用它們可以編寫性能測試的用戶腳本。locustfile還有另外一個(gè)重要組成元素,@task。


本文標(biāo)題:Locustfile中的User類和HttpUser類
網(wǎng)站路徑:http://www.dlmjj.cn/article/dhgocoe.html