7 __author__ = "Jan Medved"
8 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
9 __license__ = "New-style BSD"
10 __email__ = "jmedved@cisco.com"
13 class Counter(object):
14 def __init__(self, start=0):
15 self.lock = threading.Lock()
18 def increment(self, value=1):
21 self.value = self.value + value
27 def __init__(self, verbose=False):
28 self.verbose = verbose
31 self.start = time.time()
34 def __exit__(self, *args):
35 self.end = time.time()
36 self.secs = self.end - self.start
37 self.msecs = self.secs * 1000 # millisecs
39 print("elapsed time: %f ms" % self.msecs)
42 # Initialize the totals over all threads
43 total_requests = Counter(0)
44 total_req_rate = Counter(0.0)
46 total_mbytes = Counter(0.0)
47 total_mb_rate = Counter(0.0)
49 putheaders = {'content-type': 'application/json'}
50 getheaders = {'Accept': 'application/json'}
52 INVENTORY_URL = 'http://localhost:8080/restconf/operational/opendaylight-inventory:nodes'
53 N1T0_URL = 'http://localhost:8080/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/table/0'
57 print_lock = threading.Lock()
60 def get_inventory(tnum, url, hdrs, rnum, cond):
74 print('Thread %d: Getting %s' % (tnum, url))
76 s = requests.Session()
79 r = s.get(url, headers=hdrs, stream=False)
80 total_len += len(r.content)
83 results[r.status_code] += 1
85 results[r.status_code] = 1
87 total = sum(results.values())
89 total_requests.increment(total)
90 total_req_rate.increment(rate)
92 mbytes = total_len / (1024 * 1024)
93 mrate = mbytes / t.secs
94 total_mbytes.increment(mbytes)
95 total_mb_rate.increment(mrate)
98 print('\nThread %d: ' % tnum)
99 print(' Elapsed time: %.2f,' % t.secs)
100 print(' Requests: %d, Requests/sec: %.2f' % (total, rate))
101 print(' Volume: %.2f MB, Rate: %.2f MByte/s' % (mbytes, mrate))
109 if __name__ == "__main__":
111 parser = argparse.ArgumentParser(description='Restconf test program')
112 parser.add_argument('--odlhost', default='127.0.0.1', help='host where '
113 'odl controller is running (default is 127.0.0.1)')
114 parser.add_argument('--odlport', default='8080', help='port on '
115 'which odl\'s RESTCONF is listening (default is 8080)')
116 parser.add_argument('--requests', type=int, default=10, help='number of '
118 parser.add_argument('--url', default='restconf/operational/opendaylight-inventory:nodes',
120 parser.add_argument('--nthreads', type=int, default=1,
121 help='Number of request worker threads, default=1')
122 in_args = parser.parse_args()
124 url = 'http://' + in_args.odlhost + ":" + in_args.odlport + '/' + in_args.url
127 nthreads = int(in_args.nthreads)
128 cond = threading.Condition()
130 for i in range(nthreads):
131 t = threading.Thread(target=get_inventory,
132 args=(i, url, getheaders, int(in_args.requests), cond))
137 while finished < nthreads:
140 finished = finished + 1
142 print('\nAggregate requests: %d, Aggregate requests/sec: %.2f' % (total_requests.value,
143 total_req_rate.value))
144 print('Aggregate Volume: %.2f MB, Aggregate Rate: %.2f MByte/s' % (total_mbytes.value,
145 total_mb_rate.value))
147 # get_inventory(url, getheaders, int(in_args.requests))
149 # get_inventory(N1T0_URL, getheaders, 100)