python实现接口压力测试

2020-12-15 10:47:38 浏览数 (1)

直接上代码:

代码语言:javascript复制
# -*- coding: utf-8 -*-
# @Time : 2020-12-05 13:05
# @Author : Administrator
# @Email : 984701108@qq.com
# @File : Interface_pressure_test.py
# @Project : inter

import json
import requests
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

restime = []
OK=[]
class Interface_pressure_test():
    def API(self,URL2,param):
        try:
            r = requests.get(URL2, params=param, timeout=10)
            r.raise_for_status()  # 如果响应状态码不是 200,就主动抛出异常
        except requests.RequestException as e:
            print(e)
        else:
            js = json.dumps(r.json())
            return [r.json(), r.elapsed.total_seconds(),js]

    def circulation(self,num,URL2,param):
        for i in range(int(num)):
            restime.append(Interface_pressure_test.API(URL2,param)[1])
            if json.loads(Interface_pressure_test.API(URL2, param)[2])["message"]=='ok':
                OK.append(json.loads(Interface_pressure_test.API(URL2, param)[2])["message"])
                logger.info('请求第'   str(i 1)   '次,请求' json.loads(Interface_pressure_test.API(URL2, param)[2])["message"] ',状态码:' json.loads(Interface_pressure_test.API(URL2, param)[2])["status"])
            else:
                logger.info('请求第'   str(i 1)   '次,请求'   json.loads(Interface_pressure_test.API(URL2, param)[2])["message"]   ',状态码:'  
                            json.loads(Interface_pressure_test.API(URL2, param)[2])["status"])
        print('测试次数:',num)
        print('响应次数:', len(restime))
        print('正常响应次数:', len(OK))
        print('总响应最大时长:', max(restime))
        print('总响应最小时长:', min(restime))
        print('总响应时长:', sum(restime))
        print('平均响应时长:', sum(restime) / len(restime))

if __name__ == '__main__':
    Interface_pressure_test = Interface_pressure_test()
    num = input('请输入次数:') #压力测试次数
    URL2 = 'http://www.kuaidi100.com/query'  #地址
    param = {'type' : 'zhongtong' , 'postid' :'73116039505988' }  #参数
    Interface_pressure_test.circulation(num,URL2,param)
    input('Press Enter to exit...')

输出20次压测的结果如下:

0 人点赞