|
| 1 | +import { _compress, _decompress } from "./redis.ts"; |
| 2 | + |
| 3 | +// Simulates a realistic cache payload: JSON with a large HTML body (5-10 KB range) |
| 4 | +// and typical response headers. Repeated to hit different size tiers. |
| 5 | +function makePayload(sizeKb: number): string { |
| 6 | + const body = `<html><body>${"<div class='product'><img src='https://cdn.bagaggio.com.br/img.jpg'/><h2>Mala de Viagem Premium</h2><p>R$ 1.299,00</p><span>Em estoque</span></div>".repeat(Math.ceil((sizeKb * 1024) / 180))}`; |
| 7 | + return JSON.stringify({ |
| 8 | + body, |
| 9 | + headers: { |
| 10 | + "content-type": "text/html; charset=utf-8", |
| 11 | + "cache-control": "public, max-age=60", |
| 12 | + "x-response-time": "42ms", |
| 13 | + "vary": "Accept-Encoding", |
| 14 | + }, |
| 15 | + status: 200, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +const CODEC_GZIP = 0x01; |
| 20 | +const CODEC_DEFLATE = 0x02; |
| 21 | +const CODEC_LZ4 = 0x03; |
| 22 | +const CODEC_ZSTD = 0x04; |
| 23 | + |
| 24 | +const payload10kb = makePayload(10); |
| 25 | +const payload100kb = makePayload(100); |
| 26 | +const payload1mb = makePayload(1000); |
| 27 | + |
| 28 | +// Pre-compress for decompression benchmarks |
| 29 | +const [gz10, df10, lz10, zs10] = await Promise.all([ |
| 30 | + _compress(payload10kb, CODEC_GZIP), |
| 31 | + _compress(payload10kb, CODEC_DEFLATE), |
| 32 | + _compress(payload10kb, CODEC_LZ4), |
| 33 | + _compress(payload10kb, CODEC_ZSTD), |
| 34 | +]); |
| 35 | +const [gz1mb, df1mb, lz1mb, zs1mb] = await Promise.all([ |
| 36 | + _compress(payload1mb, CODEC_GZIP), |
| 37 | + _compress(payload1mb, CODEC_DEFLATE), |
| 38 | + _compress(payload1mb, CODEC_LZ4), |
| 39 | + _compress(payload1mb, CODEC_ZSTD), |
| 40 | +]); |
| 41 | + |
| 42 | +// ─── Compression ratios (informational, printed once) ──────────────────────── |
| 43 | +const sizes = [ |
| 44 | + ["10 KB", payload10kb], |
| 45 | + ["100 KB", payload100kb], |
| 46 | + ["1 MB", payload1mb], |
| 47 | +] as const; |
| 48 | + |
| 49 | +console.log("\n── Compression ratios ──────────────────────────────────────────"); |
| 50 | +for (const [label, payload] of sizes) { |
| 51 | + const raw = new TextEncoder().encode(payload).length; |
| 52 | + const results = await Promise.all([ |
| 53 | + _compress(payload, CODEC_GZIP).then((c) => ({ name: "gzip ", size: c.length })), |
| 54 | + _compress(payload, CODEC_DEFLATE).then((c) => ({ name: "deflate", size: c.length })), |
| 55 | + _compress(payload, CODEC_LZ4).then((c) => ({ name: "lz4 ", size: c.length })), |
| 56 | + _compress(payload, CODEC_ZSTD).then((c) => ({ name: "zstd/1 ", size: c.length })), |
| 57 | + ]); |
| 58 | + console.log(`\n${label} (raw: ${(raw / 1024).toFixed(1)} KB)`); |
| 59 | + for (const { name, size } of results) { |
| 60 | + const pct = ((1 - size / raw) * 100).toFixed(1); |
| 61 | + console.log(` ${name} ${(size / 1024).toFixed(1).padStart(7)} KB (${pct}% smaller)`); |
| 62 | + } |
| 63 | +} |
| 64 | +console.log("\n── Benchmarks ──────────────────────────────────────────────────"); |
| 65 | + |
| 66 | +// ─── Compress 10 KB ────────────────────────────────────────────────────────── |
| 67 | +Deno.bench({ name: "compress gzip 10KB", group: "compress-10kb" }, async () => { |
| 68 | + await _compress(payload10kb, CODEC_GZIP); |
| 69 | +}); |
| 70 | +Deno.bench({ name: "compress deflate 10KB", group: "compress-10kb" }, async () => { |
| 71 | + await _compress(payload10kb, CODEC_DEFLATE); |
| 72 | +}); |
| 73 | +Deno.bench({ name: "compress lz4 10KB", group: "compress-10kb" }, async () => { |
| 74 | + await _compress(payload10kb, CODEC_LZ4); |
| 75 | +}); |
| 76 | +Deno.bench({ name: "compress zstd/1 10KB", group: "compress-10kb", baseline: true }, async () => { |
| 77 | + await _compress(payload10kb, CODEC_ZSTD); |
| 78 | +}); |
| 79 | + |
| 80 | +// ─── Compress 1 MB ─────────────────────────────────────────────────────────── |
| 81 | +Deno.bench({ name: "compress gzip 1MB", group: "compress-1mb" }, async () => { |
| 82 | + await _compress(payload1mb, CODEC_GZIP); |
| 83 | +}); |
| 84 | +Deno.bench({ name: "compress deflate 1MB", group: "compress-1mb" }, async () => { |
| 85 | + await _compress(payload1mb, CODEC_DEFLATE); |
| 86 | +}); |
| 87 | +Deno.bench({ name: "compress lz4 1MB", group: "compress-1mb" }, async () => { |
| 88 | + await _compress(payload1mb, CODEC_LZ4); |
| 89 | +}); |
| 90 | +Deno.bench({ name: "compress zstd/1 1MB", group: "compress-1mb", baseline: true }, async () => { |
| 91 | + await _compress(payload1mb, CODEC_ZSTD); |
| 92 | +}); |
| 93 | + |
| 94 | +// ─── Decompress 10 KB ──────────────────────────────────────────────────────── |
| 95 | +Deno.bench({ name: "decompress gzip 10KB", group: "decompress-10kb" }, async () => { |
| 96 | + await _decompress(gz10); |
| 97 | +}); |
| 98 | +Deno.bench({ name: "decompress deflate 10KB", group: "decompress-10kb" }, async () => { |
| 99 | + await _decompress(df10); |
| 100 | +}); |
| 101 | +Deno.bench({ name: "decompress lz4 10KB", group: "decompress-10kb" }, async () => { |
| 102 | + await _decompress(lz10); |
| 103 | +}); |
| 104 | +Deno.bench({ name: "decompress zstd/1 10KB", group: "decompress-10kb", baseline: true }, async () => { |
| 105 | + await _decompress(zs10); |
| 106 | +}); |
| 107 | + |
| 108 | +// ─── Decompress 1 MB ───────────────────────────────────────────────────────── |
| 109 | +Deno.bench({ name: "decompress gzip 1MB", group: "decompress-1mb" }, async () => { |
| 110 | + await _decompress(gz1mb); |
| 111 | +}); |
| 112 | +Deno.bench({ name: "decompress deflate 1MB", group: "decompress-1mb" }, async () => { |
| 113 | + await _decompress(df1mb); |
| 114 | +}); |
| 115 | +Deno.bench({ name: "decompress lz4 1MB", group: "decompress-1mb" }, async () => { |
| 116 | + await _decompress(lz1mb); |
| 117 | +}); |
| 118 | +Deno.bench({ name: "decompress zstd/1 1MB", group: "decompress-1mb", baseline: true }, async () => { |
| 119 | + await _decompress(zs1mb); |
| 120 | +}); |
0 commit comments