最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)的HTTP并發(fā)測(cè)試完整示例

 更新時(shí)間:2020年04月23日 15:08:01   作者:redwingz  
這篇文章主要介紹了Python實(shí)現(xiàn)的HTTP并發(fā)測(cè)試,涉及Python多線(xiàn)程并發(fā)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

可修改變量thread_count指定最大的并發(fā)數(shù)量,即線(xiàn)程的數(shù)量。

完成之后,打印輸出失敗的次數(shù),以及開(kāi)始時(shí)間和結(jié)束時(shí)間,單位是毫秒。

主要是學(xué)習(xí)一下Python,僅供參考。

#!/usr/bin/python3

import sys, time, json, _thread
import http.client, urllib.parse

thread_count = 100  #并發(fā)數(shù)量
now_count = 0
error_count = 0
begin_time = ''

lock_obj = _thread.allocate()

def test_http_engine():
  global now_count
  global error_count
  global thread_count
  global begin_time
  conn = None
  if now_count == 0:
    begin_time = int(round(time.time() * 1000))
  try:
    conn = http.client.HTTPConnection("192.168.1.1", 80)
    conn.request('GET', '/')

    response = conn.getresponse()
    data = response.read()
    print (data)

    if json.dumps(response.status) != '200':
      error_count += 1;
      print ('error count: ' + str(error_count))

    sys.stdout.flush()
    now_count += 1
    if now_count == thread_count:
      print ('### error count: ' + str(error_count) + ' ###')
      print ('### begin time : ' + str(begin_time))
      print ('### end time  : ' + str(int(round(time.time() * 1000))))

  except Exception as e:
    print (e)
  finally:
    if conn:
      conn.close()

def test_thread_func():
  global now_count
  global lock_obj
  cnt = 0

  lock_obj.acquire()
  print ('')
  print ('=== Request: ' + str(now_count) + ' ===')

  cnt += 1
  test_http_engine()
  sys.stdout.flush()
  lock_obj.release()


def test_main():
  global thread_count
  for i in range(thread_count):
    _thread.start_new_thread(test_thread_func, ())

if __name__=='__main__':
  test_main()
  while True:
    time.sleep(5)

相關(guān)文章

最新評(píng)論

四子王旗| 马鞍山市| 伊吾县| 开化县| 观塘区| 九江县| 金坛市| 广宁县| 大新县| 筠连县| 浏阳市| 云霄县| 镇雄县| 冕宁县| 景德镇市| 牙克石市| 长乐市| 阜新市| 西安市| 台江县| 建平县| 屏山县| 保亭| 仁寿县| 休宁县| 平江县| 高陵县| 乌兰浩特市| 九台市| 枝江市| 安陆市| 古丈县| 建昌县| 清徐县| 怀仁县| 金湖县| 许昌县| 大庆市| 积石山| 东莞市| 南皮县|