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