|
1 | 1 | """Micropython code to be ran on-device.""" |
2 | 2 |
|
3 | 3 | import gc |
4 | | -import io |
| 4 | +import os |
5 | 5 | import time |
6 | 6 |
|
7 | 7 | import tamp |
8 | 8 |
|
9 | 9 |
|
10 | 10 | def main(): |
11 | | - # Load input data into RAM before timing |
| 11 | + # Small enough to allocate on a fragmented ESP32 heap, |
| 12 | + # large enough to amortize readinto + write call overhead. |
| 13 | + block_size = 16384 |
| 14 | + input_path = "enwik8-100kb" |
| 15 | + output_path = "enwik8-100kb.tamp" |
| 16 | + |
| 17 | + decompressed_len = os.stat(input_path)[6] |
| 18 | + |
12 | 19 | gc.collect() |
13 | | - print("Loading input data into RAM...") |
14 | | - with open("enwik8-100kb", "rb") as f: |
15 | | - input_data = f.read() |
16 | | - decompressed_len = len(input_data) |
| 20 | + read_buf = bytearray(block_size) |
| 21 | + mv = memoryview(read_buf) |
| 22 | + |
| 23 | + # Tare: flash-read cost alone, so we can subtract it from the full pass |
| 24 | + # and recover a pure-compression throughput number. |
| 25 | + print("Measuring flash-read tare...") |
| 26 | + start_us = time.ticks_us() |
| 27 | + with open(input_path, "rb") as f: |
| 28 | + while f.readinto(read_buf): |
| 29 | + pass |
| 30 | + read_elapsed_us = time.ticks_diff(time.ticks_us(), start_us) |
17 | 31 |
|
18 | | - # Pre-allocate output buffer |
19 | 32 | gc.collect() |
20 | | - output_buffer = io.BytesIO() |
21 | 33 |
|
22 | | - # Time only the compression (RAM to RAM) |
| 34 | + # Compression: read chunk -> feed compressor -> compressor writes to flash. |
| 35 | + out = open(output_path, "wb") |
23 | 36 | print("Compressing...") |
24 | | - start_ms = time.ticks_ms() |
25 | | - with tamp.open(output_buffer, "wb") as compressor: |
26 | | - compressor.write(input_data) |
27 | | - elapsed_ms = time.ticks_diff(time.ticks_ms(), start_ms) |
28 | | - |
29 | | - compressed_data = output_buffer.getvalue() |
30 | | - compressed_len = len(compressed_data) |
| 37 | + try: |
| 38 | + start_us = time.ticks_us() |
| 39 | + with open(input_path, "rb") as f_in, tamp.open(out, "wb") as compressor: |
| 40 | + while True: |
| 41 | + n = f_in.readinto(read_buf) |
| 42 | + if not n: |
| 43 | + break |
| 44 | + compressor.write(mv[:n]) |
| 45 | + total_elapsed_us = time.ticks_diff(time.ticks_us(), start_us) |
| 46 | + finally: |
| 47 | + out.close() |
31 | 48 |
|
32 | | - # Write to flash for verification (not timed) |
33 | | - with open("enwik8-100kb.tamp", "wb") as f: |
34 | | - f.write(compressed_data) |
| 49 | + compressed_len = os.stat(output_path)[6] |
| 50 | + compress_only_us = total_elapsed_us - read_elapsed_us |
35 | 51 |
|
36 | | - elapsed_s = elapsed_ms / 1000 |
37 | | - bytes_per_sec = decompressed_len / elapsed_s if elapsed_s > 0 else 0 |
| 52 | + total_s = total_elapsed_us / 1_000_000 |
| 53 | + compress_only_s = compress_only_us / 1_000_000 |
| 54 | + total_bps = decompressed_len / total_s if total_s > 0 else 0 |
| 55 | + compress_bps = decompressed_len / compress_only_s if compress_only_s > 0 else 0 |
38 | 56 |
|
39 | 57 | print(f"{decompressed_len=:,}") |
40 | 58 | print(f"{compressed_len=:,}") |
41 | | - print(f"elapsed={elapsed_s:.3f}s") |
42 | | - print(f"compression={bytes_per_sec:,.0f} bytes/s") |
| 59 | + print(f"read_tare={read_elapsed_us / 1_000_000:.6f}s") |
| 60 | + print(f"total_elapsed={total_s:.6f}s") |
| 61 | + print(f"end-to-end={total_bps:,.0f} bytes/s") |
| 62 | + print(f"compression-only={compress_bps:,.0f} bytes/s") |
43 | 63 |
|
44 | 64 |
|
45 | 65 | if __name__ == "__main__": |
|
0 commit comments