// Throughput Counter // Connects to Jetstream and counts post creates per second — a simple, // real-time throughput meter. Prints a line every second with the current // rate and running total. // // Usage: npx tsx examples/throughput.ts import { startStreamWithReconnect, isPostCreate } from "../lib/index.js"; // --- Counters --- let total = 0; let thisSecond = 0; const startTime = Date.now(); // --- Summary on exit --- const printSummary = () => { const elapsed = (Date.now() - startTime) / 1_000; const avg = elapsed > 0 ? (total / elapsed).toFixed(1) : "0"; console.log(`\n--- Session summary ---`); console.log(`Duration: ${elapsed.toFixed(1)}s`); console.log(`Total: ${total} posts`); console.log(`Average: ${avg} posts/sec`); process.exit(0); }; process.on("SIGINT", printSummary); process.on("SIGTERM", printSummary); // --- Print loop (every 1 second) --- setInterval(() => { const rate = thisSecond; total += rate; console.log(`${rate} posts/sec | ${total} total`); thisSecond = 0; }, 1_000); // --- Stream --- startStreamWithReconnect({ config: { wantedCollections: ["app.bsky.feed.post"] }, onEvent: (event) => { if (isPostCreate(event)) { thisSecond++; } }, onOpen: () => { console.log("Connected to Jetstream. Counting posts per second...\n"); }, });