欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3接口性能測試實(shí)例代碼

 更新時(shí)間:2021年06月20日 09:10:29   作者:林深見鹿,海藍(lán)見鯨  
在本篇文章里小編給大家整理的是一篇關(guān)于Python3實(shí)現(xiàn)簡單的接口性能測試的相關(guān)實(shí)例內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。

首先來看實(shí)例代碼:

# -*- coding:utf-8 -*-


import requests
import datetime
import time
import threading

'''
allow_redirects = False禁止重定向,添加在request參數(shù)后
get請求用params傳參
post請求,數(shù)據(jù)類型form,用data傳參
post請求,數(shù)據(jù)類型form,用data傳參
post請求,數(shù)據(jù)類型json,json傳參
timeout:請求超時(shí)時(shí)間,添加在request參數(shù)后
nub = 10#設(shè)置并發(fā)線程數(shù)
ResponseTime=float(result.elapsed.microseconds)/1000 #獲取響應(yīng)時(shí)間,單位ms
ThinkTime = 0.5#設(shè)置思考時(shí)間
AverageTime = "{:.3f}".format(float(sum(myrequest.times))/float(len(myrequest.times)))#計(jì)算數(shù)組的平均值,保留3位小數(shù)
totaltime = float(hour)*60*60 + float(minute)*60 + float(second) #計(jì)算總的思考時(shí)間+請求時(shí)間
'''

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("狀態(tài)碼:",result.status_code)
        print("返回報(bào)文:",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("請求開始時(shí)間: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("線程數(shù):thread %s" %t)
        t.setDaemon(True)
        t.start()
        t.join()
    endtime = datetime.datetime.now()
    print("請求結(jié)束時(shí)間:request end time %s" %endtime)
    time.sleep(3)
    AverageTime = "{:.3f}".format(float(sum(myrequest.times))/float(len(myrequest.times)))
    print("平均響應(yīng)時(shí)間: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("并發(fā)數(shù):Concurrent processing %s" %nub)
    print("#總共消耗的時(shí)間:use total time %s s" %(totaltime-float(nub*ThinkTime)))
    print("錯(cuò)誤請求數(shù):fail request %s s" %myrequest.error.count("0"))

實(shí)例擴(kuò)展:

利用ruquest發(fā)送請求,利用多線程模擬并發(fā)

#!/user/bin/env python
#coding=utf-8
import requests
import datetime
import time
import threading

class url_request():
    times = []
    error = []
    def req(self,AppID,url):
        myreq=url_request()
        headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
        payload = {'AppID':AppID,'CurrentURL':url}
        r = requests.post("http://xx.xxx.com/WeiXinJSAccessToken/json/WeChatJSTicket",headers=headers,data=payload)
        ResponseTime=float(r.elapsed.microseconds)/1000 #獲取響應(yīng)時(shí)間,單位ms
        myreq.times.append(ResponseTime) #將響應(yīng)時(shí)間寫入數(shù)組
        if r.status_code !=200 :
            myreq.error.append("0")
if __name__=='__main__':
    myreq=url_request()
    threads = []
    starttime = datetime.datetime.now()
    print "request start time %s" %starttime 
    nub = 50#設(shè)置并發(fā)線程數(shù)
    ThinkTime = 0.5#設(shè)置思考時(shí)間
    for i in range(1, nub+1): 
        t = threading.Thread(target=myreq.req, args=('12','http://m.ctrip.com/webapp/cpage/#mypoints'))
        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(myreq.times))/float(len(myreq.times))) #計(jì)算數(shù)組的平均值,保留3位小數(shù)
    print "Average Response Time %s ms" %AverageTime #打印平均響應(yīng)時(shí)間
    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) #計(jì)算總的思考時(shí)間+請求時(shí)間
    print "Concurrent processing %s" %nub #打印并發(fā)數(shù)
    print "use total time %s s" %(totaltime-float(nub*ThinkTime)) #打印總共消耗的時(shí)間
    print "fail request %s" %myreq.error.count("0") #打印錯(cuò)誤請求數(shù)
request start time 2015-02-10 18:24:14.316000
request end time 2015-02-10 18:24:39.769000
Average Response Time 46.700 ms
Concurrent processing 50
use total time 25.453 s
fail request 1

到此這篇關(guān)于Python3接口性能測試實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python3實(shí)現(xiàn)簡單的接口性能測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python類反射機(jī)制使用實(shí)例解析

    Python類反射機(jī)制使用實(shí)例解析

    這篇文章主要介紹了Python類反射機(jī)制使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python中json.dumps()函數(shù)的使用解析

    Python中json.dumps()函數(shù)的使用解析

    json.dumps將一個(gè)Python數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為JSON,本文介紹了Python中json.dumps()函數(shù)的具體使用方法,以及和dump的區(qū)別,感興趣的可以了解一下
    2021-05-05
  • django模型查詢操作的實(shí)現(xiàn)

    django模型查詢操作的實(shí)現(xiàn)

    一旦創(chuàng)建好了數(shù)據(jù)模型,Django就會(huì)自動(dòng)為我們提供一個(gè)數(shù)據(jù)庫抽象API,允許創(chuàng)建、檢索、更新和刪除對象操作,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-08-08
  • Python進(jìn)階之尾遞歸的用法實(shí)例

    Python進(jìn)階之尾遞歸的用法實(shí)例

    本篇文章主要介紹了Python進(jìn)階之尾遞歸的用法實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解Python中pyautogui庫的最全使用方法

    詳解Python中pyautogui庫的最全使用方法

    這篇文章主要介紹了詳解Python中pyautogui庫的最全使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 介紹Python的@property裝飾器的用法

    介紹Python的@property裝飾器的用法

    這篇文章主要介紹了介紹Python的@property裝飾器的用法,是Python學(xué)習(xí)進(jìn)階中的重要知識,代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python中位運(yùn)算的詳細(xì)用法教程

    Python中位運(yùn)算的詳細(xì)用法教程

    在Python中,位運(yùn)算是一種對二進(jìn)制數(shù)進(jìn)行操作的運(yùn)算方式,它們直接對二進(jìn)制位進(jìn)行操作,而不考慮這些位所表示的實(shí)際值,本文將詳細(xì)介紹Python中的位運(yùn)算符,需要的朋友可以參考下
    2024-08-08
  • Python中fnmatch模塊的使用詳情

    Python中fnmatch模塊的使用詳情

    這篇文章主要介紹了Python中fnmatch模塊的使用詳情,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • 解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題

    解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題

    今天小編就為大家分享一篇解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python聚類算法之凝聚層次聚類實(shí)例分析

    Python聚類算法之凝聚層次聚類實(shí)例分析

    這篇文章主要介紹了Python聚類算法之凝聚層次聚類的原理與具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11

最新評論