【发布时间】:2015-03-27 08:50:06
【问题描述】:
对于我的大学项目,我正在尝试开发一个基于 python 的流量生成器。我在 vmware 上创建了 2 台 CentOS 机器,我使用 1 作为我的客户端和 1 作为我的服务器机器。我使用 IP 别名技术来增加客户端和服务器的数量,只使用单个客户端/服务器机器。到目前为止,我已经在我的客户端机器上创建了 50 个 IP 别名,在我的服务器机器上创建了 10 个 IP 别名。我还使用多处理模块同时生成从所有 50 个客户端到所有 10 个服务器的流量。我还在我的服务器上开发了一些配置文件(1kb、10kb、50kb、100kb、500kb、1mb)(在 /var/www/html 目录中,因为我使用的是 Apache 服务器)并且我正在使用 urllib2 向这些配置文件发送请求我的客户端机器。在这里,当我监视 TCP 连接数时运行我的脚本时,它总是
'''
Traffic Generator Script:
Here I have used IP Aliasing to create multiple clients on single vm machine.
Same I have done on server side to create multiple servers. I have around 50 clients and 10 servers
'''
import multiprocessing
import urllib2
import random
import myurllist #list of all destination urls for all 10 servers
import time
import socbindtry #script that binds various virtual/aliased client ips to the script
response_time=[] #some shared variables
error_count=multiprocessing.Value('i',0)
def send_request3(): #function to send requests from alias client ip 1
opener=urllib2.build_opener(socbindtry.BindableHTTPHandler3) #bind to alias client ip1
try:
tstart=time.time()
for i in range(myurllist.url):
x=random.choice(myurllist.url[i])
opener.open(x).read()
print "file downloaded:",x
response_time.append(time.time()-tstart)
except urllib2.URLError, e:
error_count.value=error_count.value+1
def send_request4(): #function to send requests from alias client ip 2
opener=urllib2.build_opener(socbindtry.BindableHTTPHandler4) #bind to alias client ip2
try:
tstart=time.time()
for i in range(myurllist.url):
x=random.choice(myurllist.url[i])
opener.open(x).read()
print "file downloaded:",x
response_time.append(time.time()-tstart)
except urllib2.URLError, e:
error_count.value=error_count.value+1
#50 such functions are defined here for 50 clients
process=[]
def func():
global process
process.append(multiprocessing.Process(target=send_request3))
process.append(multiprocessing.Process(target=send_request4))
process.append(multiprocessing.Process(target=send_request5))
process.append(multiprocessing.Process(target=send_request6))
#append 50 functions here
for i in range(len(process)):
process[i].start()
for i in range(len(process)):
process[i].join()
print"All work Done..!!"
return
start=float(time.time())
func()
end=float(time.time())-start
print end
【问题讨论】:
-
你真的写了(几乎)相同的代码 50 次?
-
代码不是“完整的”(缩进...),但据我所知,您创建了 50 个进程,每个进程一次执行 1 次下载。如果这是正确的,那么您显然只有 N
-
再补充一点:有类似 httperf [ hpl.hp.com/research/linux/httperf ] 的工具可用于此目的。
-
@KlausD。是的,我不得不...因为每次我使用不同的 ip 发送请求。我不能将 httpref 显示为我的大学项目
-
@SylvainLeroux 是的,你说的是正确的。但我想增加 TCP 连接数。
标签: python centos urllib2 python-multiprocessing