brianthelion

lat_test.py

Aug 7th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import zmq
  4. import argparse
  5. import time
  6. import random
  7. from multiprocessing import Process
  8. import sys
  9. import simplejson as json
  10. import string
  11.  
  12. def source(outaddr,fps) :
  13.    
  14.     socket = zmq.Context(1).socket(zmq.PUB)
  15.     socket.setsockopt(zmq.SNDHWM,1)
  16.     socket.bind(outaddr)
  17.     count = 0
  18.     while True :
  19.         time.sleep(1.0/fps)
  20.         outtime = time.time()
  21.         socket.send( json.dumps( {'route':[],'srctime':outtime,'transtime':outtime,'count':count,'lat':0} ) )
  22.         count += 1
  23.  
  24. def node(inaddr,outaddr,params) :
  25.    
  26.     context = zmq.Context(1)
  27.     sender = context.socket(zmq.PUB)
  28.  
  29.     sender.setsockopt(zmq.RCVHWM,1)
  30.     sender.bind(outaddr)
  31.            
  32.     receiver = context.socket(zmq.SUB)
  33.     receiver.setsockopt(zmq.SNDHWM,1)
  34.     receiver.setsockopt(zmq.SUBSCRIBE,'')
  35.     receiver.connect(inaddr)
  36.        
  37.     while True :
  38.        
  39.         adict = json.loads( receiver.recv() )
  40.         adict['lat'] += time.time() - adict['transtime']
  41.         adict['route'].append('Node%s'%str(params))
  42.         fps = random.normalvariate(*params)
  43.         if fps < 0 : fps = params[0]
  44.         time.sleep( 1.0/fps )
  45.         adict['transtime'] = time.time()
  46.         sender.send( json.dumps(adict) )
  47.  
  48. def sink(inaddrs) :
  49.    
  50.     context = zmq.Context(1)
  51.     socket = context.socket(zmq.SUB)
  52.     socket.setsockopt(zmq.RCVHWM,1)
  53.     socket.setsockopt(zmq.SUBSCRIBE,'')
  54.     for addr in inaddrs : socket.connect(addr)
  55.    
  56.     while True :
  57.        
  58.         adict = json.loads( socket.recv() )
  59.         current_time = time.time()
  60.         adict['lat'] += current_time - adict['transtime']
  61.         sys.stdout.write( '%r\t%r\t%s\t%s\t%r\n' % (current_time, adict['srctime'], adict['count'], adict['route'], adict['lat']) )
  62.         sys.stdout.flush()
  63.  
  64. def randaddr() : return 49152 + random.randrange(15000)
  65.  
  66. def main() :
  67.  
  68.     desc='''Builds a 1-source/N-node/1-sink network using zmq PUB/SUB sockets.
  69.    Tracks *transmission* -- not processing -- latency through the
  70.    network. Writes the latency data to stdout for further processing.
  71.    To be used with plot_lat.py.
  72.    
  73.    Multiple -n arguments can be given:
  74.    lat_test.py SECONDS -n 33 12 -n 60 1     # creates a two-node network with
  75.    the first node's mean framerate set to 33, variance of 12 fps; the second
  76.    node has mean framerate 60, variance of 1 fps.
  77.    
  78.    If no -n arguments are given, the default is:
  79.    -n 33 12 -n 33 10 -n 33 9 -n 33 8 -n 33 7
  80.    '''
  81.    
  82.     parser = argparse.ArgumentParser(description=desc)
  83.     parser.add_argument('seconds',type=float)
  84.     parser.add_argument('-t','--socktype',default='tcp')
  85.     parser.add_argument('-f','--fps',type=float,default=30.0)
  86.     parser.add_argument('-n','--node', nargs='+', type=float, dest='nodes', action='append')
  87.     ns = parser.parse_args()
  88.    
  89.     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]]
  90.    
  91.     addr_template = string.Template('${prot}://${prefix}${num}')
  92.  
  93.     prefix = None    
  94.     if ns.socktype == 'tcp' : prefix = '*:'
  95.     elif ns.socktype == 'ipc' : prefix = '/dev/shm/'
  96.     else : raise NotImplementedError()
  97.    
  98.     addr_template = string.Template( addr_template.safe_substitute(prot=ns.socktype,prefix=prefix) )
  99.    
  100.     addrs = []
  101.     while len(addrs) < len(ns.nodes) + 1 :
  102.         new_addr = randaddr()
  103.         if new_addr not in addrs : addrs.append(new_addr)
  104.    
  105.     src_addr = addr_template.substitute(num=addrs.pop())
  106.     node_addrs = [ addr_template.substitute(num=a) for a in addrs ]
  107.    
  108.     snkproc = Process( target=sink, args=(node_addrs,) )
  109.     snkproc.start()
  110.     time.sleep(1)
  111.    
  112.     nodeprocs = [ Process( target=node,args=(src_addr,a,n) ) for n,a in zip(ns.nodes,node_addrs) ]
  113.     for p in nodeprocs : p.start()
  114.     time.sleep(1)
  115.    
  116.     srcproc = Process( target=source, args=(src_addr,ns.fps) )
  117.     srcproc.start()
  118.     time.sleep(ns.seconds)
  119.    
  120.     snkproc.terminate()
  121.     for p in nodeprocs : p.terminate()
  122.     srcproc.terminate()
  123.        
  124. if __name__ == '__main__' : main()
  125. else : pass
Advertisement
Add Comment
Please, Sign In to add comment