@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,188 @@
|
|
|
1
|
+
import { renderCompare } from "./compare";
|
|
2
|
+
import { renderSummary } from "./summary";
|
|
3
|
+
import { renderTables } from "./tables";
|
|
4
|
+
import { Entry, ResultFile } from "./types";
|
|
5
|
+
import { normalize, parseEntry } from "./utils";
|
|
6
|
+
import { state } from "./var";
|
|
7
|
+
|
|
8
|
+
const LOCAL_KEY = "benchmark-local";
|
|
9
|
+
|
|
10
|
+
interface LocalStorageData {
|
|
11
|
+
entries: Entry[];
|
|
12
|
+
adapterNames: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getLocalData(): LocalStorageData {
|
|
16
|
+
try {
|
|
17
|
+
const raw = localStorage.getItem(LOCAL_KEY);
|
|
18
|
+
if (!raw)
|
|
19
|
+
return {
|
|
20
|
+
entries: [],
|
|
21
|
+
adapterNames: {},
|
|
22
|
+
};
|
|
23
|
+
const parsed = JSON.parse(raw);
|
|
24
|
+
if (Array.isArray(parsed)) {
|
|
25
|
+
return {
|
|
26
|
+
entries: parsed,
|
|
27
|
+
adapterNames: {},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
entries: parsed.entries || [],
|
|
32
|
+
adapterNames: parsed.adapterNames || {},
|
|
33
|
+
};
|
|
34
|
+
} catch {
|
|
35
|
+
return {
|
|
36
|
+
entries: [],
|
|
37
|
+
adapterNames: {},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function saveLocalData(data: LocalStorageData) {
|
|
43
|
+
localStorage.setItem(LOCAL_KEY, JSON.stringify(data));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getLocalResults(): Entry[] {
|
|
47
|
+
return getLocalData().entries;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function saveLocalResults(entries: Entry[]) {
|
|
51
|
+
const data = getLocalData();
|
|
52
|
+
data.entries = entries;
|
|
53
|
+
saveLocalData(data);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function saveLocalAdapterName(adapterId: string, name: string) {
|
|
57
|
+
const data = getLocalData();
|
|
58
|
+
data.adapterNames[adapterId] = name;
|
|
59
|
+
saveLocalData(data);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildDirFromResult(data: ResultFile): string {
|
|
63
|
+
const adapter = data.adapter || "local";
|
|
64
|
+
const runtime = data.bun ? "bun" : data.node ? data.node : "local";
|
|
65
|
+
const os = data.os || "local";
|
|
66
|
+
return `${adapter}-local__${runtime}__${os}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function addLocalResult(file: File): Promise<boolean> {
|
|
70
|
+
try {
|
|
71
|
+
const text = await file.text();
|
|
72
|
+
const data: ResultFile = JSON.parse(text);
|
|
73
|
+
|
|
74
|
+
if (!data.results || !Array.isArray(data.results)) {
|
|
75
|
+
alert("Invalid result file format: missing results array");
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
data.results = normalize(data.results);
|
|
80
|
+
const dir = buildDirFromResult(data);
|
|
81
|
+
const info = parseEntry(dir);
|
|
82
|
+
|
|
83
|
+
const baseAdapter = data.adapter || "local";
|
|
84
|
+
const localAdapterName = `${state.adapterConfig[baseAdapter] || baseAdapter} (Local)`;
|
|
85
|
+
state.adapterConfig[info.adapter] = localAdapterName;
|
|
86
|
+
saveLocalAdapterName(info.adapter, localAdapterName);
|
|
87
|
+
|
|
88
|
+
const entry: Entry = {
|
|
89
|
+
dir,
|
|
90
|
+
info,
|
|
91
|
+
data,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const locals = getLocalResults();
|
|
95
|
+
const existing = locals.findIndex(e => e.dir === dir);
|
|
96
|
+
if (existing >= 0) {
|
|
97
|
+
locals[existing] = entry;
|
|
98
|
+
} else {
|
|
99
|
+
locals.push(entry);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
saveLocalResults(locals);
|
|
103
|
+
state.entries.push(entry);
|
|
104
|
+
|
|
105
|
+
renderSummary();
|
|
106
|
+
renderCompare();
|
|
107
|
+
renderTables();
|
|
108
|
+
|
|
109
|
+
return true;
|
|
110
|
+
} catch (e) {
|
|
111
|
+
console.error("Failed to load local result", e);
|
|
112
|
+
alert(`Failed to load file: ${e.message}`);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function removeLocalResult(dir: string) {
|
|
118
|
+
const locals = getLocalResults();
|
|
119
|
+
const filtered = locals.filter(e => e.dir !== dir);
|
|
120
|
+
saveLocalResults(filtered);
|
|
121
|
+
|
|
122
|
+
state.entries = state.entries.filter(e => e.dir !== dir);
|
|
123
|
+
|
|
124
|
+
renderSummary();
|
|
125
|
+
renderCompare();
|
|
126
|
+
renderTables();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function renderLocalResultsList() {
|
|
130
|
+
const container = document.querySelector<HTMLDivElement>(
|
|
131
|
+
"#local-results-list",
|
|
132
|
+
);
|
|
133
|
+
if (!container) return;
|
|
134
|
+
|
|
135
|
+
const locals = getLocalResults();
|
|
136
|
+
if (!locals.length) {
|
|
137
|
+
container.innerHTML =
|
|
138
|
+
"<p style='color: var(--pico-muted-color); font-size: 0.9rem;'>No local results loaded</p>";
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let html = "";
|
|
143
|
+
for (const entry of locals) {
|
|
144
|
+
const adapterName =
|
|
145
|
+
state.adapterConfig[entry.info.adapter] || entry.info.adapter;
|
|
146
|
+
const runtime = entry.info.ver === "bun" ? "bun" : `node ${entry.info.ver}`;
|
|
147
|
+
html += `
|
|
148
|
+
<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem; margin-bottom: 0.5rem; background: var(--pico-muted-border-color); border-radius: 4px;">
|
|
149
|
+
<div>
|
|
150
|
+
<strong>${adapterName}</strong>
|
|
151
|
+
<span style="color: var(--pico-muted-color); margin-left: 0.5rem;">${runtime} / ${entry.info.os}</span>
|
|
152
|
+
</div>
|
|
153
|
+
<button class="outline contrast" style="padding: 0.15rem 0.5rem; font-size: 0.75rem;" data-dir="${entry.dir}">
|
|
154
|
+
Remove
|
|
155
|
+
</button>
|
|
156
|
+
</div>
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
container.innerHTML = html;
|
|
161
|
+
|
|
162
|
+
container.querySelectorAll("button[data-dir]").forEach(btn => {
|
|
163
|
+
btn.addEventListener("click", () => {
|
|
164
|
+
const dir = btn.getAttribute("data-dir");
|
|
165
|
+
if (dir) {
|
|
166
|
+
removeLocalResult(dir);
|
|
167
|
+
renderLocalResultsList();
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function loadLocalResultsIntoState() {
|
|
174
|
+
const localData = getLocalData();
|
|
175
|
+
|
|
176
|
+
for (const [adapterId, name] of Object.entries(localData.adapterNames)) {
|
|
177
|
+
state.adapterConfig[adapterId] = name;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
for (const local of localData.entries) {
|
|
181
|
+
const existing = state.entries.findIndex(e => e.dir === local.dir);
|
|
182
|
+
if (existing >= 0) {
|
|
183
|
+
state.entries[existing] = local;
|
|
184
|
+
} else {
|
|
185
|
+
state.entries.push(local);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { adapterName, fmt, parseEntry, threshold } from "./utils";
|
|
2
|
+
import { state } from "./var";
|
|
3
|
+
|
|
4
|
+
interface BenchmarkEntry {
|
|
5
|
+
dir: string;
|
|
6
|
+
time: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function groupByThreshold(sorted: BenchmarkEntry[]) {
|
|
10
|
+
if (!sorted.length) return [];
|
|
11
|
+
|
|
12
|
+
const groups: BenchmarkEntry[][] = [];
|
|
13
|
+
let cur = [
|
|
14
|
+
sorted[0],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
18
|
+
const prev = cur[cur.length - 1];
|
|
19
|
+
const t = threshold(prev.time);
|
|
20
|
+
if ((sorted[i].time - prev.time) / prev.time <= t) {
|
|
21
|
+
cur.push(sorted[i]);
|
|
22
|
+
} else {
|
|
23
|
+
groups.push(cur);
|
|
24
|
+
cur = [
|
|
25
|
+
sorted[i],
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
groups.push(cur);
|
|
31
|
+
|
|
32
|
+
return groups;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function renderSummary() {
|
|
36
|
+
const notRanked = [
|
|
37
|
+
"memory",
|
|
38
|
+
];
|
|
39
|
+
const rankingEntries = state.entries.filter(
|
|
40
|
+
e => !notRanked.includes(e.info.adapter),
|
|
41
|
+
);
|
|
42
|
+
const allOps = [
|
|
43
|
+
...new Set(rankingEntries.flatMap(e => e.data.results.map(r => r.name))),
|
|
44
|
+
].sort();
|
|
45
|
+
|
|
46
|
+
const container = document.querySelector<HTMLDivElement>("#summary-cards");
|
|
47
|
+
container.innerHTML = "";
|
|
48
|
+
|
|
49
|
+
const modal = document.querySelector<HTMLDialogElement>("#summary-modal");
|
|
50
|
+
const modalTitle = document.querySelector<HTMLSpanElement>(
|
|
51
|
+
"#summary-modal-title",
|
|
52
|
+
);
|
|
53
|
+
const modalContent = document.querySelector<HTMLDivElement>(
|
|
54
|
+
"#summary-modal-content",
|
|
55
|
+
);
|
|
56
|
+
const modalClose = document.querySelector<HTMLButtonElement>(
|
|
57
|
+
"#summary-modal-close",
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
modalClose?.addEventListener("click", () => modal?.close());
|
|
61
|
+
modal?.addEventListener("click", e => {
|
|
62
|
+
if (e.target === modal) modal.close();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
for (const op of allOps) {
|
|
66
|
+
const withTime = rankingEntries
|
|
67
|
+
.map(e => ({
|
|
68
|
+
dir: e.dir,
|
|
69
|
+
time: e.data.results.find(r => r.name === op)?.time,
|
|
70
|
+
}))
|
|
71
|
+
.filter(
|
|
72
|
+
(
|
|
73
|
+
x,
|
|
74
|
+
): x is {
|
|
75
|
+
dir: string;
|
|
76
|
+
time: number;
|
|
77
|
+
} => x.time !== undefined,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (!withTime.length) continue;
|
|
81
|
+
|
|
82
|
+
let cardHTML = `<article><header><strong>${op}</strong></header>`;
|
|
83
|
+
|
|
84
|
+
const sorted = withTime.sort((a, b) => a.time - b.time);
|
|
85
|
+
const groups = groupByThreshold(sorted);
|
|
86
|
+
|
|
87
|
+
const visibleCount = Math.min(3, groups.length);
|
|
88
|
+
const visibleGroups = groups.slice(0, visibleCount);
|
|
89
|
+
const visibleMax = Math.max(
|
|
90
|
+
...visibleGroups.map(g => g.reduce((s, x) => s + x.time, 0) / g.length),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < visibleCount; i++) {
|
|
94
|
+
const avg =
|
|
95
|
+
visibleGroups[i].reduce((s, x) => s + x.time, 0) /
|
|
96
|
+
visibleGroups[i].length;
|
|
97
|
+
const barWidth = visibleMax > 0 ? (avg / visibleMax) * 100 : 0;
|
|
98
|
+
|
|
99
|
+
const names = visibleGroups[i]
|
|
100
|
+
.map(x => {
|
|
101
|
+
const info = parseEntry(x.dir);
|
|
102
|
+
return `${adapterName(info.adapter)}/${info.ver}`;
|
|
103
|
+
})
|
|
104
|
+
.join(", ");
|
|
105
|
+
|
|
106
|
+
cardHTML += `
|
|
107
|
+
<div style="margin-bottom: 0.75rem;">
|
|
108
|
+
<div style="display: flex; justify-content: space-between; margin-bottom: 0.25rem;">
|
|
109
|
+
<span class="adapter">${names}</span>
|
|
110
|
+
<span class="time">${fmt(avg)}</span>
|
|
111
|
+
</div>
|
|
112
|
+
<div class="bar-track">
|
|
113
|
+
<div class="bar-fill" style="width: ${barWidth}%; background: var(--pico-primary);"></div>
|
|
114
|
+
</div>
|
|
115
|
+
</div>`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (groups.length > 3) {
|
|
119
|
+
const allData = JSON.stringify(
|
|
120
|
+
groups.map(g => ({
|
|
121
|
+
names: g
|
|
122
|
+
.map(x => {
|
|
123
|
+
const info = parseEntry(x.dir);
|
|
124
|
+
return `${adapterName(info.adapter)}/${info.ver}`;
|
|
125
|
+
})
|
|
126
|
+
.join(", "),
|
|
127
|
+
avg: g.reduce((s, x) => s + x.time, 0) / g.length,
|
|
128
|
+
})),
|
|
129
|
+
);
|
|
130
|
+
cardHTML += `<button class="outline contrast show-all-btn" data-op="${op}" data-all='${allData}'>Show all (${groups.length})</button>`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
cardHTML += `</article>`;
|
|
134
|
+
container.innerHTML += cardHTML;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
container.querySelectorAll(".show-all-btn").forEach(btn => {
|
|
138
|
+
btn.addEventListener("click", () => {
|
|
139
|
+
const op = btn.getAttribute("data-op");
|
|
140
|
+
const allData = JSON.parse(btn.getAttribute("data-all") || "[]");
|
|
141
|
+
const maxTime = Math.max(...allData.map((d: { avg: number }) => d.avg));
|
|
142
|
+
|
|
143
|
+
if (modalTitle) modalTitle.textContent = op ?? "";
|
|
144
|
+
if (modalContent) {
|
|
145
|
+
modalContent.innerHTML = allData
|
|
146
|
+
.map((d: { names: string; avg: number }) => {
|
|
147
|
+
const barWidth = maxTime > 0 ? (d.avg / maxTime) * 100 : 0;
|
|
148
|
+
return `
|
|
149
|
+
<div style="margin-bottom: 0.75rem;">
|
|
150
|
+
<div style="display: flex; justify-content: space-between; margin-bottom: 0.25rem;">
|
|
151
|
+
<span class="adapter">${d.names}</span>
|
|
152
|
+
<span class="time">${fmt(d.avg)}</span>
|
|
153
|
+
</div>
|
|
154
|
+
<div class="bar-track">
|
|
155
|
+
<div class="bar-fill" style="width: ${barWidth}%; background: var(--pico-primary);"></div>
|
|
156
|
+
</div>
|
|
157
|
+
</div>`;
|
|
158
|
+
})
|
|
159
|
+
.join("");
|
|
160
|
+
}
|
|
161
|
+
modal?.showModal();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { adapterName, shortOp, formatRuntime, fmt } from "./utils";
|
|
2
|
+
import { state, OP_ORDER_SMALL, OP_ORDER_LARGE } from "./var";
|
|
3
|
+
|
|
4
|
+
const SECTIONS: [
|
|
5
|
+
string,
|
|
6
|
+
string[],
|
|
7
|
+
][] = [
|
|
8
|
+
[
|
|
9
|
+
"Small Collection (users, 10k)",
|
|
10
|
+
OP_ORDER_SMALL,
|
|
11
|
+
],
|
|
12
|
+
[
|
|
13
|
+
"Large Collection (posts, 200k)",
|
|
14
|
+
OP_ORDER_LARGE,
|
|
15
|
+
],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
export function renderTables(selectedAdapter: string = null) {
|
|
19
|
+
const container = document.querySelector("#tables-container");
|
|
20
|
+
const select = document.querySelector<HTMLSelectElement>(
|
|
21
|
+
"#tables-adapter-select",
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const adapters = [
|
|
25
|
+
...new Set(state.entries.map(e => e.info.adapter)),
|
|
26
|
+
].sort();
|
|
27
|
+
|
|
28
|
+
if (select.options.length === 0) {
|
|
29
|
+
select.innerHTML = adapters
|
|
30
|
+
.map(a => `<option value="${a}">${adapterName(a)}</option>`)
|
|
31
|
+
.join("");
|
|
32
|
+
select.innerHTML += `<option value="ALL">Show All</option>`;
|
|
33
|
+
|
|
34
|
+
select.addEventListener("change", () => {
|
|
35
|
+
renderTables(select.value);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!selectedAdapter && adapters.length > 0) {
|
|
39
|
+
selectedAdapter = adapters.find(a => a === "dir") || adapters[0];
|
|
40
|
+
select.value = selectedAdapter;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
container.innerHTML = buildTablesHTML(
|
|
45
|
+
selectedAdapter === "ALL"
|
|
46
|
+
? adapters
|
|
47
|
+
: selectedAdapter
|
|
48
|
+
? [
|
|
49
|
+
selectedAdapter,
|
|
50
|
+
]
|
|
51
|
+
: adapters,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function buildTablesHTML(adaptersToRender: string[]): string {
|
|
56
|
+
let html = "";
|
|
57
|
+
|
|
58
|
+
for (const adapter of adaptersToRender) {
|
|
59
|
+
const group = state.entries.filter(e => e.info.adapter === adapter);
|
|
60
|
+
const verOrder = [
|
|
61
|
+
"bun",
|
|
62
|
+
"24",
|
|
63
|
+
"22",
|
|
64
|
+
"20",
|
|
65
|
+
];
|
|
66
|
+
group.sort((a, b) => {
|
|
67
|
+
const ai = verOrder.indexOf(a.info.ver);
|
|
68
|
+
const bi = verOrder.indexOf(b.info.ver);
|
|
69
|
+
if (ai !== bi) return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
|
|
70
|
+
return a.info.os.localeCompare(b.info.os);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const firstEntry = group[0];
|
|
74
|
+
const coreVer = firstEntry?.data.coreVersion;
|
|
75
|
+
const adapterVer = firstEntry?.data.adapterVersion;
|
|
76
|
+
const versionLine = coreVer
|
|
77
|
+
? `<p>core: ${coreVer}${adapterVer ? ` | adapter: ${adapterVer}` : ""}</p>`
|
|
78
|
+
: "";
|
|
79
|
+
html += `<h3>${adapterName(adapter)} Adapter</h3>${versionLine}`;
|
|
80
|
+
|
|
81
|
+
for (const [label, ops] of SECTIONS) {
|
|
82
|
+
const present = ops.filter(op =>
|
|
83
|
+
group.some(e => e.data.results.some(r => r.name === op)),
|
|
84
|
+
);
|
|
85
|
+
if (!present.length) continue;
|
|
86
|
+
|
|
87
|
+
html += `<h4>${label}</h4>`;
|
|
88
|
+
html += `<figure><table class="striped">`;
|
|
89
|
+
html += `<thead><tr><th scope="col">Runtime / OS</th>`;
|
|
90
|
+
|
|
91
|
+
for (const op of present) html += `<th scope="col">${shortOp(op)}</th>`;
|
|
92
|
+
html += `</tr></thead><tbody>`;
|
|
93
|
+
|
|
94
|
+
for (const e of group) {
|
|
95
|
+
html += `<tr><td>${formatRuntime(e.info.ver)} / ${e.info.os}</td>`;
|
|
96
|
+
for (const op of present) {
|
|
97
|
+
const r = e.data.results.find(x => x.name === op);
|
|
98
|
+
html += `<td>${r ? fmt(r.time) : "-"}</td>`;
|
|
99
|
+
}
|
|
100
|
+
html += `</tr>`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
html += `</tbody></table></figure>`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return html;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function buildMarkdown(adaptersToRender: string[]): string {
|
|
111
|
+
const lines: string[] = [];
|
|
112
|
+
lines.push("# ValtheraDB Benchmark Results");
|
|
113
|
+
lines.push("");
|
|
114
|
+
|
|
115
|
+
for (const adapter of adaptersToRender) {
|
|
116
|
+
const group = state.entries.filter(e => e.info.adapter === adapter);
|
|
117
|
+
const verOrder = [
|
|
118
|
+
"bun",
|
|
119
|
+
"24",
|
|
120
|
+
"22",
|
|
121
|
+
"20",
|
|
122
|
+
];
|
|
123
|
+
group.sort((a, b) => {
|
|
124
|
+
const ai = verOrder.indexOf(a.info.ver);
|
|
125
|
+
const bi = verOrder.indexOf(b.info.ver);
|
|
126
|
+
if (ai !== bi) return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
|
|
127
|
+
return a.info.os.localeCompare(b.info.os);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const firstEntry = group[0];
|
|
131
|
+
const coreVer = firstEntry?.data.coreVersion;
|
|
132
|
+
const adapterVer = firstEntry?.data.adapterVersion;
|
|
133
|
+
|
|
134
|
+
lines.push(`## ${adapterName(adapter)} Adapter`);
|
|
135
|
+
if (coreVer) {
|
|
136
|
+
lines.push(
|
|
137
|
+
`core: ${coreVer}${adapterVer ? ` | adapter: ${adapterVer}` : ""}`,
|
|
138
|
+
);
|
|
139
|
+
lines.push("");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const [label, ops] of SECTIONS) {
|
|
143
|
+
const present = ops.filter(op =>
|
|
144
|
+
group.some(e => e.data.results.some(r => r.name === op)),
|
|
145
|
+
);
|
|
146
|
+
if (!present.length) continue;
|
|
147
|
+
|
|
148
|
+
lines.push(`### ${label}`);
|
|
149
|
+
lines.push("");
|
|
150
|
+
|
|
151
|
+
const header = [
|
|
152
|
+
"Runtime / OS",
|
|
153
|
+
...present.map(shortOp),
|
|
154
|
+
];
|
|
155
|
+
lines.push(`| ${header.join(" | ")} |`);
|
|
156
|
+
lines.push(`| ${header.map(() => "---").join(" | ")} |`);
|
|
157
|
+
|
|
158
|
+
for (const e of group) {
|
|
159
|
+
const cells = [
|
|
160
|
+
`${formatRuntime(e.info.ver)} / ${e.info.os}`,
|
|
161
|
+
...present.map(op => {
|
|
162
|
+
const r = e.data.results.find(x => x.name === op);
|
|
163
|
+
return r ? fmt(r.time) : "-";
|
|
164
|
+
}),
|
|
165
|
+
];
|
|
166
|
+
lines.push(`| ${cells.join(" | ")} |`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
lines.push("");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return lines.join("\n");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function downloadMarkdown() {
|
|
177
|
+
const select = document.querySelector<HTMLSelectElement>(
|
|
178
|
+
"#tables-adapter-select",
|
|
179
|
+
);
|
|
180
|
+
const adapters = [
|
|
181
|
+
...new Set(state.entries.map(e => e.info.adapter)),
|
|
182
|
+
].sort();
|
|
183
|
+
const selected = select?.value ?? "ALL";
|
|
184
|
+
const adaptersToRender =
|
|
185
|
+
selected === "ALL"
|
|
186
|
+
? adapters
|
|
187
|
+
: [
|
|
188
|
+
selected,
|
|
189
|
+
];
|
|
190
|
+
|
|
191
|
+
const md = buildMarkdown(adaptersToRender);
|
|
192
|
+
const blob = new Blob(
|
|
193
|
+
[
|
|
194
|
+
md,
|
|
195
|
+
],
|
|
196
|
+
{
|
|
197
|
+
type: "text/markdown",
|
|
198
|
+
},
|
|
199
|
+
);
|
|
200
|
+
const url = URL.createObjectURL(blob);
|
|
201
|
+
const a = document.createElement("a");
|
|
202
|
+
a.href = url;
|
|
203
|
+
a.download = `benchmark-${selected === "ALL" ? "all" : selected}.md`;
|
|
204
|
+
a.click();
|
|
205
|
+
URL.revokeObjectURL(url);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function initTablesDownload() {
|
|
209
|
+
document
|
|
210
|
+
.querySelector<HTMLButtonElement>("#tables-download-md")
|
|
211
|
+
?.addEventListener("click", downloadMarkdown);
|
|
212
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface BenchResult {
|
|
2
|
+
name: string;
|
|
3
|
+
time: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ResultFile {
|
|
7
|
+
os?: string;
|
|
8
|
+
arch?: string;
|
|
9
|
+
node?: string;
|
|
10
|
+
bun?: string;
|
|
11
|
+
adapter?: string;
|
|
12
|
+
coreVersion?: string;
|
|
13
|
+
adapterVersion?: string;
|
|
14
|
+
results: BenchResult[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ParsedEntry {
|
|
18
|
+
adapter: string;
|
|
19
|
+
ver: string;
|
|
20
|
+
os: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Entry {
|
|
24
|
+
dir: string;
|
|
25
|
+
info: ParsedEntry;
|
|
26
|
+
data: ResultFile;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CacheData {
|
|
30
|
+
timestamp: number;
|
|
31
|
+
adapterConfig: Record<string, string>;
|
|
32
|
+
entries: Entry[];
|
|
33
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BenchResult } from "../run";
|
|
2
|
+
import { ParsedEntry } from "./types";
|
|
3
|
+
import { state } from "./var";
|
|
4
|
+
|
|
5
|
+
export function shortOp(name: string): string {
|
|
6
|
+
return name.replace(/-(?:small|large)$/, "");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function threshold(ms: number): number {
|
|
10
|
+
return ms < 10 ? 0.05 : 0.01;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function adapterName(id: string): string {
|
|
14
|
+
return state.adapterConfig[id] || id;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function mean(values: number[]): number {
|
|
18
|
+
return values.reduce((a, b) => a + b, 0) / values.length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function normalize(results: BenchResult[]): BenchResult[] {
|
|
22
|
+
const map = new Map<string, number[]>();
|
|
23
|
+
for (const r of results) {
|
|
24
|
+
const arr = map.get(r.name);
|
|
25
|
+
if (arr) arr.push(r.time);
|
|
26
|
+
else
|
|
27
|
+
map.set(r.name, [
|
|
28
|
+
r.time,
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
const out: BenchResult[] = [];
|
|
32
|
+
for (const [name, times] of map) {
|
|
33
|
+
out.push({
|
|
34
|
+
name,
|
|
35
|
+
time: mean(times),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function parseEntry(dir: string): ParsedEntry {
|
|
42
|
+
const p = dir.split("__");
|
|
43
|
+
return {
|
|
44
|
+
adapter: p[0],
|
|
45
|
+
ver: p[1],
|
|
46
|
+
os: p[2],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function fmt(ms: number): string {
|
|
51
|
+
if (ms >= 1000) return (ms / 1000).toFixed(2) + "s";
|
|
52
|
+
if (ms >= 1) return ms.toFixed(1) + "ms";
|
|
53
|
+
return (ms * 1000).toFixed(2) + "μs";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function formatRuntime(ver: string): string {
|
|
57
|
+
return ver === "bun" ? "bun" : `node ${ver}`;
|
|
58
|
+
}
|
package/src/page/var.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Entry } from "./types";
|
|
2
|
+
|
|
3
|
+
export const OP_ORDER_SMALL = [
|
|
4
|
+
"add-small",
|
|
5
|
+
"findOne-small",
|
|
6
|
+
"find-search-small",
|
|
7
|
+
"find-paginated-small",
|
|
8
|
+
"updateOne-small",
|
|
9
|
+
"update-search-small",
|
|
10
|
+
"removeOne-small",
|
|
11
|
+
"remove-search-small",
|
|
12
|
+
"find-small",
|
|
13
|
+
"update-small",
|
|
14
|
+
"remove-small",
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
export const OP_ORDER_LARGE = [
|
|
18
|
+
"add-large",
|
|
19
|
+
"findOne-large",
|
|
20
|
+
"find-search-large",
|
|
21
|
+
"find-paginated-large",
|
|
22
|
+
"updateOne-large",
|
|
23
|
+
"update-search-large",
|
|
24
|
+
"removeOne-large",
|
|
25
|
+
"remove-search-large",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export const state = {
|
|
29
|
+
entries: [] as Entry[],
|
|
30
|
+
adapterConfig: {} as Record<string, string>,
|
|
31
|
+
};
|
package/src/run.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { performance } from "perf_hooks";
|
|
2
|
+
|
|
3
|
+
export interface BenchResult {
|
|
4
|
+
name: string;
|
|
5
|
+
time: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function bench(
|
|
9
|
+
name: string,
|
|
10
|
+
fn: () => Promise<any>,
|
|
11
|
+
ops: number = 1,
|
|
12
|
+
): Promise<BenchResult> {
|
|
13
|
+
console.log(` ${name}...`);
|
|
14
|
+
const start = performance.now();
|
|
15
|
+
await fn();
|
|
16
|
+
const end = performance.now();
|
|
17
|
+
const time = (end - start) / ops;
|
|
18
|
+
console.log(
|
|
19
|
+
` ${time < 1 ? (time * 1000).toFixed(2) + "μs" : time < 1000 ? time.toFixed(2) + "ms" : (time / 1000).toFixed(2) + "s"}`,
|
|
20
|
+
);
|
|
21
|
+
return {
|
|
22
|
+
name,
|
|
23
|
+
time,
|
|
24
|
+
};
|
|
25
|
+
}
|