@wxn0brp/db-benchmark 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -2
- package/package.json +34 -5
- package/src/custom/dir-index.ts +7 -0
- package/src/custom/sqlite.ts +41 -0
- package/src/db.ts +13 -0
- package/src/generate-static.ts +35 -0
- package/src/generate.ts +50 -0
- package/src/index.ts +31 -0
- package/src/large.ts +107 -0
- package/src/local/args.ts +63 -0
- package/src/local/cloud.ts +143 -0
- package/src/local/compare.ts +159 -0
- package/src/local/display.ts +84 -0
- package/src/local/index.ts +113 -0
- package/src/page/cache.ts +33 -0
- package/src/page/compare.ts +435 -0
- package/src/page/index.ts +113 -0
- package/src/page/local.ts +188 -0
- package/src/page/summary.ts +164 -0
- package/src/page/tables.ts +212 -0
- package/src/page/types.ts +33 -0
- package/src/page/utils.ts +58 -0
- package/src/page/var.ts +31 -0
- package/src/run.ts +25 -0
- package/src/small.ts +95 -0
- package/src/utils.ts +71 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { type BenchResult } from "../run";
|
|
2
|
+
import { type CloudAverages, type OpAverage } from "./cloud";
|
|
3
|
+
import { formatTime } from "./display";
|
|
4
|
+
|
|
5
|
+
export interface ComparisonRow {
|
|
6
|
+
op: string;
|
|
7
|
+
local: number;
|
|
8
|
+
overall?: OpAverage;
|
|
9
|
+
adapter?: OpAverage;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface Diff {
|
|
13
|
+
text: string;
|
|
14
|
+
color: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function buildComparisonRows(
|
|
18
|
+
localResults: BenchResult[],
|
|
19
|
+
cloud: CloudAverages,
|
|
20
|
+
adapterName: string,
|
|
21
|
+
): ComparisonRow[] {
|
|
22
|
+
const adapterAvg = cloud.adapters.get(adapterName);
|
|
23
|
+
|
|
24
|
+
return localResults.map(r => ({
|
|
25
|
+
op: r.name,
|
|
26
|
+
local: r.time,
|
|
27
|
+
overall: cloud.overall.get(r.name),
|
|
28
|
+
adapter: adapterAvg?.get(r.name),
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function diff(local: number, reference: number): Diff {
|
|
33
|
+
const ratio = local / reference;
|
|
34
|
+
const isTiny = Math.min(local, reference) < 1;
|
|
35
|
+
const equalRatio = isTiny ? 1.25 : 1.05;
|
|
36
|
+
|
|
37
|
+
if (ratio >= 1 / equalRatio && ratio <= equalRatio) {
|
|
38
|
+
return {
|
|
39
|
+
text: "≈ equal",
|
|
40
|
+
color: "\x1b[2m",
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (ratio < 1) {
|
|
45
|
+
return {
|
|
46
|
+
text: `${(1 / ratio).toFixed(2)}x faster`,
|
|
47
|
+
color: "\x1b[32m",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
text: `${ratio.toFixed(2)}x slower`,
|
|
53
|
+
color: "\x1b[31m",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function colorCell(d: Diff | null, width: number) {
|
|
58
|
+
const text = d?.text ?? "-";
|
|
59
|
+
const padded = text.padEnd(width);
|
|
60
|
+
return d ? `${d.color}${padded}\x1b[0m` : padded;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function printComparisonTable(
|
|
64
|
+
rows: ComparisonRow[],
|
|
65
|
+
adapterName: string,
|
|
66
|
+
) {
|
|
67
|
+
const adapterHeader = `${adapterName} avg`;
|
|
68
|
+
|
|
69
|
+
const localTimes = rows.map(r => formatTime(r.local));
|
|
70
|
+
const overallTimes = rows.map(r =>
|
|
71
|
+
r.overall ? formatTime(r.overall.avg) : "-",
|
|
72
|
+
);
|
|
73
|
+
const adapterTimes = rows.map(r =>
|
|
74
|
+
r.adapter ? formatTime(r.adapter.avg) : "-",
|
|
75
|
+
);
|
|
76
|
+
const overallDiffs = rows.map(r =>
|
|
77
|
+
r.overall ? diff(r.local, r.overall.avg) : null,
|
|
78
|
+
);
|
|
79
|
+
const adapterDiffs = rows.map(r =>
|
|
80
|
+
r.adapter ? diff(r.local, r.adapter.avg) : null,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const W_OP = Math.max("Test".length, ...rows.map(r => r.op.length));
|
|
84
|
+
const W_LOCAL = Math.max("Local".length, ...localTimes.map(t => t.length));
|
|
85
|
+
const W_OVERALL = Math.max(
|
|
86
|
+
"Overall avg".length,
|
|
87
|
+
...overallTimes.map(t => t.length),
|
|
88
|
+
);
|
|
89
|
+
const W_OVERALL_DIFF = Math.max(
|
|
90
|
+
"vs overall".length,
|
|
91
|
+
...overallDiffs.map(d => d?.text.length ?? 1),
|
|
92
|
+
);
|
|
93
|
+
const W_ADAPTER = Math.max(
|
|
94
|
+
adapterHeader.length,
|
|
95
|
+
...adapterTimes.map(t => t.length),
|
|
96
|
+
);
|
|
97
|
+
const W_ADAPTER_DIFF = Math.max(
|
|
98
|
+
"vs adapter".length,
|
|
99
|
+
...adapterDiffs.map(d => d?.text.length ?? 1),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const line = (l: string, m: string, r: string) =>
|
|
103
|
+
l +
|
|
104
|
+
"─".repeat(W_OP + 2) +
|
|
105
|
+
m +
|
|
106
|
+
"─".repeat(W_LOCAL + 2) +
|
|
107
|
+
m +
|
|
108
|
+
"─".repeat(W_OVERALL + 2) +
|
|
109
|
+
m +
|
|
110
|
+
"─".repeat(W_OVERALL_DIFF + 2) +
|
|
111
|
+
m +
|
|
112
|
+
"─".repeat(W_ADAPTER + 2) +
|
|
113
|
+
m +
|
|
114
|
+
"─".repeat(W_ADAPTER_DIFF + 2) +
|
|
115
|
+
r;
|
|
116
|
+
|
|
117
|
+
const row = (
|
|
118
|
+
op: string,
|
|
119
|
+
local: string,
|
|
120
|
+
overall: string,
|
|
121
|
+
overallDiff: Diff | null,
|
|
122
|
+
adapter: string,
|
|
123
|
+
adapterDiff: Diff | null,
|
|
124
|
+
) =>
|
|
125
|
+
`│ ${op.padEnd(W_OP)} │ ${local.padStart(W_LOCAL)} │ ${overall.padStart(W_OVERALL)} │ ${colorCell(overallDiff, W_OVERALL_DIFF)} │ ${adapter.padStart(W_ADAPTER)} │ ${colorCell(adapterDiff, W_ADAPTER_DIFF)} │`;
|
|
126
|
+
|
|
127
|
+
console.log();
|
|
128
|
+
console.log("\x1b[1m--- Comparison with cloud ---\x1b[0m");
|
|
129
|
+
|
|
130
|
+
if (!rows.some(r => r.adapter)) {
|
|
131
|
+
console.log(
|
|
132
|
+
`\x1b[2m No cloud measurements found for adapter "${adapterName}"\x1b[0m`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
console.log();
|
|
137
|
+
console.log(line("┌", "┬", "┐"));
|
|
138
|
+
console.log(
|
|
139
|
+
`│ ${"Test".padEnd(W_OP)} │ ${"Local".padStart(W_LOCAL)} │ ${"Overall avg".padStart(W_OVERALL)} │ ${"vs overall".padEnd(W_OVERALL_DIFF)} │ ${adapterHeader.padStart(W_ADAPTER)} │ ${"vs adapter".padEnd(W_ADAPTER_DIFF)} │`,
|
|
140
|
+
);
|
|
141
|
+
console.log(line("├", "┼", "┤"));
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < rows.length; i++) {
|
|
144
|
+
const r = rows[i];
|
|
145
|
+
console.log(
|
|
146
|
+
row(
|
|
147
|
+
r.op,
|
|
148
|
+
localTimes[i],
|
|
149
|
+
overallTimes[i],
|
|
150
|
+
overallDiffs[i],
|
|
151
|
+
adapterTimes[i],
|
|
152
|
+
adapterDiffs[i],
|
|
153
|
+
),
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
console.log(line("└", "┴", "┘"));
|
|
158
|
+
console.log();
|
|
159
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type BenchResult } from "../run";
|
|
2
|
+
|
|
3
|
+
export function formatTime(timeMs: number) {
|
|
4
|
+
if (timeMs < 1) return `${(timeMs * 1000).toFixed(2)}μs`;
|
|
5
|
+
if (timeMs < 1000) return `${timeMs.toFixed(2)}ms`;
|
|
6
|
+
return `${(timeMs / 1000).toFixed(2)}s`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function printHeader(adapterName: string, adapterPath: string) {
|
|
10
|
+
console.log();
|
|
11
|
+
console.log("──────────────────────────────────────────────────");
|
|
12
|
+
console.log("ValtheraDB Benchmark Runner");
|
|
13
|
+
console.log("──────────────────────────────────────────────────");
|
|
14
|
+
console.log(` Adapter: ${adapterName}`);
|
|
15
|
+
console.log(` Path: ${adapterPath}`);
|
|
16
|
+
console.log(` Date: ${new Date().toISOString()}`);
|
|
17
|
+
console.log("──────────────────────────────────────────────────");
|
|
18
|
+
console.log();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function printResultsTable(results: BenchResult[]) {
|
|
22
|
+
const W_NAME = 25;
|
|
23
|
+
const W_TIME = 10;
|
|
24
|
+
const W_BAR = 25;
|
|
25
|
+
|
|
26
|
+
const times = results.map(r => r.time);
|
|
27
|
+
const avg = times.reduce((a, b) => a + b, 0) / times.length;
|
|
28
|
+
const min = Math.min(...times);
|
|
29
|
+
const max = Math.max(...times);
|
|
30
|
+
|
|
31
|
+
const logMin = Math.log(min);
|
|
32
|
+
const logRange = Math.log(max) - logMin || 1;
|
|
33
|
+
|
|
34
|
+
const line = (l: string, m: string, r: string) =>
|
|
35
|
+
l +
|
|
36
|
+
"─".repeat(W_NAME + 2) +
|
|
37
|
+
m +
|
|
38
|
+
"─".repeat(W_TIME + 2) +
|
|
39
|
+
m +
|
|
40
|
+
"─".repeat(W_BAR + 2) +
|
|
41
|
+
r;
|
|
42
|
+
|
|
43
|
+
const row = (a: string, b: string, c: string) =>
|
|
44
|
+
`│ ${a.padEnd(W_NAME)} │ ${b.padStart(W_TIME)} │ ${c.padEnd(W_BAR)} │`;
|
|
45
|
+
|
|
46
|
+
const bar = (t: number) => {
|
|
47
|
+
const pos = (Math.log(t) - logMin) / logRange;
|
|
48
|
+
const len = Math.max(1, Math.round(pos * W_BAR));
|
|
49
|
+
|
|
50
|
+
const ratio = t / avg;
|
|
51
|
+
const color =
|
|
52
|
+
ratio < 0.5 ? "\x1b[32m" : ratio < 2 ? "\x1b[33m" : "\x1b[31m";
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
color +
|
|
56
|
+
"█".repeat(len) +
|
|
57
|
+
"\x1b[0m" +
|
|
58
|
+
"\x1b[2m" +
|
|
59
|
+
"░".repeat(W_BAR - len) +
|
|
60
|
+
"\x1b[0m"
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
console.log();
|
|
65
|
+
console.log(line("┌", "┬", "┐"));
|
|
66
|
+
console.log(row("Test", "Time", "Distribution"));
|
|
67
|
+
console.log(line("├", "┼", "┤"));
|
|
68
|
+
|
|
69
|
+
for (const r of results)
|
|
70
|
+
console.log(row(r.name, formatTime(r.time), bar(r.time)));
|
|
71
|
+
|
|
72
|
+
console.log(line("└", "┴", "┘"));
|
|
73
|
+
console.log();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function printSummary(allResults: BenchResult[], adapterName: string) {
|
|
77
|
+
const totalTime = allResults.reduce((sum, r) => sum + r.time, 0);
|
|
78
|
+
console.log();
|
|
79
|
+
console.log(
|
|
80
|
+
`\x1b[32m✓\x1b[0m Completed ${allResults.length} benchmarks for ${adapterName}`,
|
|
81
|
+
);
|
|
82
|
+
console.log(` Total time: ${formatTime(totalTime)}`);
|
|
83
|
+
console.log();
|
|
84
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { ValtheraClass } from "@wxn0brp/db-core";
|
|
4
|
+
import { existsSync } from "fs";
|
|
5
|
+
import { basename, dirname, resolve } from "path";
|
|
6
|
+
import { benchmarkLarge } from "../large";
|
|
7
|
+
import { type BenchResult } from "../run";
|
|
8
|
+
import { benchmarkSmall } from "../small";
|
|
9
|
+
import { loadCustomSetup, writeResults } from "../utils";
|
|
10
|
+
import { parseCliOptions, printHelp } from "./args";
|
|
11
|
+
import { buildCloudAverages, fetchCloudEntries } from "./cloud";
|
|
12
|
+
import { buildComparisonRows, printComparisonTable } from "./compare";
|
|
13
|
+
import { printHeader, printResultsTable, printSummary } from "./display";
|
|
14
|
+
|
|
15
|
+
const options = parseCliOptions();
|
|
16
|
+
|
|
17
|
+
if (options.help) {
|
|
18
|
+
printHelp();
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const adapterPath = resolve(process.cwd(), options.adapterPathArg);
|
|
23
|
+
|
|
24
|
+
if (!existsSync(adapterPath)) {
|
|
25
|
+
console.error(`Adapter file not found: ${adapterPath}`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const pathParts = adapterPath.split("/");
|
|
30
|
+
const valtheraE2eIndex = pathParts.lastIndexOf("valthera-e2e");
|
|
31
|
+
const adapterName =
|
|
32
|
+
valtheraE2eIndex > 0
|
|
33
|
+
? pathParts[valtheraE2eIndex - 1]
|
|
34
|
+
: basename(dirname(adapterPath));
|
|
35
|
+
|
|
36
|
+
const adapterModule = await import(adapterPath);
|
|
37
|
+
const adapterFactory = adapterModule.default;
|
|
38
|
+
|
|
39
|
+
if (typeof adapterFactory !== "function") {
|
|
40
|
+
console.error(`Invalid adapter factory in ${adapterPath}`);
|
|
41
|
+
console.error("Expected default export to be a function");
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
printHeader(adapterName, adapterPath);
|
|
46
|
+
|
|
47
|
+
console.log(" Loading adapter...");
|
|
48
|
+
const adapter = await adapterFactory();
|
|
49
|
+
|
|
50
|
+
if (!adapter) {
|
|
51
|
+
console.error("Adapter factory returned null");
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const db = new ValtheraClass({
|
|
56
|
+
adapter,
|
|
57
|
+
});
|
|
58
|
+
await db.init();
|
|
59
|
+
|
|
60
|
+
const onAfterAdd = await loadCustomSetup(adapterName, db.adapter);
|
|
61
|
+
|
|
62
|
+
console.log();
|
|
63
|
+
|
|
64
|
+
const allResults: BenchResult[] = [];
|
|
65
|
+
|
|
66
|
+
console.log("\x1b[1m--- Small collection (users, 10k) ---\x1b[0m");
|
|
67
|
+
const smallResults = await benchmarkSmall(db.c("users"));
|
|
68
|
+
allResults.push(...smallResults);
|
|
69
|
+
console.log();
|
|
70
|
+
|
|
71
|
+
if (!options.smallOnly) {
|
|
72
|
+
console.log("\x1b[1m--- Large collection (posts, 200k) ---\x1b[0m");
|
|
73
|
+
const largeResults = await benchmarkLarge(db.c("posts"), onAfterAdd);
|
|
74
|
+
allResults.push(...largeResults);
|
|
75
|
+
console.log();
|
|
76
|
+
} else {
|
|
77
|
+
console.log("\x1b[2m (small-only mode)\x1b[0m");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
console.log("\x1b[1m--- Results ---\x1b[0m");
|
|
81
|
+
printResultsTable(allResults);
|
|
82
|
+
|
|
83
|
+
printSummary(allResults, adapterName);
|
|
84
|
+
|
|
85
|
+
if (!options.noCloud) {
|
|
86
|
+
console.log(" Loading cloud results...");
|
|
87
|
+
try {
|
|
88
|
+
const cloudEntries = await fetchCloudEntries();
|
|
89
|
+
const cloudAverages = buildCloudAverages(cloudEntries);
|
|
90
|
+
const comparison = buildComparisonRows(
|
|
91
|
+
allResults,
|
|
92
|
+
cloudAverages,
|
|
93
|
+
adapterName,
|
|
94
|
+
);
|
|
95
|
+
printComparisonTable(comparison, adapterName);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.warn(`\x1b[33m!\x1b[0m Could not load cloud results: ${e.message}`);
|
|
98
|
+
console.warn(" Use --no-cloud to skip this comparison.");
|
|
99
|
+
console.log();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (options.jsonOutput) {
|
|
104
|
+
writeResults(options.jsonFile, {
|
|
105
|
+
results: allResults,
|
|
106
|
+
adapter: adapterName,
|
|
107
|
+
coreVersion: db.version,
|
|
108
|
+
adapterVersion: db.adapter?.version || "N/A",
|
|
109
|
+
});
|
|
110
|
+
console.log(`\x1b[2mResults saved to ${options.jsonFile}\x1b[0m`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
await db.close();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CacheData } from "./types";
|
|
2
|
+
import { state } from "./var";
|
|
3
|
+
|
|
4
|
+
const CACHE_KEY = "benchmark-cache";
|
|
5
|
+
const CACHE_TTL = 10 * 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export function loadFromCache() {
|
|
8
|
+
try {
|
|
9
|
+
const raw = localStorage.getItem(CACHE_KEY);
|
|
10
|
+
if (!raw) return null;
|
|
11
|
+
const data: CacheData = JSON.parse(raw);
|
|
12
|
+
if (Date.now() - data.timestamp > CACHE_TTL) {
|
|
13
|
+
localStorage.removeItem(CACHE_KEY);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return data;
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function saveToCache() {
|
|
23
|
+
const payload: CacheData = {
|
|
24
|
+
timestamp: Date.now(),
|
|
25
|
+
...state,
|
|
26
|
+
};
|
|
27
|
+
localStorage.setItem(CACHE_KEY, JSON.stringify(payload));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function clearCache() {
|
|
31
|
+
localStorage.removeItem(CACHE_KEY);
|
|
32
|
+
location.reload();
|
|
33
|
+
}
|