Benchmark encoding translated messages as binary, too.
[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_json_size = 0
17 total_raw_binary_size = 0
18 total_translated_json_size = 0
19 total_translated_binary_size = 0
20
21 for trace_file in sys.argv[1:]:
22     for line in open(trace_file):
23         try:
24             json_message = json.loads(line)
25         except ValueError:
26             continue
27
28         if 'id' and 'data' in json_message:
29             total_raw_json_size += len(line)
30             binary_message = openxc_pb2.RawMessage()
31             binary_message.message_id = json_message['id']
32             binary_message.data = int(json_message['data'], 0)
33             total_raw_binary_size += len(binary_message.SerializeToString())
34         else:
35             if isinstance(json_message['value'], bool):
36                 binary_message = openxc_pb2.TranslatedBooleanMessage()
37             elif isinstance(json_message['value'], numbers.Number):
38                 binary_message = openxc_pb2.TranslatedNumericMessage()
39             else:
40                 binary_message = openxc_pb2.TranslatedStringMessage()
41             binary_message.name = json_message['name']
42             binary_message.value = json_message['value']
43             total_translated_json_size += len(line)
44             total_translated_binary_size += len(binary_message.SerializeToString())
45
46
47 print("For the %d trace files given..." % len(sys.argv[1:]))
48 print("Total transferred raw JSON size is %s" % sizeof_fmt(total_raw_json_size))
49 print("Total transferred raw binary size is %s" % sizeof_fmt(total_raw_binary_size))
50 print("Total transferred translated JSON size is %s" %
51         sizeof_fmt(total_translated_json_size))
52 print("Total transferred translated binary size is %s" %
53         sizeof_fmt(total_translated_binary_size))
54
55 total_json_size = total_raw_json_size + total_translated_json_size
56 print("Total transferred JSON size is %s" % sizeof_fmt(total_json_size))
57 total_binary_size = total_raw_binary_size + total_translated_binary_size
58 print("Total transferred binary size is %s" % sizeof_fmt(total_binary_size))
59
60 print("Binary encoding is %f%% smaller than JSON for raw messages" % (
61         100 - (total_raw_binary_size / total_raw_json_size * 100)))
62 print("Binary encoding is %f%% smaller than JSON for translated messages" % (
63         100 - (total_translated_binary_size / total_translated_json_size * 100)))
64 print("Binary encoding is %f%% smaller than JSON overall" % (
65         100 - (total_binary_size / total_json_size * 100)))