1import { Schema, SchemaIssue } from "effect"
2import { Bench } from "tinybench"
3
4const batchSize = 1_000
5const bench = new Bench({
6 iterations: 1_000,
7 time: 0,
8 warmupIterations: 100,
9 warmupTime: 0,
10 timestampProvider: "hrtimeNow"
11})
12const issue = new SchemaIssue.InvalidValue({ message: "Expected string" })
13let sink: Schema.SchemaError | undefined
14
15bench.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
23await bench.run()
24
25if (sink === undefined) {
26 throw new Error("Benchmark did not run")
27}
28
29console.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}))