用Python編寫WEB伺服器壓力測試工具

2021-04-06 19:08:53 字數 2043 閱讀 1055

# code by 李嘉

# 不對因使用**產生任何後果負任何責任

# 需要測試的 url 列表,每一次的訪問,我們隨機取乙個

urls = [

"/test?page=",

"/test2?orderby=a&page=",

"/test2?orderby=d&page=",

]max_page = 10000

server_name = "192.168.0.64:80"

test_count = 10000

# 建立乙個 threading.thread 的派生類

class requestthread(threading.thread):

# 建構函式

def __init__(self, thread_name):

threading.thread.__init__(self)

self.test_count = 0

# 執行緒執行的入口函式

def run(self):

# 不直接把**寫在run裡面是因為也許我們還要做其他形式的測試

# 模擬 keep-alive 的訪問, http 1.1

for i in range(0, random.randint(0, 100)):

# 構造乙個 url,提供隨機引數的能力

url = urls[random.randint(0, len(urls) - 1)];

url += str(random.randint(0, max_page))

# 這就連線到伺服器上去

#print url

try:

conn.request("get", url)

rsps = conn.getresponse()

if rsps.status == 200:

# 讀取返回的資料

data = rsps.read()

self.test_count += 1

except:

continue

conn.close()

# main **開始

# 開始的時間

start_time = time.time()

threads =

# 併發的執行緒數

thread_count = 100

i = 0

while i < thread_count:

t = requestthread("thread" + str(i))

t.start()

i += 1

# 接受統計的命令

word = ""

while true:

word = raw_input("cmd:")

if word == "s":

time_span = time.time() - start_time

all_count = 0

for t in threads:

all_count += t.test_count

print "%s request/second" % str(all_count / time_span)

elif word == "e":

# 準備退出 其實 x 掉 視窗更加容易,沒什麼浪費的資源

用Python編寫WEB伺服器壓力測試工具

code by 李嘉 不對因使用 產生任何後果負任何責任 需要測試的 url 列表,每一次的訪問,我們隨機取乙個 urls test?page test2?orderby a page test2?orderby d page max page 10000 server name 192.168.0...

用python編寫乙個web靜態伺服器

建立web類 def init self 建立server物件 self.server socket.socket socket.af inet,socket.sock stream 重複使用繫結的資訊 self.server.setsockopt socket.sol socket,socket....

編寫簡單多執行緒web伺服器

編寫簡單多執行緒web伺服器 刺蝟 http blog.csdn.net littlehedgehog 下面的原理解釋參照 乙個多執行緒web伺服器例項 c,linux,詳細的web伺服器原理 一文 原理 在瀏覽器中輸入乙個 回車之後,瀏覽器會向相應主機的相應埠傳送一段報文,如果是http協議的 如...