python 仿ab网站压力测试

测试代码如下:

 

# -*- coding: UTF-8 -*-

from gevent.monkey import patch_all;patch_all()
from multiprocessing import Process,cpu_count
import urllib2,gevent,random,sys
import cookielib
def start():

    url = "这里填写你的网站"

    user_agent = ["Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                  "Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13",
                  "Mozilla/5.0 (iP  hone; U; Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3",
                  "Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 ",
                  "Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12"
                  ]

    while 1:

        try:
            User_Agent = random.choice(user_agent)
            cookie = cookielib.CookieJar()
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
            request = urllib2.Request(url)
            request.add_header('User-Agent',User_Agent)
            opener.open(request)
        except :
            #print "unknow error"
            pass

        #解除下面注释将减少压力,保持下面注释将增加网站压力
        gevent.sleep(0)


def main():
    g_list = []
    for _ in range(1000):
        g_list.append(gevent.spawn(start))
    gevent.joinall(g_list)

if __name__ == '__main__':
    try:
        for i in range(cpu_count()):
            p = Process(target=main)
            p.start()
    except:
        sys.exit(0)

 

 

模拟CPU数量的并发,保证充足的CPU利用率!

你可能感兴趣的:(python 仿ab网站压力测试)