python+locust+get/post接口性能測試


#!/usr/local/bin/python3.7

from locust import HttpLocust,TaskSet,task
import gevent
# 猴子補丁
gevent.monkey.patch_all()
import os
import subprocess
import json

"""
@author:mozili
@file:functiontest.py
@time:2020/03/27
"""


class UserBehavior(TaskSet):
    # 定義用戶最先開始做的事情
    def on_start(self):
        pass
    
    def fun(self,response):
        if response.status_code != 200:
            print('返回異常')
            print("請求返回狀態碼:",response.status_code)
        elif response.status_code == 200:
            print('返回正常')
    @task(1)
    def download(self): 
        url = '/download?size=51457280'
        r = self.client.get(url=url)
        self.fun(r)
        
    @task(2)
    def upload(self):
        url = '/upload'
        params = {'size':30000000}
        r = self.client.post(url=url,params=params)
        self.fun(r)
    @task(3)
    def hello(self):
        url = '/hello'
        params = {'size':3000000}
        r = self.client.post(url=url,params=params)
        self.fun(r)
class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 3000
    max_wait = 6000

if __name__ == '__main__':
    os.system('locust -f FunctionTest/functiontest.py --host=http://10.0.0.xxx:xxx')
    # 在命令行打開
    # subprocess.Popen('locust -f FunctionTest/functiontest.py',shell=True)







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM