Add benchmarking tests for binary encoding (moved from cantranslator).
[apps/agl-service-can-low-level.git] / benchmark / proto / compare_sizes.py
1 #!/usr/bin/env python
2
3 from __future__ import division
4 import sys
5
6 import openxc_pb2
7 import json
8
9 def sizeof_fmt(num):
10     for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']:
11         if num < 1024.0:
12             return "%3.1f%s" % (num, unit)
13         num /= 1024.0
14
15 total_json_size = 0
16 total_binary_size = 0
17
18 trace_file = sys.argv[1]
19 for line in open(trace_file):
20     raw_message = json.loads(line)
21     total_json_size += len(json.dumps(raw_message))
22     binary_message = openxc_pb2.RawMessage()
23     binary_message.message_id = raw_message['id']
24     binary_message.data = int(raw_message['data'], 0)
25     total_binary_size += len(binary_message.SerializeToString())
26
27 print("For the trace file %s..." % trace_file)
28 print("Total transferred JSON size is %s" % sizeof_fmt(total_json_size))
29 print("Total transferred binary size is %s" % sizeof_fmt(total_binary_size))
30 print("Binary encoding is %f%% smaller than JSON overall" % (
31         100 - (total_binary_size / total_json_size * 100)))