pythonlocust使用技巧_python locust 性能测试:HOOKS钩子方法
為locust中不同類型的事件,提供的鉤子方法;
from locust import TaskSet, task, events, Locust
from locust.clients import HttpSession
import subprocess
import random, traceback
from common.sec_encrypt import AESEncrypt, md5_time
from common import read_config
from common.logger import print_file
def on_request_success(request_type, name, response_time, response_length):
print_file(
'success Type: {}, Name: {}, Time: {}ms, length: {}'.format(request_type, name, response_time,
response_length))
def on_request_failure(request_type, name, response_time, exception):
print_file(
'failure Type: {}, Name: {}, Time: {}ms, exception: {}'.format(request_type, name, response_time,
exception))
def on_locust_error(locust_instance, exception, tb):
print_file(
"error locust_instance: {}, exception: {}, traceback: {}".format(locust_instance, exception,
traceback.format_tb(tb)))
def on_hatch_complete(user_count):
print_file("User Locust have generate {} users".format(user_count))
def on_quitting():
print_file("Locust is quiting!")
events.request_success += on_request_success
events.request_failure += on_request_failure
events.locust_error += on_locust_error
events.hatch_complete += on_hatch_complete
events.quitting += on_quitting
class WebUser(TaskSet):
def on_start(self):
self.session = HttpSession('http://localhost:8000/api')
self.r = random.randint(10000, 99999)
@task(2)
def user_auth(self):
"""用戶認(rèn)證"""
auth_user = ('lixiaofeng', 'fengzi802300')
res = self.session.get('/sec_get_event_list/', params={'eid': self.r}, auth=auth_user,
name='/sec_get_event_list/?eid=[eid]')
print_file(res.json())
# assert res.json()['status'] == 200, '用戶認(rèn)證接口請(qǐng)求失敗!'
@task(1)
def aes_api(self):
payload = {'name': '187187011{}'.format(self.r)}
# 加密
encodeed = AESEncrypt(payload).encrypt_data()
# print(encodeed)
res = self.session.get('/sec_get_guest_list/', params={'data': encodeed},
name='/sec_get_guest_list/?data=[aes]')
print_file(res.text)
# assert res.json()['status'] == 200, 'AES接口加密請(qǐng)求失敗!'
@task(2)
def md5_api(self):
payload = {"eid": "{}".format(self.r), "name": "一加8手機(jī)發(fā)布會(huì){}".format(str(self.r)), "limit": 2000, "status": "",
"address": "深圳寶體會(huì)展中心",
"start_time": "2019-09-15 22:40:00", 'time': '', 'sign': ''}
payload = md5_time(payload)
res = self.session.post('/sec_add_event/', data=payload, name='/sec_add_event/?data=[md5]')
print_file(res.text)
# assert res.json()['status'] == 200, 'md5接口加密請(qǐng)求失敗!'
@task(2)
def add_guest(self):
"""添加嘉賓"""
payload = {'eid': '{}'.format(self.r), 'realname': '趙小刀{}'.format(self.r),
'phone': '187011{}'.format(self.r),
'email': '187011{}@163.com'.format(self.r)}
res = self.session.post('/add_guest/', data=payload, name='/add_guest/?data=[payload]')
print_file(res.text)
class Run(Locust):
# host = 'http://localhost:8000/api'
task_set = WebUser
max_wait = 6000
min_wait = 3000
# 'locust -f D:\Interface_framework_Beauty\locust_files\locust_api.py --no-web -c 100 -r 10 --run-time {}s'.format(read_config.time)
def run():
subprocess.check_call(
'locust -f G:\Interface_framework_pytest\locust_files\\locust_test.py --no-web -c 1000 -r 100 --run-time {}s'.format(
read_config.time))
if __name__ == '__main__':
run()
1.request_success 成功完成請(qǐng)求后觸發(fā);
參數(shù):
request_tye:請(qǐng)求類型;
name:被調(diào)用url路徑;
response_time:響應(yīng)時(shí)間,ms;
response_length:響應(yīng)內(nèi)容長(zhǎng)度;
2.request_failure 請(qǐng)求失敗時(shí)觸發(fā);
參數(shù):
request_tye:請(qǐng)求類型;
name:被調(diào)用url路徑;
response_time:響應(yīng)時(shí)間,ms;
exception:拋出的異常;
3.locust_eror 在執(zhí)行l(wèi)ocust類內(nèi)發(fā)生異常時(shí)觸發(fā);
參數(shù):
locust_instance:發(fā)生異常的locust實(shí)例;
exception:拋出的異常;
tb:回溯的對(duì)象;
4.hatch_complete 生成所有l(wèi)ocust用戶時(shí)觸發(fā);
參數(shù):
user_count:生成的用戶數(shù);
5.quitting locust進(jìn)程退出時(shí)觸發(fā);
Python Locust性能測(cè)試框架實(shí)踐
[本文出自天外歸云的博客園] Locust的介紹 Locust是一個(gè)python的性能測(cè)試工具,你可以通過(guò)寫python腳本的方式來(lái)對(duì)web接口進(jìn)行負(fù)載測(cè)試. Locust的安裝 首先你要安裝pyth ...
python locust 性能測(cè)試:locust安裝和一些參數(shù)介紹
安裝參考 https://www.cnblogs.com/fnng/p/6081798.html ps:python3.7暫不支持locust:python3安裝建 ...
python locust 性能測(cè)試:locust 關(guān)聯(lián)---提取返回?cái)?shù)據(jù)并使用
from locust import HttpLocust, TaskSet, taskimport jsonfrom common import readConfig class UserBehav ...
python locust 性能測(cè)試:HttpSession
官網(wǎng)解釋:用于在請(qǐng)求之間執(zhí)行Web請(qǐng)求和保留(會(huì)話)cookie的類(以便能夠登錄和退出網(wǎng)站):記錄每個(gè)請(qǐng)求,以便locust可以顯示統(tǒng)計(jì)信息: from locust import TaskSet, ...
python locust 性能測(cè)試:locust 參數(shù)化(list) ---循環(huán)取數(shù)據(jù),數(shù)據(jù)可重復(fù)使用
from locust import TaskSet, task, HttpLocust class UserBehavior(TaskSet): def on_start(self): # 當(dāng)模擬用 ...
python locust 性能測(cè)試:嵌套
TaskSet類和TaskSequence類可用于嵌套: from locust import TaskSet, tas ...
python locust 性能測(cè)試:locust參數(shù)-保證并發(fā)測(cè)試數(shù)據(jù)唯一性,循環(huán)取數(shù)據(jù)
from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...
python locust 性能測(cè)試:locsut參數(shù)化-保證并發(fā)測(cè)試數(shù)據(jù)唯一性,不循環(huán)取數(shù)據(jù)
from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...
Python locust性能測(cè)試框架模板
locust框架模板 from locust import HttpLocust, TaskSet, task import Queue class UserBehavior(TaskSet): de ...
隨機(jī)推薦
oracle唯一索引與普通索引的區(qū)別和聯(lián)系以及using index用法
oracle唯一索引與普通索引的區(qū)別和聯(lián)系 區(qū)別:唯一索引unique index和一般索引normal index最大的差異是在索引列上增加一層唯一約束.添加唯一索引的數(shù)據(jù)列可以為空,但是只要尊在數(shù) ...
系統(tǒng)不支持curl
解決安裝pinphp時(shí)出現(xiàn)的“系統(tǒng)不支持curl!” ? 今天在本機(jī)安裝phppin開源程序時(shí),提示“系統(tǒng)不支持curl!”錯(cuò)誤. 由于我本機(jī)是UBUNTU系統(tǒng),所以直接通過(guò)apt-get進(jìn)行安裝. ...
ES5嚴(yán)格模式(Strict mode)
嚴(yán)格模式(Strict mode)是由ECMA-262規(guī)范定義的新興JavaScript標(biāo)準(zhǔn),第五版發(fā)布于2009年12月.旨在改善錯(cuò)誤檢查功能并且標(biāo)識(shí)可能不會(huì)延續(xù)到未來(lái)JavaScript版本的腳本 ...
在c++程序中執(zhí)行DOS命令
轉(zhuǎn)自博客:http://blog.csdn.net/ypist/article/details/8485049 #1,system()方式 在C盤根目錄下新建文件夾,名稱為12: system(&qu ...
thinkPhp 3.1.3的驗(yàn)證碼無(wú)法顯示的問(wèn)題
Image幫助類的output方法中,在下面的代碼 header("Content-type: image/" . $type); 前增加代碼: ob_end_clean();
JavaScript 運(yùn)行機(jī)制詳解:再談Event Loop
總結(jié)
以上是生活随笔為你收集整理的pythonlocust使用技巧_python locust 性能测试:HOOKS钩子方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux常用命令,亲测可用
- 下一篇: 【数学建模】基于matlab银行多机排队