Stateless API: send batches by chunks 21/4321/1
authorYaroslav Brustinov <[email protected]>
Sat, 10 Dec 2016 20:23:31 +0000 (22:23 +0200)
committerYaroslav Brustinov <[email protected]>
Sat, 10 Dec 2016 20:23:31 +0000 (22:23 +0200)
Change-Id: If551b474c4b6be58dfc3ed19e5c14a4ccd387afd
Signed-off-by: Yaroslav Brustinov <[email protected]>
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py

index ce430e2..fbad9f7 100644 (file)
@@ -32,13 +32,36 @@ class BatchMessage(object):
         id, msg = self.rpc_client.create_jsonrpc_v2(method_name, params, api_class, encode = False)
         self.batch_list.append(msg)
 
-    def invoke(self, block = False):
+    def invoke(self, block = False, chunk_size = 500000):
         if not self.rpc_client.connected:
             return RC_ERR("Not connected to server")
 
-        msg = json.dumps(self.batch_list)
-
-        return self.rpc_client.send_msg(msg)
+        if chunk_size:
+            response_batch = RC()
+            size = 0
+            new_batch = []
+            for msg in self.batch_list:
+                if size < chunk_size:
+                    size += len(json.dumps(msg))
+                    new_batch.append(msg)
+                else:
+                    batch_json = json.dumps(new_batch)
+                    response = self.rpc_client.send_msg(batch_json)
+                    if not response:
+                        return response
+                    response_batch.add(response)
+                    size = 0
+                    new_batch = []
+            if new_batch:
+                batch_json = json.dumps(new_batch)
+                response = self.rpc_client.send_msg(batch_json)
+                if not response:
+                    return response
+                response_batch.add(response)
+            return response_batch
+        else:
+            batch_json = json.dumps(self.batch_list)
+            return self.rpc_client.send_msg(batch_json)
 
 
 # JSON RPC v2.0 client