aboutsummaryrefslogtreecommitdiffstats
path: root/packages/benchmarks/src/benchmarks.ts
blob: f288324614d1d22439c2a2d2d289f82b1af02e4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { TaskResult } from "tinybench";
import { Bench } from "tinybench";

import type { SeedResult } from "./seed";
import { logInfo, logStep, logSuccess } from "./log";
import { formatMs, formatNumber } from "./utils";

// Type guard for completed task results
type CompletedTaskResult = Extract<TaskResult, { state: "completed" }>;

export interface BenchmarkRow {
  name: string;
  ops: number;
  mean: number;
  p75: number;
  p99: number;
  samples: number;
}

export interface BenchmarkOptions {
  timeMs?: number;
  warmupMs?: number;
}

export async function runBenchmarks(
  seed: SeedResult,
  options?: BenchmarkOptions,
): Promise<BenchmarkRow[]> {
  const bench = new Bench({
    time: options?.timeMs ?? 1000,
    warmupTime: options?.warmupMs ?? 300,
  });

  const sampleTag = seed.tags[0];
  const sampleList = seed.lists[0];
  const sampleIds = seed.bookmarks.slice(0, 50).map((b) => b.id);

  bench.add("bookmarks.getBookmarks (page)", async () => {
    await seed.trpc.bookmarks.getBookmarks.query({
      limit: 50,
    });
  });

  if (sampleTag) {
    bench.add("bookmarks.getBookmarks (tag filter)", async () => {
      await seed.trpc.bookmarks.getBookmarks.query({
        limit: 50,
        tagId: sampleTag.id,
      });
    });
  }

  if (sampleList) {
    bench.add("bookmarks.getBookmarks (list filter)", async () => {
      await seed.trpc.bookmarks.getBookmarks.query({
        limit: 50,
        listId: sampleList.id,
      });
    });
  }

  if (sampleList && sampleIds.length > 0) {
    bench.add("lists.getListsOfBookmark", async () => {
      await seed.trpc.lists.getListsOfBookmark.query({
        bookmarkId: sampleIds[0],
      });
    });
  }

  bench.add("bookmarks.searchBookmarks", async () => {
    await seed.trpc.bookmarks.searchBookmarks.query({
      text: seed.searchTerm,
      limit: 20,
    });
  });

  bench.add("bookmarks.getBookmarks (by ids)", async () => {
    await seed.trpc.bookmarks.getBookmarks.query({
      ids: sampleIds.slice(0, 20),
      includeContent: false,
    });
  });

  logStep("Running benchmarks");
  await bench.run();
  logSuccess("Benchmarks complete");

  const rows = bench.tasks
    .map((task) => {
      const result = task.result;

      // Check for errored state
      if ("error" in result) {
        console.error(`\n⚠️  Benchmark "${task.name}" failed with error:`);
        console.error(result.error);
        return null;
      }

      // Check if task completed successfully
      if (result.state !== "completed") {
        console.warn(
          `\n⚠️  Benchmark "${task.name}" did not complete. State: ${result.state}`,
        );
        return null;
      }

      return toRow(task.name, result);
    })
    .filter(Boolean) as BenchmarkRow[];

  renderTable(rows);
  logInfo(
    "ops/s uses tinybench's hz metric; durations are recorded in milliseconds.",
  );

  return rows;
}

function toRow(name: string, result: CompletedTaskResult): BenchmarkRow {
  // The statistics are now in result.latency and result.throughput
  const latency = result.latency;
  const throughput = result.throughput;

  return {
    name,
    ops: throughput.mean, // ops/s is the mean throughput
    mean: latency.mean,
    p75: latency.p75,
    p99: latency.p99,
    samples: latency.samplesCount,
  };
}

function renderTable(rows: BenchmarkRow[]): void {
  const headers = ["Benchmark", "ops/s", "avg", "p75", "p99", "samples"];

  const data = rows.map((row) => [
    row.name,
    formatNumber(row.ops, 1),
    formatMs(row.mean),
    formatMs(row.p75),
    formatMs(row.p99),
    String(row.samples),
  ]);

  const columnWidths = headers.map((header, index) =>
    Math.max(header.length, ...data.map((row) => row[index].length)),
  );

  const formatRow = (cells: string[]): string =>
    cells.map((cell, index) => cell.padEnd(columnWidths[index])).join("  ");

  console.log("");
  console.log(formatRow(headers));
  console.log(columnWidths.map((width) => "-".repeat(width)).join("  "));
  data.forEach((row) => console.log(formatRow(row)));
  console.log("");
}