| 1 | import { Schema, SchemaIssue } from "effect" |
| 2 | import { Bench } from "tinybench" |
| 3 | |
| 4 | const batchSize = 1_000 |
| 5 | const bench = new Bench({ |
| 6 | iterations: 1_000, |
| 7 | time: 0, |
| 8 | warmupIterations: 100, |
| 9 | warmupTime: 0, |
| 10 | timestampProvider: "hrtimeNow" |
| 11 | }) |
| 12 | const issue = new SchemaIssue.InvalidValue({ message: "Expected string" }) |
| 13 | let sink: Schema.SchemaError | undefined |
| 14 | |
| 15 | bench.add("SchemaError construction", () => { |
| 16 | let error = new Schema.SchemaError(issue) |
| 17 | for (let index = 1; index < batchSize; index++) { |
| 18 | error = new Schema.SchemaError(issue) |
| 19 | } |
| 20 | sink = error |
| 21 | }) |
| 22 | |
| 23 | await bench.run() |
| 24 | |
| 25 | if (sink === undefined) { |
| 26 | throw new Error("Benchmark did not run") |
| 27 | } |
| 28 | |
| 29 | console.table(bench.table((task) => { |
| 30 | const result = task.result |
| 31 | if (result?.state !== "completed") { |
| 32 | return { |
| 33 | "Task name": task.name, |
| 34 | State: result?.state ?? "missing result" |
| 35 | } |
| 36 | } |
| 37 | const latencyToNs = (value: number) => value * 1_000_000 / batchSize |
| 38 | return { |
| 39 | "Task name": task.name, |
| 40 | "Latency avg (ns/op)": latencyToNs(result.latency.mean).toFixed(2), |
| 41 | "Latency med (ns/op)": latencyToNs(result.latency.p50).toFixed(2), |
| 42 | "Latency RME": `${result.latency.rme.toFixed(2)}%`, |
| 43 | "Throughput avg (ops/s)": Math.round(result.throughput.mean * batchSize), |
| 44 | Samples: result.latency.samplesCount |
| 45 | } |
| 46 | })) |