Auto-generated patch by python-black
[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
42 # Initialize the totals over all threads
43 total_requests = Counter(0)
44 total_req_rate = Counter(0.0)
45
46 total_mbytes = Counter(0.0)
47 total_mb_rate = Counter(0.0)
48
49 putheaders = {"content-type": "application/json"}
50 getheaders = {"Accept": "application/json"}
51
52 INVENTORY_URL = (
53     "http://localhost:8080/restconf/operational/opendaylight-inventory:nodes"
54 )
55 N1T0_URL = "http://localhost:8080/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/table/0"
56
57 num_threads = 1
58
59 print_lock = threading.Lock()
60
61
62 def get_inventory(tnum, url, hdrs, rnum, cond):
63     """
64
65     :param tnum:
66     :param url:
67     :param hdrs:
68     :param rnum:
69     :param cond:
70     :return:
71     """
72     total_len = float(0)
73     results = {}
74
75     with print_lock:
76         print("Thread %d: Getting %s" % (tnum, url))
77
78     s = requests.Session()
79     with Timer() as t:
80         for i in range(rnum):
81             r = s.get(url, headers=hdrs, stream=False)
82             total_len += len(r.content)
83
84             try:
85                 results[r.status_code] += 1
86             except (KeyError):
87                 results[r.status_code] = 1
88
89     total = sum(results.values())
90     rate = total / t.secs
91     total_requests.increment(total)
92     total_req_rate.increment(rate)
93
94     mbytes = total_len / (1024 * 1024)
95     mrate = mbytes / t.secs
96     total_mbytes.increment(mbytes)
97     total_mb_rate.increment(mrate)
98
99     with print_lock:
100         print("\nThread %d: " % tnum)
101         print("    Elapsed time: %.2f," % t.secs)
102         print("    Requests: %d, Requests/sec: %.2f" % (total, rate))
103         print("    Volume: %.2f MB, Rate: %.2f MByte/s" % (mbytes, mrate))
104         print("    Results: ")
105         print(results)
106
107     with cond:
108         cond.notifyAll()
109
110
111 if __name__ == "__main__":
112
113     parser = argparse.ArgumentParser(description="Restconf test program")
114     parser.add_argument(
115         "--odlhost",
116         default="127.0.0.1",
117         help="host where " "odl controller is running (default is 127.0.0.1)",
118     )
119     parser.add_argument(
120         "--odlport",
121         default="8080",
122         help="port on " "which odl's RESTCONF is listening (default is 8080)",
123     )
124     parser.add_argument(
125         "--requests", type=int, default=10, help="number of " "requests to send"
126     )
127     parser.add_argument(
128         "--url",
129         default="restconf/operational/opendaylight-inventory:nodes",
130         help="Url to send.",
131     )
132     parser.add_argument(
133         "--nthreads",
134         type=int,
135         default=1,
136         help="Number of request worker threads, default=1",
137     )
138     in_args = parser.parse_args()
139
140     url = "http://" + in_args.odlhost + ":" + in_args.odlport + "/" + in_args.url
141
142     threads = []
143     nthreads = int(in_args.nthreads)
144     cond = threading.Condition()
145
146     for i in range(nthreads):
147         t = threading.Thread(
148             target=get_inventory, args=(i, url, getheaders, int(in_args.requests), cond)
149         )
150         threads.append(t)
151         t.start()
152
153     finished = 0
154     while finished < nthreads:
155         with cond:
156             cond.wait()
157             finished = finished + 1
158
159     print(
160         "\nAggregate requests: %d, Aggregate requests/sec: %.2f"
161         % (total_requests.value, total_req_rate.value)
162     )
163     print(
164         "Aggregate Volume: %.2f MB, Aggregate Rate: %.2f MByte/s"
165         % (total_mbytes.value, total_mb_rate.value)
166     )
167
168 #    get_inventory(url, getheaders, int(in_args.requests))
169
170 #    get_inventory(N1T0_URL, getheaders, 100)