"""Use Python 3.9+ to reproduce the illustrative byte counts offline."""
import argparse
import gzip
import json
LIMIT = 1152 # Invented test budget, in bytes.
MARK = b'SKU-42'
def trial(offset, header_size):
# Count a fixed, illustrative HTTP/1.1 response head, including the blank line.
head = b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Encoding: gzip\r\nX-Padding: '
head += b'x' * (header_size - len(head) - 4) + b'\r\n\r\n'
start = '
Café'.encode('utf-8')
body = start.ljust(offset) + MARK + b' ' * (1400 - offset) + b''
wire = gzip.compress(body, mtime=0)
raw = gzip.decompress(wire)
assert len(head) == header_size and len(head) + len(wire) < LIMIT
assert raw.count(MARK) == 1 and raw.index(MARK) == offset
end = len(head) + offset + len(MARK)
kept = raw[:max(0, LIMIT - len(head))][offset:offset + len(MARK)]
assert (end <= LIMIT) == (kept == MARK)
return [str(offset), f'{len(kept)}/{len(MARK)}', kept.decode() or '∅']
def run():
return dict(key='byte-limit', budget_bytes=LIMIT, response_head_bytes=128,
columns=['Start offset in decoded body', 'Bytes retained', 'Fragment'],
rows=[trial(n, 128) for n in (128, 1018, 1021, 1100)],
larger_header_example=dict(response_head_bytes=131, row=trial(1018, 131)),
limits='Synthetic byte arithmetic. Actual response headers, transport and crawler processing vary. No indexing outcome is measured.')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('case', nargs='?', choices=['byte-limit'])
parser.parse_args()
print(json.dumps(run(), indent=2, ensure_ascii=False))