3 from __future__ import division
11 for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']:
13 return "%3.1f%s" % (num, unit)
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
22 for trace_file in sys.argv[1:]:
23 for line in open(trace_file):
25 json_message = json.loads(line)
29 if 'id' and 'data' in json_message:
30 # rough approx. that CAN messages are 10 bytes - they could be less
31 # but most of ours are full 64+11 bits
32 total_raw_can_size += 10
33 total_raw_json_size += len(line)
34 binary_message = openxc_pb2.RawMessage()
35 binary_message.message_id = json_message['id']
36 binary_message.data = int(json_message['data'], 0)
37 total_raw_binary_size += len(binary_message.SerializeToString())
39 if isinstance(json_message['value'], bool):
40 binary_message = openxc_pb2.TranslatedBooleanMessage()
41 elif isinstance(json_message['value'], numbers.Number):
42 binary_message = openxc_pb2.TranslatedNumericMessage()
44 binary_message = openxc_pb2.TranslatedStringMessage()
45 binary_message.name = json_message['name']
46 binary_message.value = json_message['value']
47 total_translated_json_size += len(line)
48 total_translated_binary_size += len(binary_message.SerializeToString())
51 print("For the %d trace files given..." % len(sys.argv[1:]))
52 print("Total transferred raw CAN size is %s" % sizeof_fmt(total_raw_can_size))
53 print("Total transferred raw JSON size is %s" % sizeof_fmt(total_raw_json_size))
54 print("Total transferred raw binary size is %s" % sizeof_fmt(total_raw_binary_size))
55 print("Total transferred translated JSON size is %s" %
56 sizeof_fmt(total_translated_json_size))
57 print("Total transferred translated binary size is %s" %
58 sizeof_fmt(total_translated_binary_size))
60 total_json_size = total_raw_json_size + total_translated_json_size
61 print("Total transferred JSON size is %s" % sizeof_fmt(total_json_size))
62 total_binary_size = total_raw_binary_size + total_translated_binary_size
63 print("Total transferred binary size is %s" % sizeof_fmt(total_binary_size))
65 if total_raw_can_size > 0:
66 print("Binary encoding adds %f%% overhead to raw CAN messages" % (
67 total_raw_binary_size / total_raw_can_size * 100 - 100))
68 print("JSON encoding adds %f%% overhead to raw CAN messages" % (
69 total_raw_json_size / total_raw_can_size * 100 - 100))
70 if total_raw_json_size > 0:
71 print("Binary encoding is %f%% smaller than JSON for raw messages" % (
72 100 - (total_raw_binary_size / total_raw_json_size * 100)))
73 if total_translated_json_size > 0:
74 print("Binary encoding is %f%% smaller than JSON for translated messages" % (
75 100 - (total_translated_binary_size / total_translated_json_size * 100)))
76 print("Binary encoding is %f%% smaller than JSON overall" % (
77 100 - (total_binary_size / total_json_size * 100)))