fixing pep8 problems for test verify tox job
[integration/test.git] / tools / odl-mdsal-clustering-tests / clustering-performance-test / onos_tester.py
index ee81c0380ffbc83945f52908e15b7fe6bb03c9b9..0e00655dfef3b305f635b2f8c8afb21cee784716 100644 (file)
@@ -38,6 +38,11 @@ flow_template = {
     }
 }
 
+flow_delete_template = {
+    "deviceId": "of:0000000000000001",
+    "flowId": 21392098393151996
+}
+
 
 class Timer(object):
     def __init__(self, verbose=False):
@@ -52,7 +57,7 @@ class Timer(object):
         self.secs = self.end - self.start
         self.msecs = self.secs * 1000  # millisecs
         if self.verbose:
-            print ("elapsed time: %f ms" % self.msecs)
+            print("elapsed time: %f ms" % self.msecs)
 
 
 class Counter(object):
@@ -85,13 +90,15 @@ def _prepare_post(cntl, method, flows, template=None):
     Returns:
         :returns req: http request object
     """
-    fl1 = flows[0]
-    dev_id, ip = fl1
-    url = 'http://' + cntl + ':' + '8181/onos/v1/flows/' + dev_id
-    flow = copy.deepcopy(template)
-    flow["deviceId"] = dev_id
-    flow["selector"]["criteria"][1]["ip"] = '%s/32' % str(netaddr.IPAddress(ip))
-    req_data = json.dumps(flow)
+    flow_list = []
+    for dev_id, ip in (flows):
+        flow = copy.deepcopy(template)
+        flow["deviceId"] = dev_id
+        flow["selector"]["criteria"][1]["ip"] = '%s/32' % str(netaddr.IPAddress(ip))
+        flow_list.append(flow)
+    body = {"flows": flow_list}
+    url = 'http://' + cntl + ':' + '8181/onos/v1/flows/'
+    req_data = json.dumps(body)
     req = requests.Request(method, url, headers={'Content-Type': 'application/json'},
                            data=req_data, auth=('onos', 'rocks'))
     return req
@@ -112,10 +119,17 @@ def _prepare_delete(cntl, method, flows, template=None):
     Returns:
         :returns req: http request object
     """
-    fl1 = flows[0]
-    dev_id, flow_id = fl1
-    url = 'http://' + cntl + ':' + '8181/onos/v1/flows/' + dev_id + '/' + flow_id
-    req = requests.Request(method, url, auth=('onos', 'rocks'))
+    flow_list = []
+    for dev_id, flow_id in (flows):
+        flow = copy.deepcopy(template)
+        flow["deviceId"] = dev_id
+        flow["flowId"] = flow_id
+        flow_list.append(flow)
+    body = {"flows": flow_list}
+    url = 'http://' + cntl + ':' + '8181/onos/v1/flows/'
+    req_data = json.dumps(body)
+    req = requests.Request(method, url, headers={'Content-Type': 'application/json'},
+                           data=req_data, auth=('onos', 'rocks'))
     return req
 
 
@@ -281,6 +295,8 @@ def main(*argv):
                              'Each thread will add/delete <FLOWS> flows.')
     parser.add_argument('--flows', type=int, default=10,
                         help='Number of flows that will be added/deleted in total, default 10')
+    parser.add_argument('--fpr', type=int, default=1,
+                        help='Number of flows per REST request, default 1')
     parser.add_argument('--timeout', type=int, default=100,
                         help='The maximum time (seconds) to wait between the add and delete cycles; default=100')
     parser.add_argument('--no-delete', dest='no_delete', action='store_true', default=False,
@@ -308,13 +324,20 @@ def main(*argv):
     print "    flows  :", base_num_flows
 
     # lets fill the queue for workers
+    nflows = 0
+    flow_list = []
     flow_details = []
     sendqueue = Queue.Queue()
     for i in range(in_args.flows):
         dev_id = random.choice(base_dev_ids)
         dst_ip = ip_addr.increment()
-        sendqueue.put([(dev_id, dst_ip)])
+        flow_list.append((dev_id, dst_ip))
         flow_details.append((dev_id, dst_ip))
+        nflows += 1
+        if nflows == in_args.fpr:
+            sendqueue.put(flow_list)
+            nflows = 0
+            flow_list = []
 
     # result_gueue
     resultqueue = Queue.Queue()
@@ -357,7 +380,7 @@ def main(*argv):
             flow_stats = get_flow_simple_stats(controller=in_args.host)
             print flow_stats
             try:
-                pending_adds = int(flow_stats[u'PENDING_ADD'])
+                pending_adds = int(flow_stats[u'PENDING_ADD'])  # noqa  # FIXME: Print this somewhere.
             except KeyError:
                 break
             time.sleep(1)
@@ -369,6 +392,10 @@ def main(*argv):
 
     if in_args.no_delete:
         return
+
+    # sleep in between
+    time.sleep(in_args.timeout)
+
     # lets delete flows, need to get ids to be deleted, becasue we dont have flow_id on config time
     # we have to pair flows on out own
     flows_remove_details = []
@@ -378,9 +405,16 @@ def main(*argv):
     print "Flows to be removed: ", len(flows_remove_details)
 
     # lets fill the queue for workers
+    nflows = 0
+    flow_list = []
     sendqueue = Queue.Queue()
     for fld in flows_remove_details:
-        sendqueue.put([fld])
+        flow_list.append(fld)
+        nflows += 1
+        if nflows == in_args.fpr:
+            sendqueue.put(flow_list)
+            nflows = 0
+            flow_list = []
 
     # result_gueue
     resultqueue = Queue.Queue()
@@ -395,7 +429,8 @@ def main(*argv):
             thr = threading.Thread(target=_wt_request_sender, args=(i, preparefnc),
                                    kwargs={"inqueue": sendqueue, "exitevent": exitevent,
                                            "controllers": [in_args.host], "restport": in_args.port,
-                                           "template": flow_template, "outqueue": resultqueue, "method": "DELETE"})
+                                           "template": flow_delete_template, "outqueue": resultqueue,
+                                           "method": "DELETE"})
             threads.append(thr)
             thr.start()
 
@@ -430,7 +465,7 @@ def main(*argv):
             flow_stats = get_flow_simple_stats(controller=in_args.host)
             print flow_stats
             try:
-                pending_rems = int(flow_stats[u'PENDING_REMOVE'])
+                pending_rems = int(flow_stats[u'PENDING_REMOVE'])  # noqa  # FIXME: Print this somewhere.
             except KeyError:
                 break
             time.sleep(1)
@@ -450,5 +485,6 @@ def main(*argv):
             fd.write("AddRate,DeleteRate\n")
             fd.write("{0},{1}\n".format(addrate, delrate))
 
+
 if __name__ == "__main__":
     main(sys.argv[1:])