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