簡體   English   中英

使用webscrapig對網站進行壓力測試

[英]stress test a website with webscrapig

我的朋友通過node.js編寫了一個網站,我想幫助他進行壓力測試,以防萬一崩潰。 我用beautifulsoup進行了測試,效果很好。

碼:

for i in range(0,1000):
    #i=i+1
    l=randint(2008, 2018)
    first="year=%d" %l
    k=randint(1600, 2400)
    second="cc=%d" %k
    url="http://xx.xxx.xxx.xxx:xxxx/outputData?{0}&{1}&submit=Submit".format(first,second)
    res = requests.get(url)
    soup=BeautifulSoup(res.text,'lxml')
    print(soup) 

如果要使用python使用1000條代碼同時運行它,是否有其他方法可以對其進行測試,而不是依次運行1000次? 謝謝!

您可以使用threading做到這一點。 但是我不建議創建1000個線程並同時運行它。

import threading
def testit():
    l=randint(2008, 2018)
    first="year=%d" %l
    k=randint(1600, 2400)
    second="cc=%d" %k
    url="http://xx.xxx.xxx.xxx:xxxx/outputData?{0}&{1}&submit=Submit".format(first,second)
    res = requests.get(url)
    soup=BeautifulSoup(res.text,'lxml')
    print(soup) 

threads = [threading.Thread(target=testit) for i in range(1000)] # Not recommend to use 1000 here

for t in thread:
    t.start()

使用線程。 打開n線程,並同時ping服務器。 您可以查看https://www.python-course.eu/threads.php

但是運行線程是一項占用大量CPU的任務,因此請確保將n保持受控狀態。 n賦予更高的值可能會使客戶感到壓力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM