• Python3实现简单的接口性能测试


    # -*- coding:utf-8 -*-
    
    
    import requests
    import datetime
    import time
    import threading
    
    '''
    allow_redirects = False禁止重定向,添加在request参数后
    get请求用params传参
    post请求,数据类型form,用data传参
    post请求,数据类型form,用data传参
    post请求,数据类型json,json传参
    timeout:请求超时时间,添加在request参数后
    nub = 10#设置并发线程数
    ResponseTime=float(result.elapsed.microseconds)/1000 #获取响应时间,单位ms
    ThinkTime = 0.5#设置思考时间
    AverageTime = "{:.3f}".format(float(sum(myrequest.times))/float(len(myrequest.times)))#计算数组的平均值,保留3位小数
    totaltime = float(hour)*60*60 + float(minute)*60 + float(second) #计算总的思考时间+请求时间
    '''
    
    class url_request:
        times = []
        error = []
        def weather_DC(self):
            myrequest=url_request()
            weatherinfo_search = 'https://restapi.amap.com/v3/weather/weatherInfo?parameters'
            params = {'key': 'cd1b11e80ffac05253196aa2a1233f25',
                      'city': 110101,
                      'extensions': 'base',
                      'output': 'JSON'}
    
            result = requests.get(url=weatherinfo_search, params=params)
            print("状态码:",result.status_code)
            print("返回报文:",result.text)
            ResponseTime=float(result.elapsed.microseconds)/1000
            myrequest.times.append(ResponseTime)
            if result.status_code !=200 :
                myrequest.error.append("0")
    if __name__=='__main__':
        myrequest=url_request()
        threads = []
        starttime = datetime.datetime.now()
        print("请求开始时间:request start time %s" %starttime)
        nub = 10
        ThinkTime = 0.5
        for i in range(1, nub+1):
            t = threading.Thread(target=myrequest.weather_DC())
            threads.append(t)
        for t in threads:
            time.sleep(ThinkTime)
            print("线程数:thread %s" %t)
            t.setDaemon(True)
            t.start()
            t.join()
        endtime = datetime.datetime.now()
        print("请求结束时间:request end time %s" %endtime)
        time.sleep(3)
        AverageTime = "{:.3f}".format(float(sum(myrequest.times))/float(len(myrequest.times)))
        print("平均响应时间:Average Response Time %s ms" %AverageTime)
        usetime = str(endtime - starttime)
        hour = usetime.split(':').pop(0)
        minute = usetime.split(':').pop(1)
        second = usetime.split(':').pop(2)
        totaltime = float(hour)*60*60 + float(minute)*60 + float(second)
        print("并发数:Concurrent processing %s" %nub)
        print("#总共消耗的时间:use total time %s s" %(totaltime-float(nub*ThinkTime)))
        print("错误请求数:fail request %s s" %myrequest.error.count("0"))

    参考文章:https://www.cnblogs.com/clarke/p/5965778.html

  • 相关阅读:
    Eclipse中SVN插件的安装方式
    Javascript实现DIV滚动自动滚动到底部
    Android程序开发的环境配置
    Android程序开发基础之——页面布局
    TinyMCE使用手册
    PHP中使用mktime获取时间戳的一个黑色幽默
    VS11本地IIS调试时(URL不使用虚拟目录,直接用localhost)
    [VS扩展工具] Image Optimizer(图像优化压缩)
    [程序安装包制作] Advanced Installer 备忘
    安装VS2010 SP1时遇到WCF RIA Service 版本错误
  • 原文地址:https://www.cnblogs.com/jiaown123/p/14902136.html
Copyright © 2020-2023  润新知