用Python實現的Web伺服器的壓力測試工具

2021-06-16 07:39:02 字數 2006 閱讀 6094

花了點時間用python寫了乙個網頁抓取的工具,用來做網頁的伺服器壓力測試。也可以模擬執行時的狀態。

# coding:gbk

import time

import urllib2

import threading

from queue import queue

from time import sleep

# 效能測試頁面

# 配置:壓力測試

#thread_num = 10 # 併發執行緒總數

#one_worker_num = 500 # 每個執行緒的迴圈次數

#loop_sleep = 0.01 # 每次請求時間間隔(秒)

# 配置:模擬執行狀態

thread_num = 100 # 併發執行緒總數

one_worker_num = 10000 # 每個執行緒的迴圈次數

loop_sleep = 0.5 # 每次請求時間間隔(秒)

# 出錯數

error_num = 0

#具體的處理函式,負責處理單個任務

def dowork(index):

t = threading.currentthread()

#print "["+t.name+" "+str(index)+"] "+perf_test_url

try:

html = urllib2.urlopen(perf_test_url).read()

except urllib2.urlerror, e:

print "["+t.name+" "+str(index)+"] "

print e

global error_num

error_num += 1

#這個是工作程序,負責不斷從佇列取資料並處理

def working():

t = threading.currentthread()

print "["+t.name+"] sub thread begin"

i = 0

while i < one_worker_num:

i += 1

dowork(i)

sleep(loop_sleep)

print "["+t.name+"] sub thread end"

def main():

#dowork(0)

#return

t1 = time.time()

threads =

# 建立執行緒

for i in range(thread_num):

t = threading.thread(target=working, name="t"+str(i))

t.setdaemon(true)

for t in threads:

t.start()

for t in threads:

t.join()

print "main thread end"

t2 = time.time()

print "****************************************"

print "url:", perf_test_url

print "任務數量:", thread_num, "*", one_worker_num, "=", thread_num*one_worker_num

print "總耗時(秒):", t2-t1

print "每次請求耗時(秒):", (t2-t1) / (thread_num*one_worker_num)

print "每秒承載請求數:", 1 / ((t2-t1) / (thread_num*one_worker_num))

print "錯誤數量:", error_num

if __name__ == "__main__": main()

用C 實現Web伺服器

www的工作基於客戶機 伺服器計算模型,由web 瀏覽器 客戶機 和web伺服器 伺服器 構成,兩者之間採用超文字傳送協議 http 進行通訊,http協議的作用原理包括四個步驟 連線,請求,應答。根據上述http協議的作用原理,本文實現了get請求的web伺服器程式的方法,通過建立tcpliste...

python實現簡單的web伺服器

最近有需求需要提供乙個簡單的 web 伺服器,用於客戶端上報一些內容,採用 post 方式上報並支援 gzip 壓縮,如下 python coding utf 8 上報資料用 gzip 壓縮了,所以用 http 伺服器接收資料並輸出接收到的資料 只儲存過去7天的日誌,按天建立新的日誌 接收上報的檔案...

Python實現簡單的WEB伺服器

使用mt7620n實現了wifi探針,並將得到的資料通過http post方式傳送到伺服器。為了測試http post介面,所以使用python實現乙個簡單的web伺服器。basehttpserver模組 處理get請求 defdo get self self.send response 200 s...