|
| 1 | +"""Renders the README benchmark charts as SVG. |
| 2 | +
|
| 3 | + python benchmarks/charts/render.py |
| 4 | +
|
| 5 | +Numbers are the ones in README.md (8-core Ryzen 7 7800X3D, .NET 10, Release, Server GC). Edit the tables |
| 6 | +below when the README numbers change and re-run; the SVGs are committed so GitHub can show them. |
| 7 | +""" |
| 8 | +import os |
| 9 | + |
| 10 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 11 | + |
| 12 | +FONT = "-apple-system, 'Segoe UI', Helvetica, Arial, sans-serif" |
| 13 | +COLORS = { |
| 14 | + "ours": "#16a34a", # JSON-RPC.Net |
| 15 | + "theirs": "#3b82f6", # StreamJsonRpc |
| 16 | + "scale": "#9ca3af", # in-process reference rows |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def fmt(v): |
| 21 | + if v >= 1e6: |
| 22 | + return f"{v / 1e6:.1f} M" |
| 23 | + if v >= 1e3: |
| 24 | + return f"{v / 1e3:.0f} k" |
| 25 | + return f"{v:.0f}" |
| 26 | + |
| 27 | + |
| 28 | +def chart(title, subtitle, rows, filename, width=920): |
| 29 | + """rows: (label, low, high, kind, note). Bar length is the mean of low and high; the label shows the range.""" |
| 30 | + left, right, top, row_h, bar_h = 330, 110, 78, 34, 20 |
| 31 | + height = top + row_h * len(rows) + 46 |
| 32 | + plot_w = width - left - right |
| 33 | + vmax = max((lo + hi) / 2 for _, lo, hi, _, _ in rows) |
| 34 | + |
| 35 | + out = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}" ' |
| 36 | + f'font-family="{FONT}" font-size="13">', |
| 37 | + f'<rect width="{width}" height="{height}" rx="10" fill="#ffffff" stroke="#e5e7eb"/>', |
| 38 | + f'<text x="24" y="32" font-size="18" font-weight="600" fill="#111827">{title}</text>', |
| 39 | + f'<text x="24" y="54" font-size="12" fill="#6b7280">{subtitle}</text>'] |
| 40 | + |
| 41 | + # gridlines at round values |
| 42 | + step = 10 ** (len(str(int(vmax))) - 1) |
| 43 | + if vmax / step < 3: |
| 44 | + step /= 2 |
| 45 | + g = step |
| 46 | + while g <= vmax: |
| 47 | + x = left + plot_w * g / vmax |
| 48 | + out.append(f'<line x1="{x:.1f}" y1="{top - 8}" x2="{x:.1f}" y2="{height - 40}" stroke="#f3f4f6"/>') |
| 49 | + out.append(f'<text x="{x:.1f}" y="{height - 24}" text-anchor="middle" font-size="11" fill="#9ca3af">{fmt(g)}</text>') |
| 50 | + g += step |
| 51 | + out.append(f'<text x="{left + plot_w:.1f}" y="{height - 10}" text-anchor="end" font-size="11" fill="#9ca3af">requests per second</text>') |
| 52 | + |
| 53 | + for i, (label, lo, hi, kind, note) in enumerate(rows): |
| 54 | + y = top + i * row_h |
| 55 | + v = (lo + hi) / 2 |
| 56 | + w = max(2.0, plot_w * v / vmax) |
| 57 | + out.append(f'<text x="{left - 12}" y="{y + bar_h / 2 + 4}" text-anchor="end" fill="#111827">{label}</text>') |
| 58 | + if note: |
| 59 | + out.append(f'<text x="{left - 12}" y="{y + bar_h / 2 + 17}" text-anchor="end" font-size="10.5" fill="#6b7280">{note}</text>') |
| 60 | + out.append(f'<rect x="{left}" y="{y}" width="{w:.1f}" height="{bar_h}" rx="4" fill="{COLORS[kind]}"/>') |
| 61 | + if lo == hi: |
| 62 | + value = fmt(v) |
| 63 | + else: |
| 64 | + a, b = fmt(lo), fmt(hi) |
| 65 | + if a == b: # the range rounds to the same text at one decimal: show two |
| 66 | + a, b = f"{lo / 1e6:.2f} M", f"{hi / 1e6:.2f} M" |
| 67 | + value = f"{a} to {b}" |
| 68 | + out.append(f'<text x="{left + w + 8:.1f}" y="{y + bar_h / 2 + 4}" font-weight="600" fill="#111827">{value}</text>') |
| 69 | + |
| 70 | + out.append('</svg>') |
| 71 | + path = os.path.join(HERE, filename) |
| 72 | + with open(path, "w", encoding="utf-8", newline="\n") as f: |
| 73 | + f.write("\n".join(out) + "\n") |
| 74 | + print("wrote", os.path.relpath(path)) |
| 75 | + |
| 76 | + |
| 77 | +chart( |
| 78 | + "JSON-RPC.Net vs StreamJsonRpc", |
| 79 | + "Same five requests, same Kestrel TCP listener, same pipelining client (16 connections, 256 in flight). Two 3 s runs each.", |
| 80 | + [ |
| 81 | + ("JSON-RPC.Net over Kestrel TCP", 11.4e6, 12.1e6, "ours", "raw documents"), |
| 82 | + ("StreamJsonRpc over Kestrel TCP", 1.14e6, 1.18e6, "theirs", "Content-Length framing, System.Text.Json formatter"), |
| 83 | + ("StreamJsonRpc over Kestrel TCP", 1.05e6, 1.10e6, "theirs", "newline framing, System.Text.Json formatter"), |
| 84 | + ("StreamJsonRpc over Kestrel TCP", 515e3, 518e3, "theirs", "Content-Length framing, Json.NET formatter (default)"), |
| 85 | + ("JSON-RPC.Net in-process, 1 thread", 3.1e6, 3.3e6, "ours", "direct call, bytes in, bytes out"), |
| 86 | + ("StreamJsonRpc in-process, 1 client", 116e3, 166e3, "theirs", "Pipe pair, 256 pipelined"), |
| 87 | + ("StreamJsonRpc typed proxy", 90e3, 90e3, "theirs", "sequential await, 11 µs per round trip"), |
| 88 | + ], |
| 89 | + "compare-streamjsonrpc.svg", |
| 90 | +) |
| 91 | + |
| 92 | +chart( |
| 93 | + "JSON-RPC.Net by transport", |
| 94 | + "AustinHarris.JsonRpc.AspNetCore on Kestrel, loopback, 16 clients on the same 8-core machine.", |
| 95 | + [ |
| 96 | + ("TCP, 256 pipelined", 14.3e6, 14.8e6, "ours", "JsonRpcConnectionHandler"), |
| 97 | + ("HTTP, batch of 100 per POST", 9.5e6, 10.6e6, "ours", "MapJsonRpc"), |
| 98 | + ("HTTP, 1 request per POST", 50e3, 130e3, "ours", "130 to 300 µs per round trip per client"), |
| 99 | + ("in-process, 16 threads", 32.3e6, 32.3e6, "scale", "no transport, for scale"), |
| 100 | + ("in-process, 1 thread", 4.2e6, 4.2e6, "scale", "no transport, for scale"), |
| 101 | + ], |
| 102 | + "kestrel-transports.svg", |
| 103 | +) |
| 104 | + |
| 105 | +chart( |
| 106 | + "JSON-RPC.Net alone, by thread count", |
| 107 | + "Byte entry point in a loop, no scheduler, no transport. 8 cores / 16 threads.", |
| 108 | + [ |
| 109 | + ("1 thread", 4.4e6, 4.4e6, "ours", "227 ns per request, 0 bytes allocated"), |
| 110 | + ("2 threads", 8.8e6, 8.8e6, "ours", ""), |
| 111 | + ("4 threads", 15.1e6, 15.1e6, "ours", ""), |
| 112 | + ("8 threads", 24.0e6, 24.0e6, "ours", ""), |
| 113 | + ("16 threads", 32.8e6, 32.8e6, "ours", "SMT: two threads per core"), |
| 114 | + ], |
| 115 | + "sync-threads.svg", |
| 116 | +) |
0 commit comments