@wxn0brp/db-benchmark 0.0.1 → 0.20261124.1829
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/README.md +18 -0
- package/dist/custom/dir-index.js +6 -0
- package/dist/custom/sqlite.js +36 -0
- package/dist/db.js +9 -0
- package/dist/generate.js +34 -0
- package/dist/index.js +22 -0
- package/dist/large.js +55 -0
- package/dist/local/args.js +51 -0
- package/dist/local/cloud.js +94 -0
- package/dist/local/compare.js +79 -0
- package/dist/local/display.js +64 -0
- package/dist/local/index.js +87 -0
- package/dist/page/cache.js +30 -0
- package/dist/page/compare.js +315 -0
- package/dist/page/index.js +93 -0
- package/dist/page/local.js +157 -0
- package/dist/page/summary.js +123 -0
- package/dist/page/tables.js +169 -0
- package/dist/page/types.js +1 -0
- package/dist/page/utils.js +51 -0
- package/dist/page/var.js +27 -0
- package/dist/run.js +13 -0
- package/dist/small.js +45 -0
- package/dist/utils.js +52 -0
- package/package.json +1 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { adapterName, shortOp, formatRuntime, fmt } from "./utils.js";
|
|
2
|
+
import { state, OP_ORDER_SMALL, OP_ORDER_LARGE } from "./var.js";
|
|
3
|
+
const SECTIONS = [
|
|
4
|
+
[
|
|
5
|
+
"Small Collection (users, 10k)",
|
|
6
|
+
OP_ORDER_SMALL,
|
|
7
|
+
],
|
|
8
|
+
[
|
|
9
|
+
"Large Collection (posts, 200k)",
|
|
10
|
+
OP_ORDER_LARGE,
|
|
11
|
+
],
|
|
12
|
+
];
|
|
13
|
+
export function renderTables(selectedAdapter = null) {
|
|
14
|
+
const container = document.querySelector("#tables-container");
|
|
15
|
+
const select = document.querySelector("#tables-adapter-select");
|
|
16
|
+
const adapters = [
|
|
17
|
+
...new Set(state.entries.map(e => e.info.adapter)),
|
|
18
|
+
].sort();
|
|
19
|
+
if (select.options.length === 0) {
|
|
20
|
+
select.innerHTML = adapters
|
|
21
|
+
.map(a => `<option value="${a}">${adapterName(a)}</option>`)
|
|
22
|
+
.join("");
|
|
23
|
+
select.innerHTML += `<option value="ALL">Show All</option>`;
|
|
24
|
+
select.addEventListener("change", () => {
|
|
25
|
+
renderTables(select.value);
|
|
26
|
+
});
|
|
27
|
+
if (!selectedAdapter && adapters.length > 0) {
|
|
28
|
+
selectedAdapter = adapters.find(a => a === "dir") || adapters[0];
|
|
29
|
+
select.value = selectedAdapter;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
container.innerHTML = buildTablesHTML(selectedAdapter === "ALL"
|
|
33
|
+
? adapters
|
|
34
|
+
: selectedAdapter
|
|
35
|
+
? [
|
|
36
|
+
selectedAdapter,
|
|
37
|
+
]
|
|
38
|
+
: adapters);
|
|
39
|
+
}
|
|
40
|
+
function buildTablesHTML(adaptersToRender) {
|
|
41
|
+
let html = "";
|
|
42
|
+
for (const adapter of adaptersToRender) {
|
|
43
|
+
const group = state.entries.filter(e => e.info.adapter === adapter);
|
|
44
|
+
const verOrder = [
|
|
45
|
+
"bun",
|
|
46
|
+
"24",
|
|
47
|
+
"22",
|
|
48
|
+
"20",
|
|
49
|
+
];
|
|
50
|
+
group.sort((a, b) => {
|
|
51
|
+
const ai = verOrder.indexOf(a.info.ver);
|
|
52
|
+
const bi = verOrder.indexOf(b.info.ver);
|
|
53
|
+
if (ai !== bi)
|
|
54
|
+
return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
|
|
55
|
+
return a.info.os.localeCompare(b.info.os);
|
|
56
|
+
});
|
|
57
|
+
const firstEntry = group[0];
|
|
58
|
+
const coreVer = firstEntry?.data.coreVersion;
|
|
59
|
+
const adapterVer = firstEntry?.data.adapterVersion;
|
|
60
|
+
const versionLine = coreVer
|
|
61
|
+
? `<p>core: ${coreVer}${adapterVer ? ` | adapter: ${adapterVer}` : ""}</p>`
|
|
62
|
+
: "";
|
|
63
|
+
html += `<h3>${adapterName(adapter)} Adapter</h3>${versionLine}`;
|
|
64
|
+
for (const [label, ops] of SECTIONS) {
|
|
65
|
+
const present = ops.filter(op => group.some(e => e.data.results.some(r => r.name === op)));
|
|
66
|
+
if (!present.length)
|
|
67
|
+
continue;
|
|
68
|
+
html += `<h4>${label}</h4>`;
|
|
69
|
+
html += `<figure><table class="striped">`;
|
|
70
|
+
html += `<thead><tr><th scope="col">Runtime / OS</th>`;
|
|
71
|
+
for (const op of present)
|
|
72
|
+
html += `<th scope="col">${shortOp(op)}</th>`;
|
|
73
|
+
html += `</tr></thead><tbody>`;
|
|
74
|
+
for (const e of group) {
|
|
75
|
+
html += `<tr><td>${formatRuntime(e.info.ver)} / ${e.info.os}</td>`;
|
|
76
|
+
for (const op of present) {
|
|
77
|
+
const r = e.data.results.find(x => x.name === op);
|
|
78
|
+
html += `<td>${r ? fmt(r.time) : "-"}</td>`;
|
|
79
|
+
}
|
|
80
|
+
html += `</tr>`;
|
|
81
|
+
}
|
|
82
|
+
html += `</tbody></table></figure>`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return html;
|
|
86
|
+
}
|
|
87
|
+
function buildMarkdown(adaptersToRender) {
|
|
88
|
+
const lines = [];
|
|
89
|
+
lines.push("# ValtheraDB Benchmark Results");
|
|
90
|
+
lines.push("");
|
|
91
|
+
for (const adapter of adaptersToRender) {
|
|
92
|
+
const group = state.entries.filter(e => e.info.adapter === adapter);
|
|
93
|
+
const verOrder = [
|
|
94
|
+
"bun",
|
|
95
|
+
"24",
|
|
96
|
+
"22",
|
|
97
|
+
"20",
|
|
98
|
+
];
|
|
99
|
+
group.sort((a, b) => {
|
|
100
|
+
const ai = verOrder.indexOf(a.info.ver);
|
|
101
|
+
const bi = verOrder.indexOf(b.info.ver);
|
|
102
|
+
if (ai !== bi)
|
|
103
|
+
return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
|
|
104
|
+
return a.info.os.localeCompare(b.info.os);
|
|
105
|
+
});
|
|
106
|
+
const firstEntry = group[0];
|
|
107
|
+
const coreVer = firstEntry?.data.coreVersion;
|
|
108
|
+
const adapterVer = firstEntry?.data.adapterVersion;
|
|
109
|
+
lines.push(`## ${adapterName(adapter)} Adapter`);
|
|
110
|
+
if (coreVer) {
|
|
111
|
+
lines.push(`core: ${coreVer}${adapterVer ? ` | adapter: ${adapterVer}` : ""}`);
|
|
112
|
+
lines.push("");
|
|
113
|
+
}
|
|
114
|
+
for (const [label, ops] of SECTIONS) {
|
|
115
|
+
const present = ops.filter(op => group.some(e => e.data.results.some(r => r.name === op)));
|
|
116
|
+
if (!present.length)
|
|
117
|
+
continue;
|
|
118
|
+
lines.push(`### ${label}`);
|
|
119
|
+
lines.push("");
|
|
120
|
+
const header = [
|
|
121
|
+
"Runtime / OS",
|
|
122
|
+
...present.map(shortOp),
|
|
123
|
+
];
|
|
124
|
+
lines.push(`| ${header.join(" | ")} |`);
|
|
125
|
+
lines.push(`| ${header.map(() => "---").join(" | ")} |`);
|
|
126
|
+
for (const e of group) {
|
|
127
|
+
const cells = [
|
|
128
|
+
`${formatRuntime(e.info.ver)} / ${e.info.os}`,
|
|
129
|
+
...present.map(op => {
|
|
130
|
+
const r = e.data.results.find(x => x.name === op);
|
|
131
|
+
return r ? fmt(r.time) : "-";
|
|
132
|
+
}),
|
|
133
|
+
];
|
|
134
|
+
lines.push(`| ${cells.join(" | ")} |`);
|
|
135
|
+
}
|
|
136
|
+
lines.push("");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return lines.join("\n");
|
|
140
|
+
}
|
|
141
|
+
function downloadMarkdown() {
|
|
142
|
+
const select = document.querySelector("#tables-adapter-select");
|
|
143
|
+
const adapters = [
|
|
144
|
+
...new Set(state.entries.map(e => e.info.adapter)),
|
|
145
|
+
].sort();
|
|
146
|
+
const selected = select?.value ?? "ALL";
|
|
147
|
+
const adaptersToRender = selected === "ALL"
|
|
148
|
+
? adapters
|
|
149
|
+
: [
|
|
150
|
+
selected,
|
|
151
|
+
];
|
|
152
|
+
const md = buildMarkdown(adaptersToRender);
|
|
153
|
+
const blob = new Blob([
|
|
154
|
+
md,
|
|
155
|
+
], {
|
|
156
|
+
type: "text/markdown",
|
|
157
|
+
});
|
|
158
|
+
const url = URL.createObjectURL(blob);
|
|
159
|
+
const a = document.createElement("a");
|
|
160
|
+
a.href = url;
|
|
161
|
+
a.download = `benchmark-${selected === "ALL" ? "all" : selected}.md`;
|
|
162
|
+
a.click();
|
|
163
|
+
URL.revokeObjectURL(url);
|
|
164
|
+
}
|
|
165
|
+
export function initTablesDownload() {
|
|
166
|
+
document
|
|
167
|
+
.querySelector("#tables-download-md")
|
|
168
|
+
?.addEventListener("click", downloadMarkdown);
|
|
169
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { state } from "./var.js";
|
|
2
|
+
export function shortOp(name) {
|
|
3
|
+
return name.replace(/-(?:small|large)$/, "");
|
|
4
|
+
}
|
|
5
|
+
export function threshold(ms) {
|
|
6
|
+
return ms < 10 ? 0.05 : 0.01;
|
|
7
|
+
}
|
|
8
|
+
export function adapterName(id) {
|
|
9
|
+
return state.adapterConfig[id] || id;
|
|
10
|
+
}
|
|
11
|
+
export function mean(values) {
|
|
12
|
+
return values.reduce((a, b) => a + b, 0) / values.length;
|
|
13
|
+
}
|
|
14
|
+
export function normalize(results) {
|
|
15
|
+
const map = new Map();
|
|
16
|
+
for (const r of results) {
|
|
17
|
+
const arr = map.get(r.name);
|
|
18
|
+
if (arr)
|
|
19
|
+
arr.push(r.time);
|
|
20
|
+
else
|
|
21
|
+
map.set(r.name, [
|
|
22
|
+
r.time,
|
|
23
|
+
]);
|
|
24
|
+
}
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const [name, times] of map) {
|
|
27
|
+
out.push({
|
|
28
|
+
name,
|
|
29
|
+
time: mean(times),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
export function parseEntry(dir) {
|
|
35
|
+
const p = dir.split("__");
|
|
36
|
+
return {
|
|
37
|
+
adapter: p[0],
|
|
38
|
+
ver: p[1],
|
|
39
|
+
os: p[2],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function fmt(ms) {
|
|
43
|
+
if (ms >= 1000)
|
|
44
|
+
return (ms / 1000).toFixed(2) + "s";
|
|
45
|
+
if (ms >= 1)
|
|
46
|
+
return ms.toFixed(1) + "ms";
|
|
47
|
+
return (ms * 1000).toFixed(2) + "μs";
|
|
48
|
+
}
|
|
49
|
+
export function formatRuntime(ver) {
|
|
50
|
+
return ver === "bun" ? "bun" : `node ${ver}`;
|
|
51
|
+
}
|
package/dist/page/var.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const OP_ORDER_SMALL = [
|
|
2
|
+
"add-small",
|
|
3
|
+
"findOne-small",
|
|
4
|
+
"find-search-small",
|
|
5
|
+
"find-paginated-small",
|
|
6
|
+
"updateOne-small",
|
|
7
|
+
"update-search-small",
|
|
8
|
+
"removeOne-small",
|
|
9
|
+
"remove-search-small",
|
|
10
|
+
"find-small",
|
|
11
|
+
"update-small",
|
|
12
|
+
"remove-small",
|
|
13
|
+
];
|
|
14
|
+
export const OP_ORDER_LARGE = [
|
|
15
|
+
"add-large",
|
|
16
|
+
"findOne-large",
|
|
17
|
+
"find-search-large",
|
|
18
|
+
"find-paginated-large",
|
|
19
|
+
"updateOne-large",
|
|
20
|
+
"update-search-large",
|
|
21
|
+
"removeOne-large",
|
|
22
|
+
"remove-search-large",
|
|
23
|
+
];
|
|
24
|
+
export const state = {
|
|
25
|
+
entries: [],
|
|
26
|
+
adapterConfig: {},
|
|
27
|
+
};
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { performance } from "perf_hooks";
|
|
2
|
+
export async function bench(name, fn, ops = 1) {
|
|
3
|
+
console.log(` ${name}...`);
|
|
4
|
+
const start = performance.now();
|
|
5
|
+
await fn();
|
|
6
|
+
const end = performance.now();
|
|
7
|
+
const time = (end - start) / ops;
|
|
8
|
+
console.log(` ${time < 1 ? (time * 1000).toFixed(2) + "μs" : time < 1000 ? time.toFixed(2) + "ms" : (time / 1000).toFixed(2) + "s"}`);
|
|
9
|
+
return {
|
|
10
|
+
name,
|
|
11
|
+
time,
|
|
12
|
+
};
|
|
13
|
+
}
|
package/dist/small.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { generateUsers } from "./generate.js";
|
|
2
|
+
import { bench } from "./run.js";
|
|
3
|
+
const COUNT = 10_000;
|
|
4
|
+
export async function benchmarkSmall(col) {
|
|
5
|
+
const results = [];
|
|
6
|
+
const userData = generateUsers(COUNT);
|
|
7
|
+
const ids = [];
|
|
8
|
+
results.push(await bench("add-small", async () => {
|
|
9
|
+
for (const u of userData) {
|
|
10
|
+
const doc = await col.add(u);
|
|
11
|
+
ids.push(doc._id);
|
|
12
|
+
}
|
|
13
|
+
}, COUNT));
|
|
14
|
+
const firstId = ids[0];
|
|
15
|
+
const midId = ids[Math.floor(ids.length / 2)];
|
|
16
|
+
const lastId = ids[ids.length - 1];
|
|
17
|
+
results.push(await bench("findOne-small", () => col.findOne({
|
|
18
|
+
_id: firstId,
|
|
19
|
+
})));
|
|
20
|
+
results.push(await bench("find-search-small", () => col.find({
|
|
21
|
+
isActive: true,
|
|
22
|
+
})));
|
|
23
|
+
results.push(await bench("find-paginated-small", () => col.find({}, {
|
|
24
|
+
limit: 100,
|
|
25
|
+
})));
|
|
26
|
+
results.push(await bench("updateOne-small", () => col.updateOne({
|
|
27
|
+
_id: midId,
|
|
28
|
+
}, {
|
|
29
|
+
age: 99,
|
|
30
|
+
})));
|
|
31
|
+
results.push(await bench("update-search-small", () => col.update({
|
|
32
|
+
isActive: false,
|
|
33
|
+
}, {
|
|
34
|
+
isActive: true,
|
|
35
|
+
})));
|
|
36
|
+
results.push(await bench("removeOne-small", () => col.removeOne({
|
|
37
|
+
_id: lastId,
|
|
38
|
+
})));
|
|
39
|
+
results.push(await bench("remove-search-small", () => col.remove({
|
|
40
|
+
$lt: {
|
|
41
|
+
age: 25,
|
|
42
|
+
},
|
|
43
|
+
})));
|
|
44
|
+
return results;
|
|
45
|
+
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
process.on("uncaughtException", err => {
|
|
4
|
+
console.error("Uncaught exception:", err);
|
|
5
|
+
process.exit(1);
|
|
6
|
+
});
|
|
7
|
+
process.on("unhandledRejection", reason => {
|
|
8
|
+
console.error("Unhandled rejection:", reason);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
});
|
|
11
|
+
export async function loadCustomSetup(adapterName, adapter) {
|
|
12
|
+
const setupPath = resolve(import.meta.dirname, "custom", `${adapterName}.js`);
|
|
13
|
+
let onAfterAdd = async () => { };
|
|
14
|
+
if (existsSync(setupPath)) {
|
|
15
|
+
console.log(`-> Using custom setup for ${adapterName}`);
|
|
16
|
+
const mod = await import(setupPath);
|
|
17
|
+
if (typeof mod.init === "function") {
|
|
18
|
+
await mod.init(adapter);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
console.log(`-> No init function found for ${adapterName}`);
|
|
22
|
+
}
|
|
23
|
+
if (typeof mod.onAfterAdd === "function") {
|
|
24
|
+
onAfterAdd = () => mod.onAfterAdd(adapter);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
console.log(`-> No custom setup found for ${adapterName}`);
|
|
29
|
+
}
|
|
30
|
+
return onAfterAdd;
|
|
31
|
+
}
|
|
32
|
+
export function writeResults(file, params) {
|
|
33
|
+
const results = {
|
|
34
|
+
time: new Date().toISOString(),
|
|
35
|
+
os: process.platform,
|
|
36
|
+
arch: process.arch,
|
|
37
|
+
node: process?.version,
|
|
38
|
+
bun: globalThis.Bun?.version || "N/A",
|
|
39
|
+
adapter: params.adapter,
|
|
40
|
+
coreVersion: params.coreVersion,
|
|
41
|
+
adapterVersion: params.adapterVersion,
|
|
42
|
+
results: params.results,
|
|
43
|
+
};
|
|
44
|
+
if (existsSync(file)) {
|
|
45
|
+
const oldResults = JSON.parse(readFileSync(file, "utf-8"));
|
|
46
|
+
results.results = [
|
|
47
|
+
...oldResults.results,
|
|
48
|
+
...results.results,
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
writeFileSync(file, JSON.stringify(results, null, 2));
|
|
52
|
+
}
|