|
| 1 | +import { Bench } from "tinybench"; |
| 2 | +import { validateBlockKit } from "../src/validate-block-kit.js"; |
| 3 | +import { findDuplicateBlockIds } from "../src/helpers/find-duplicate-block-ids.js"; |
| 4 | +import { checkFocusOnLoadUniqueness } from "../src/helpers/check-focus-on-load-uniqueness.js"; |
| 5 | +import { checkSurfaceCompatibility } from "../src/helpers/check-surface-compatibility.js"; |
| 6 | + |
| 7 | +const buildSimpleBlocks = (n: number): unknown[] => |
| 8 | + Array.from({ length: n }, (_, i) => ({ |
| 9 | + type: "section", |
| 10 | + block_id: `b${i}`, |
| 11 | + text: { type: "mrkdwn", text: `Block ${i} — hello *world*` }, |
| 12 | + })); |
| 13 | + |
| 14 | +const buildMixedBlocks = (n: number): unknown[] => |
| 15 | + Array.from({ length: n }, (_, i) => { |
| 16 | + const kind = i % 5; |
| 17 | + if (kind === 0) return { type: "divider", block_id: `b${i}` }; |
| 18 | + if (kind === 1) |
| 19 | + return { |
| 20 | + type: "section", |
| 21 | + block_id: `b${i}`, |
| 22 | + text: { type: "mrkdwn", text: `Block ${i} — hello *world*` }, |
| 23 | + }; |
| 24 | + if (kind === 2) |
| 25 | + return { |
| 26 | + type: "header", |
| 27 | + block_id: `b${i}`, |
| 28 | + text: { type: "plain_text", text: `Section ${i}` }, |
| 29 | + }; |
| 30 | + if (kind === 3) |
| 31 | + return { |
| 32 | + type: "actions", |
| 33 | + block_id: `b${i}`, |
| 34 | + elements: [ |
| 35 | + { |
| 36 | + type: "button", |
| 37 | + text: { type: "plain_text", text: "Go" }, |
| 38 | + action_id: `a${i}`, |
| 39 | + value: String(i), |
| 40 | + }, |
| 41 | + ], |
| 42 | + }; |
| 43 | + return { |
| 44 | + type: "context", |
| 45 | + block_id: `b${i}`, |
| 46 | + elements: [{ type: "mrkdwn", text: `Footer ${i}` }], |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | +const SMALL = buildSimpleBlocks(5); |
| 51 | +const TYPICAL = buildMixedBlocks(20); |
| 52 | +const LARGE = buildMixedBlocks(50); |
| 53 | + |
| 54 | +const INVALID = [ |
| 55 | + { type: "section" }, |
| 56 | + { type: "header", text: { type: "plain_text", text: "x".repeat(200) } }, |
| 57 | +]; |
| 58 | + |
| 59 | +const main = async (): Promise<void> => { |
| 60 | + const bench = new Bench({ time: 500 }); |
| 61 | + |
| 62 | + bench |
| 63 | + .add("validateBlockKit / 5-block valid", () => { |
| 64 | + validateBlockKit(SMALL); |
| 65 | + }) |
| 66 | + .add("validateBlockKit / 20-block valid", () => { |
| 67 | + validateBlockKit(TYPICAL); |
| 68 | + }) |
| 69 | + .add("validateBlockKit / 50-block valid", () => { |
| 70 | + validateBlockKit(LARGE); |
| 71 | + }) |
| 72 | + .add("validateBlockKit / 2-block invalid (error formatting)", () => { |
| 73 | + validateBlockKit(INVALID); |
| 74 | + }) |
| 75 | + .add("findDuplicateBlockIds / 50 blocks", () => { |
| 76 | + findDuplicateBlockIds(LARGE as never); |
| 77 | + }) |
| 78 | + .add("checkFocusOnLoadUniqueness / 50 blocks", () => { |
| 79 | + checkFocusOnLoadUniqueness(LARGE); |
| 80 | + }) |
| 81 | + .add("checkSurfaceCompatibility / 50 blocks on message", () => { |
| 82 | + checkSurfaceCompatibility(LARGE as never, "message"); |
| 83 | + }); |
| 84 | + |
| 85 | + await bench.run(); |
| 86 | + |
| 87 | + console.log("\nresults (ops/sec, higher is better):"); |
| 88 | + console.table( |
| 89 | + bench.tasks.map(({ name, result }) => { |
| 90 | + // tinybench's result type is a union that includes an aborted variant |
| 91 | + // missing latency/throughput. Narrow on `state` before reading them. |
| 92 | + if (result?.state !== "completed") { |
| 93 | + return { task: name, "ops/sec": "—", "avg µs/op": "—", samples: 0 }; |
| 94 | + } |
| 95 | + return { |
| 96 | + task: name, |
| 97 | + "ops/sec": result.throughput.mean.toFixed(0), |
| 98 | + "avg µs/op": (result.latency.mean * 1000).toFixed(2), |
| 99 | + samples: result.latency.samplesCount, |
| 100 | + }; |
| 101 | + }), |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +main().catch((err) => { |
| 106 | + console.error(err); |
| 107 | + process.exit(1); |
| 108 | +}); |
0 commit comments