1 __author__ = "Jan Medved"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "jmedved@cisco.com"
11 class Counter(object):
12 def __init__(self, start=0):
13 self.lock = threading.Lock()
15 def increment(self, value=1):
18 self.value = self.value + value
24 def __init__(self, verbose=False):
25 self.verbose = verbose
28 self.start = time.time()
31 def __exit__(self, *args):
32 self.end = time.time()
33 self.secs = self.end - self.start
34 self.msecs = self.secs * 1000 # millisecs
36 print ("elapsed time: %f ms" % self.msecs)
38 # Initialize the totals over all threads
39 total_requests = Counter(0)
40 total_req_rate = Counter(0.0)
42 total_mbytes = Counter(0.0)
43 total_mb_rate = Counter(0.0)
45 putheaders = {'content-type': 'application/json'}
46 getheaders = {'Accept': 'application/json'}
48 INVENTORY_URL = 'http://localhost:8080/restconf/operational/opendaylight-inventory:nodes'
49 N1T0_URL = 'http://localhost:8080/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/table/0'
53 print_lock = threading.Lock()
56 def get_inventory(tnum, url, hdrs, rnum, cond):
61 print 'Thread %d: Getting %s' % (tnum, url)
63 s = requests.Session()
66 r = s.get(url, headers=hdrs, stream=False )
67 total_len += len(r.content)
70 results[r.status_code] += 1
72 results[r.status_code] = 1
74 total = sum(results.values())
76 total_requests.increment(total)
77 total_req_rate.increment(rate)
79 mbytes = total_len / (1024*1024)
81 total_mbytes.increment(mbytes)
82 total_mb_rate.increment(mrate)
85 print '\nThread %d: ' % tnum
86 print ' Elapsed time: %.2f,' % t.secs
87 print ' Requests: %d, Requests/sec: %.2f' % (total, rate)
88 print ' Volume: %.2f MB, Rate: %.2f MByte/s' % (mbytes, mrate)
96 if __name__ == "__main__":
98 parser = argparse.ArgumentParser(description='Restconf test program')
99 parser.add_argument('--odlhost', default='127.0.0.1', help='host where '
100 'odl controller is running (default is 127.0.0.1)')
101 parser.add_argument('--odlport', default='8080', help='port on '
102 'which odl\'s RESTCONF is listening (default is 8080)')
103 parser.add_argument('--requests', type=int, default=10, help='number of '
105 parser.add_argument('--url', default='restconf/operational/opendaylight-inventory:nodes',
107 parser.add_argument('--nthreads', type=int, default=1,
108 help='Number of request worker threads, default=1')
109 in_args = parser.parse_args()
111 url = 'http://' + in_args.odlhost + ":" + in_args.odlport + '/' + in_args.url
114 nthreads = int(in_args.nthreads)
115 cond = threading.Condition()
117 for i in range(nthreads):
118 t = threading.Thread(target=get_inventory,
119 args=(i,url, getheaders, int(in_args.requests), cond))
124 while finished < nthreads:
127 finished = finished + 1
129 print '\nAggregate requests: %d, Aggregate requests/sec: %.2f' % (total_requests.value,
130 total_req_rate.value)
131 print 'Aggregate Volume: %.2f MB, Aggregate Rate: %.2f MByte/s' % (total_mbytes.value,
134 # get_inventory(url, getheaders, int(in_args.requests))
136 # get_inventory(N1T0_URL, getheaders, 100)