Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import zmq
- import argparse
- import time
- import random
- from multiprocessing import Process
- import sys
- import simplejson as json
- import string
- def source(outaddr,fps) :
- socket = zmq.Context(1).socket(zmq.PUB)
- socket.setsockopt(zmq.SNDHWM,1)
- socket.bind(outaddr)
- count = 0
- while True :
- time.sleep(1.0/fps)
- outtime = time.time()
- socket.send( json.dumps( {'route':[],'srctime':outtime,'transtime':outtime,'count':count,'lat':0} ) )
- count += 1
- def node(inaddr,outaddr,params) :
- context = zmq.Context(1)
- sender = context.socket(zmq.PUB)
- sender.setsockopt(zmq.RCVHWM,1)
- sender.bind(outaddr)
- receiver = context.socket(zmq.SUB)
- receiver.setsockopt(zmq.SNDHWM,1)
- receiver.setsockopt(zmq.SUBSCRIBE,'')
- receiver.connect(inaddr)
- while True :
- adict = json.loads( receiver.recv() )
- adict['lat'] += time.time() - adict['transtime']
- adict['route'].append('Node%s'%str(params))
- fps = random.normalvariate(*params)
- if fps < 0 : fps = params[0]
- time.sleep( 1.0/fps )
- adict['transtime'] = time.time()
- sender.send( json.dumps(adict) )
- def sink(inaddrs) :
- context = zmq.Context(1)
- socket = context.socket(zmq.SUB)
- socket.setsockopt(zmq.RCVHWM,1)
- socket.setsockopt(zmq.SUBSCRIBE,'')
- for addr in inaddrs : socket.connect(addr)
- while True :
- adict = json.loads( socket.recv() )
- current_time = time.time()
- adict['lat'] += current_time - adict['transtime']
- sys.stdout.write( '%r\t%r\t%s\t%s\t%r\n' % (current_time, adict['srctime'], adict['count'], adict['route'], adict['lat']) )
- sys.stdout.flush()
- def randaddr() : return 49152 + random.randrange(15000)
- def main() :
- desc='''Builds a 1-source/N-node/1-sink network using zmq PUB/SUB sockets.
- Tracks *transmission* -- not processing -- latency through the
- network. Writes the latency data to stdout for further processing.
- To be used with plot_lat.py.
- Multiple -n arguments can be given:
- lat_test.py SECONDS -n 33 12 -n 60 1 # creates a two-node network with
- the first node's mean framerate set to 33, variance of 12 fps; the second
- node has mean framerate 60, variance of 1 fps.
- If no -n arguments are given, the default is:
- -n 33 12 -n 33 10 -n 33 9 -n 33 8 -n 33 7
- '''
- parser = argparse.ArgumentParser(description=desc)
- parser.add_argument('seconds',type=float)
- parser.add_argument('-t','--socktype',default='tcp')
- parser.add_argument('-f','--fps',type=float,default=30.0)
- parser.add_argument('-n','--node', nargs='+', type=float, dest='nodes', action='append')
- ns = parser.parse_args()
- if ns.nodes is None : ns.nodes = [[33.0,12.0],[33.0,10.0],[33.0,9.0],[33.0,8.0],[33.0,7.0]]
- addr_template = string.Template('${prot}://${prefix}${num}')
- prefix = None
- if ns.socktype == 'tcp' : prefix = '*:'
- elif ns.socktype == 'ipc' : prefix = '/dev/shm/'
- else : raise NotImplementedError()
- addr_template = string.Template( addr_template.safe_substitute(prot=ns.socktype,prefix=prefix) )
- addrs = []
- while len(addrs) < len(ns.nodes) + 1 :
- new_addr = randaddr()
- if new_addr not in addrs : addrs.append(new_addr)
- src_addr = addr_template.substitute(num=addrs.pop())
- node_addrs = [ addr_template.substitute(num=a) for a in addrs ]
- snkproc = Process( target=sink, args=(node_addrs,) )
- snkproc.start()
- time.sleep(1)
- nodeprocs = [ Process( target=node,args=(src_addr,a,n) ) for n,a in zip(ns.nodes,node_addrs) ]
- for p in nodeprocs : p.start()
- time.sleep(1)
- srcproc = Process( target=source, args=(src_addr,ns.fps) )
- srcproc.start()
- time.sleep(ns.seconds)
- snkproc.terminate()
- for p in nodeprocs : p.terminate()
- srcproc.terminate()
- if __name__ == '__main__' : main()
- else : pass
Advertisement
Add Comment
Please, Sign In to add comment