# coding=utf-8
import urllib
import urllib2
import cookielib
import threading
import time
from Queue import Queue
from time import sleep
THREAD_NUM = 100 # 并发线程总数
ONE_WORKER_NUM = 10000 # 每个线程的循环次数
LOOP_SLEEP = 0.5 # 每次请求时间间隔(秒)
# 出错数
ERROR_NUM = 0
requrl = "http://cs.dachuizichan.com/login.do"
test_data = {
'loginid':'17326800227',
'password':'111111',
'rand':'1111'
}
test_data_urlencode = urllib.urlencode(test_data)
header = {
"Host" : "cs.dachuizichan.com",
"Referer" : "http://cs.dachuizichan.com/manage/frame.do",
"User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/54.0.2840.71 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded"
}
cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
req = urllib2.Request(requrl, test_data_urlencode,header)
result = opener.open(req)
# for key in header:
# req.add_header(key, header[key])
def doWork(index):
t = threading.currentThread()
try:
result2 = opener.open("http://cs.dachuizichan.com/manage/myDebtor/list.do")
print result2.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)
Threads.append(t)
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()