如何在Python中调用不同的URL进行负载测试?
我有一个针对任何特定url执行负载测试的代码。但我必须对具有不同URL的web服务进行负载测试。为此,我需要创建一个URL数组,每个线程都应该命中数组中给定的所有URL。我该怎么做?这是我的代码:如何在Python中调用不同的URL进行负载测试?,python,arrays,list,load,Python,Arrays,List,Load,我有一个针对任何特定url执行负载测试的代码。但我必须对具有不同URL的web服务进行负载测试。为此,我需要创建一个URL数组,每个线程都应该命中数组中给定的所有URL。我该怎么做?这是我的代码: import httplib2 import socket import time from threading import Event from threading import Thread from threading import current_thread from urllib
import httplib2
import socket
import time
from threading import Event
from threading import Thread
from threading import current_thread
from urllib import urlencode
# Modify these values to control how the testing is done
# How many threads should be running at peak load.
NUM_THREADS = 50
# How many minutes the test should run with all threads active.
TIME_AT_PEAK_QPS = 20 # minutes
# How many seconds to wait between starting threads.
# Shouldn't be set below 30 seconds.
DELAY_BETWEEN_THREAD_START = 30 # seconds
quitevent = Event()
def threadproc():
"""This function is executed by each thread."""
print "Thread started: %s" % current_thread().getName()
h = httplib2.Http(timeout=30)
while not quitevent.is_set():
try:
# HTTP requests to exercise the server go here
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
resp, content = h.request(
"http://www.google.com")
if resp.status != 200:
print "Response not OK"
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
except socket.timeout:
pass
print "Thread finished: %s" % current_thread().getName()
if __name__ == "__main__":
runtime = (TIME_AT_PEAK_QPS * 60 + DELAY_BETWEEN_THREAD_START * NUM_THREADS)
print "Total runtime will be: %d seconds" % runtime
threads = []
try:
for i in range(NUM_THREADS):
t = Thread(target=threadproc)
t.start()
threads.append(t)
time.sleep(DELAY_BETWEEN_THREAD_START)
print "All threads running"
time.sleep(TIME_AT_PEAK_QPS*60)
print "Completed full time at peak qps, shutting down threads"
except:
print "Exception raised, shutting down threads"
quitevent.set()
time.sleep(3)
for t in threads:
t.join(1.0)
print "Finished"
扩展类,而不是将
threadproc
传递给Thread
:
class Worker(Thread):
def __init__(self, urls):
super(Worker, self).__init__()
self.urls = urls
def run(self):
for url in self.urls:
self.fetch(url)
也就是说,除非您这样做是为了更好地理解线程和负载测试如何在内部工作,否则我建议您使用一个成熟的测试框架,比如。多年的经验积累了下来,你必须先积累起来。你还没有解释你所拥有的东西有什么问题。你有什么错误吗?结果是否与预期不符?如果您真的要对服务器进行负载测试,我建议您使用专门为此目的设计的工具。我成功使用的一个是开源ApacheJMeter()。我还建议使用tsung进行负载测试()。事实上,我是python的初学者,您解释的类很容易理解,但我不知道在代码中我必须提到针对该负载测试执行的所有URL。您只能看到code resp中提到的一个url,content=h.request(“),但我如何将其分配给多个url?将
threadproc
移动到新类Worker
,为其分配参数self,url
,然后将h.request(“google.com”)
替换为h.request(url)
Thanx,但我必须在其中定义URL数组。例如example.com、example.com/1、example.com/2。。。。