简体   繁体   English

使用webscrapig对网站进行压力测试

[英]stress test a website with webscrapig

My friend wrote a website by node.js, and I want to help him to do the stress test just in case it crashed some day. 我的朋友通过node.js编写了一个网站,我想帮助他进行压力测试,以防万一崩溃。 I used beautifulsoup to test it, and it went well. 我用beautifulsoup进行了测试,效果很好。

Code: 码:

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) 

Instead of running 1000 times one by one in order, is there any other way to test it if I want to run it at the same time with 1000 codes by using python? 如果要使用python使用1000条代码同时运行它,是否有其他方法可以对其进行测试,而不是依次运行1000次? Thanks! 谢谢!

You can do it with threading . 您可以使用threading做到这一点。 But I don't recommend to create 1000 thread and run it at the same time. 但是我不建议创建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()

Use threads. 使用线程。 Open up n threads, and ping the server at the same time. 打开n线程,并同时ping服务器。 You can look into https://www.python-course.eu/threads.php 您可以查看https://www.python-course.eu/threads.php

But running threads is a CPU intensive task, so make sure you keep n controlled. 但是运行线程是一项占用大量CPU的任务,因此请确保将n保持受控状态。 Giving a higher value to n , could stress the client. n赋予更高的值可能会使客户感到压力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM