3 # This script takes directories which contain the hpack-test-case json
4 # files, and calculates the compression ratio in each file and outputs
5 # the result in table formatted in rst.
7 # The each directory contains the result of various HPACK compressor.
9 # The table is laid out so that we can see that how input header set
10 # in one json file is compressed in each compressor.
12 # For hpack-test-case, see https://github.com/Jxck/hpack-test-case
14 import sys, json, os, re
18 def __init__(self, complen, srclen):
19 self.complen = complen
22 def compute_stat(jsdata):
25 for item in jsdata['cases']:
26 complen += len(item['wire']) // 2
28 sum([len(list(x.keys())[0]) + len(list(x.values())[0]) \
29 for x in item['headers']])
30 return Stat(complen, srclen)
33 return '{:.02f} ({}/{}) '.format(r.complen/r.srclen, r.complen, r.srclen)
35 if __name__ == '__main__':
36 entries = [(os.path.basename(re.sub(r'/+$', '', p)), p) \
37 for p in sys.argv[1:]]
43 for name, ent in entries:
44 files = [p for p in os.listdir(ent) if p.endswith('.json')]
46 maxnamelen = max(maxnamelen, len(name))
49 maxstorynamelen = max(maxstorynamelen, len(fn))
50 with open(os.path.join(ent, fn)) as f:
52 rv = compute_stat(json.loads(input))
54 maxnamelen = max(maxnamelen, len(format_result(rv)))
55 stories = list(stories)
58 storynameformat = '{{:{}}} '.format(maxstorynamelen)
59 nameformat = '{{:{}}} '.format(maxnamelen)
63 hpack-test-case compression ratio
64 =================================
66 The each cell has ``X (Y/Z)`` format:
71 number of bytes after compression
73 number of bytes before compression
78 sys.stdout.write('='*maxstorynamelen)
81 sys.stdout.write('='*maxnamelen)
83 sys.stdout.write('\n')
87 sys.stdout.write(storynameformat.format('story'))
88 for name, _ in entries:
89 sys.stdout.write(nameformat.format(name))
90 sys.stdout.write('\n')
95 sys.stdout.write(storynameformat.format(story))
97 for name, _ in entries:
99 if story not in stats:
100 sys.stdout.write(nameformat.format('N/A'))
103 srclen = stats[story].srclen
104 elif srclen != stats[story].srclen:
105 raise Exception('Bad srclen')
106 sys.stdout.write(nameformat.format(format_result(stats[story])))
107 sys.stdout.write('\n')