Added a printout for the overall test execution time
[integration/test.git] / test / tools / odl-mdsal-clustering-tests / clustering-performance-test / inventory_perf.py
1 __author__ = "Jan Medved"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "jmedved@cisco.com"
5
6 import argparse
7 import requests
8 import time
9 import threading
10
11 class Counter(object):
12     def __init__(self, start=0):
13         self.lock = threading.Lock()
14         self.value = start
15     def increment(self, value=1):
16         self.lock.acquire()
17         try:
18             self.value = self.value + value
19         finally:
20             self.lock.release()
21
22
23 class Timer(object):
24     def __init__(self, verbose=False):
25         self.verbose = verbose
26
27     def __enter__(self):
28         self.start = time.time()
29         return self
30
31     def __exit__(self, *args):
32         self.end = time.time()
33         self.secs = self.end - self.start
34         self.msecs = self.secs * 1000  # millisecs
35         if self.verbose:
36             print ("elapsed time: %f ms" % self.msecs)
37
38 # Initialize the totals over all threads
39 total_requests = Counter(0)
40 total_req_rate = Counter(0.0)
41
42 total_mbytes = Counter(0.0)
43 total_mb_rate = Counter(0.0)
44
45 putheaders = {'content-type': 'application/json'}
46 getheaders = {'Accept': 'application/json'}
47
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'
50
51 num_threads = 1
52
53 print_lock = threading.Lock()
54
55
56 def get_inventory(tnum, url, hdrs, rnum, cond):
57     """
58
59     :param tnum:
60     :param url:
61     :param hdrs:
62     :param rnum:
63     :param cond:
64     :return:
65     """
66     total_len = float(0)
67     results = {}
68
69     with print_lock:
70         print 'Thread %d: Getting %s' % (tnum, url)
71
72     s = requests.Session()
73     with Timer() as t:
74         for i in range(rnum):
75             r = s.get(url, headers=hdrs, stream=False )
76             total_len += len(r.content)
77
78             try:
79                 results[r.status_code] += 1
80             except(KeyError):
81                 results[r.status_code] = 1
82
83     total = sum(results.values())
84     rate = total/t.secs
85     total_requests.increment(total)
86     total_req_rate.increment(rate)
87
88     mbytes = total_len / (1024*1024)
89     mrate = mbytes/t.secs
90     total_mbytes.increment(mbytes)
91     total_mb_rate.increment(mrate)
92
93     with print_lock:
94         print '\nThread %d: ' % tnum
95         print '    Elapsed time: %.2f,' % t.secs
96         print '    Requests: %d, Requests/sec: %.2f' % (total, rate)
97         print '    Volume: %.2f MB, Rate: %.2f MByte/s' % (mbytes, mrate)
98         print '    Results: ',
99         print results
100
101     with cond:
102         cond.notifyAll()
103
104
105 if __name__ == "__main__":
106
107     parser = argparse.ArgumentParser(description='Restconf test program')
108     parser.add_argument('--odlhost', default='127.0.0.1', help='host where '
109                         'odl controller is running (default is 127.0.0.1)')
110     parser.add_argument('--odlport', default='8080', help='port on '
111                         'which odl\'s RESTCONF is listening (default is 8080)')
112     parser.add_argument('--requests', type=int, default=10, help='number of '
113                         'requests to send')
114     parser.add_argument('--url', default='restconf/operational/opendaylight-inventory:nodes',
115                         help='Url to send.')
116     parser.add_argument('--nthreads', type=int, default=1,
117                         help='Number of request worker threads, default=1')
118     in_args = parser.parse_args()
119
120     url = 'http://' + in_args.odlhost + ":" + in_args.odlport + '/' + in_args.url
121
122     threads = []
123     nthreads = int(in_args.nthreads)
124     cond = threading.Condition()
125
126     for i in range(nthreads):
127         t = threading.Thread(target=get_inventory,
128                              args=(i,url, getheaders, int(in_args.requests), cond))
129         threads.append(t)
130         t.start()
131
132     finished = 0
133     while finished < nthreads:
134         with cond:
135             cond.wait()
136             finished = finished + 1
137
138     print '\nAggregate requests: %d, Aggregate requests/sec: %.2f' % (total_requests.value,
139                                                                     total_req_rate.value)
140     print 'Aggregate Volume: %.2f MB, Aggregate Rate: %.2f MByte/s' % (total_mbytes.value,
141                                                                        total_mb_rate.value)
142
143 #    get_inventory(url, getheaders, int(in_args.requests))
144
145 #    get_inventory(N1T0_URL, getheaders, 100)