From 4f744632aff50723979f03fba66f968a6d194dd6 Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Wed, 6 Feb 2019 12:00:51 +0100
Subject: protocol benchmarks: Accessors for serialized data size availability

---
 lib/protocol_benchmarks.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/lib/protocol_benchmarks.py b/lib/protocol_benchmarks.py
index 8bf8bcb..b8d68cc 100755
--- a/lib/protocol_benchmarks.py
+++ b/lib/protocol_benchmarks.py
@@ -71,6 +71,9 @@ class DummyProtocol:
     def get_extra_files(self):
         return dict()
 
+    def can_get_serialized_length(self):
+        return False
+
 class ArduinoJSON(DummyProtocol):
 
     def __init__(self, data, bufsize = 255, int_type = 'uint16_t', float_type = 'float'):
@@ -88,6 +91,9 @@ class ArduinoJSON(DummyProtocol):
     def get_serialized_length(self):
         return len(json.dumps(self.data))
 
+    def can_get_serialized_length(self):
+        return True
+
     def get_encode(self):
         return self.enc_buf
 
@@ -439,6 +445,9 @@ class ManualJSON(DummyProtocol):
     def get_serialized_length(self):
         return len(json.dumps(self.data))
 
+    def can_get_serialized_length(self):
+        return True
+
     def is_ascii(self):
         return True
 
@@ -548,6 +557,23 @@ class ModernJSON(DummyProtocol):
         else:
             raise ValueError('invalid output format {}'.format(self.output_format))
 
+    def get_serialized_length(self):
+        if self.output_format == 'json':
+            return len(json.dumps(self.data))
+        elif self.output_format == 'bson':
+            return len(bson.BSON.encode(self.data))
+        elif self.output_format == 'cbor':
+            return len(cbor.dumps(self.data))
+        elif self.output_format == 'msgpack':
+            return len(msgpack.dumps(self.data))
+        elif self.output_format == 'ubjson':
+            return len(ubjson.dumpb(self.data))
+        else:
+            raise ValueError('invalid output format {}'.format(self.output_format))
+
+    def can_get_serialized_length(self):
+        return True
+
     def get_length_var(self):
         return 'out.size()'
 
@@ -602,6 +628,9 @@ class MPack(DummyProtocol):
     def get_serialized_length(self):
         return len(msgpack.dumps(self.data))
 
+    def can_get_serialized_length(self):
+        return True
+
     def is_ascii(self):
         return False
 
@@ -916,6 +945,9 @@ class UBJ(DummyProtocol):
     def get_serialized_length(self):
         return len(ubjson.dumpb(self.data))
 
+    def can_get_serialized_length(self):
+        return True
+
     def is_ascii(self):
         return False
 
-- 
cgit v1.2.3