192716f458ac4095ed0c5f46b2cc80910177ee38
[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 import numbers
6
7 import openxc_pb2
8 import json
9
10 def sizeof_fmt(num):
11     for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']:
12         if num < 1024.0:
13             return "%3.1f%s" % (num, unit)
14         num /= 1024.0
15
16 total_raw_can_size = 0
17 total_raw_json_size = 0
18 total_raw_binary_size = 0
19 total_translated_json_size = 0
20 total_translated_binary_size = 0
21
22 for trace_file in sys.argv[1:]:
23     for line in open(trace_file):
24         try:
25             json_message = json.loads(line)
26         except ValueError:
27             continue
28
29         message = openxc_pb2.VehicleMessage()
30
31         if 'id' and 'data' in json_message:
32             # rough approx. that CAN messages are 10 bytes - they could be less
33             # but most of ours are full 64+11 bits
34             total_raw_can_size += 10
35             total_raw_json_size += len(line)
36             binary_message = openxc_pb2.RawMessage()
37             binary_message.message_id = json_message['id']
38             binary_message.data = int(json_message['data'], 0)
39             message.type = openxc_pb2.VehicleMessage.RAW
40             message.raw_message = binary_message
41             total_raw_binary_size += len(message.SerializeToString())
42         else:
43             if isinstance(json_message['value'], bool):
44                 message.type = openxc_pb2.VehicleMessage.BOOL
45                 message.boolean_message.name = json_message['name']
46                 message.boolean_message.value = json_message['value']
47             elif isinstance(json_message['value'], numbers.Number):
48                 message.type = openxc_pb2.VehicleMessage.NUM
49                 message.numerical_message.name = json_message['name']
50                 message.numerical_message.value = json_message['value']
51             else:
52                 message.type = openxc_pb2.VehicleMessage.STRING
53                 message.string_message.name = json_message['name']
54                 message.string_message.value = json_message['value']
55             total_translated_json_size += len(line)
56             total_translated_binary_size += len(message.SerializeToString())
57
58
59 print("For the %d trace files given..." % len(sys.argv[1:]))
60 print("Total transferred raw CAN size is %s" % sizeof_fmt(total_raw_can_size))
61 print("Total transferred raw JSON size is %s" % sizeof_fmt(total_raw_json_size))
62 print("Total transferred raw binary size is %s" % sizeof_fmt(total_raw_binary_size))
63 print("Total transferred translated JSON size is %s" %
64         sizeof_fmt(total_translated_json_size))
65 print("Total transferred translated binary size is %s" %
66         sizeof_fmt(total_translated_binary_size))
67
68 total_json_size = total_raw_json_size + total_translated_json_size
69 print("Total transferred JSON size is %s" % sizeof_fmt(total_json_size))
70 total_binary_size = total_raw_binary_size + total_translated_binary_size
71 print("Total transferred binary size is %s" % sizeof_fmt(total_binary_size))
72
73 if total_raw_can_size > 0:
74     print("Binary encoding adds %f%% overhead to raw CAN messages" % (
75             total_raw_binary_size / total_raw_can_size * 100 - 100))
76     print("JSON encoding adds %f%% overhead to raw CAN messages" % (
77             total_raw_json_size / total_raw_can_size * 100 - 100))
78 if total_raw_json_size > 0:
79     print("Binary encoding is %f%% smaller than JSON for raw messages" % (
80             100 - (total_raw_binary_size / total_raw_json_size * 100)))
81 if total_translated_json_size > 0:
82     print("Binary encoding is %f%% smaller than JSON for translated messages" % (
83             100 - (total_translated_binary_size / total_translated_json_size * 100)))
84 print("Binary encoding is %f%% smaller than JSON overall" % (
85         100 - (total_binary_size / total_json_size * 100)))