TASK
Implementation
Add compression to your message framing. Benchmark the CPU vs bandwidth tradeoff at different payload sizes to determine when compression helps versus hurts.
Add a compression flag to the frame header. The receiver checks the flag and decompresses if needed.
Implement handlers:
Request: {"type": "compress", "msg_id": 1, "data": "hello hello hello hello", "algorithm": "lz4"}
Response: {"type": "compress_ok", "in_reply_to": 1, "original_size": 23, "compressed_size": 14, "ratio": 0.61}
Request: {"type": "compress_benchmark", "msg_id": 2, "payload_sizes_bytes": [64, 1024, 10240, 102400]}
Response: {"type": "compress_benchmark_ok", "in_reply_to": 2, "results": [
{"size": 64, "compressed_size": 72, "ratio": 1.13, "verdict": "skip_compression"},
{"size": 1024, "compressed_size": 420, "ratio": 0.41, "verdict": "compress"},
{"size": 10240, "compressed_size": 2100, "ratio": 0.21, "verdict": "compress"},
{"size": 102400, "compressed_size": 15000, "ratio": 0.15, "verdict": "compress"}
]}Sample Test Cases
Compress repetitive dataTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"compress","msg_id":2,"data":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","algorithm":"lz4"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Compress and decompress roundtripTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"compress","msg_id":2,"data":"test data for roundtrip","algorithm":"lz4"}}
{"src":"c1","dest":"n1","body":{"type":"decompress","msg_id":3,"algorithm":"lz4"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Compress the payload after serialization, before framing
Hint 2▾
Small payloads (< 100 bytes) often get larger after compression due to header overhead
Hint 3▾
Use a compression flag in the frame header: 0x00=raw, 0x01=compressed
Hint 4▾
Benchmark at different payload sizes to find the crossover point
Hint 5▾
CPU cost of compression may exceed the bandwidth savings for small messages
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
compressionLZ4SnappyCPU vs bandwidthtradeoff analysis
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()