|
1 | | -import { Brine } from "../../dist"; |
2 | | -import Josh from "@joshdb/core"; |
3 | | -// @ts-expect-error 7016 |
4 | | -import JoshSqlite from "@joshdb/sqlite"; |
| 1 | +import { Brine } from "../../"; |
5 | 2 | import { Spinner } from "@favware/colorette-spinner"; |
6 | 3 | import { BrineDatabases } from "../../src"; |
| 4 | +import { Bench } from "tinybench"; |
7 | 5 |
|
8 | | -const runs = 100; |
9 | | -const databaseSize = 5; |
| 6 | +const randomData = (length: number) => { |
| 7 | + const chars = |
| 8 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 9 | + let result = ""; |
10 | 10 |
|
11 | | -console.log( |
12 | | - `\nBrine Benchmark commencing, runs set at ${runs} and ${databaseSize} database size.\n`, |
13 | | -); |
14 | | - |
15 | | -interface kv { |
16 | | - set: (key: string, value: string) => Promise<void>; |
17 | | - get: (key: string) => Promise<void>; |
18 | | - clear: () => Promise<void>; |
19 | | -} |
20 | | - |
21 | | -const test = async (store: kv) => { |
22 | | - const start = performance.now(); |
| 11 | + for (let i = 0; i < length; i++) { |
| 12 | + result += chars.charAt(Math.floor(Math.random() * chars.length)); |
| 13 | + } |
23 | 14 |
|
24 | | - const randomKey = Math.random().toString(36).substring(7); |
25 | | - const randomValue = Math.random().toString(36).substring(7); |
| 15 | + return result; |
| 16 | +}; |
26 | 17 |
|
27 | | - await store.set(randomKey, randomValue); |
28 | | - console.log(await store.get(randomKey)); |
| 18 | +const benchme = async (name: string, db: Brine) => { |
| 19 | + const spinner = new Spinner("Initializing database"); |
| 20 | + const bench = new Bench({ time: 1000, warmupTime: 500 }); |
29 | 21 |
|
30 | | - const end = performance.now(); |
| 22 | + spinner.start(); |
31 | 23 |
|
32 | | - return end - start; |
33 | | -}; |
| 24 | + await db.init(); |
34 | 25 |
|
35 | | -const spinner = new Spinner("Benchmark"); |
36 | | -const benchmark = async (name: string, runner: kv) => { |
37 | | - spinner.start({ |
38 | | - text: `Benchmarking (${name}): 0/${runs}`, |
| 26 | + spinner.update({ |
| 27 | + text: "Setting up database", |
39 | 28 | }); |
40 | 29 |
|
41 | | - let total = 0; |
42 | | - let lastUpdate = Date.now(); |
| 30 | + await db.clear(); |
43 | 31 |
|
44 | | - await runner.clear(); |
| 32 | + const setInitialManyData: [string, string][] = []; |
| 33 | + |
| 34 | + for (let i = 0; i < 1_000_000; i++) { |
| 35 | + setInitialManyData.push([`key-${i}`, randomData(100)]); |
45 | 36 |
|
46 | | - for (let i = 0; i < runs; i++) { |
47 | | - total += await test(runner); |
48 | 37 | spinner.update({ |
49 | | - text: `Benchmarking (${name}): ${i + 1}/${runs}`, |
| 38 | + text: `Setting up database (${i + 1}/1000000) (${( |
| 39 | + (i / 1_000_000) * |
| 40 | + 100 |
| 41 | + ).toFixed(2)}%)`, |
50 | 42 | }); |
51 | | - |
52 | | - if (lastUpdate + 50 < Date.now()) { |
53 | | - lastUpdate = Date.now(); |
54 | | - spinner.spin(); |
55 | | - } |
56 | 43 | } |
57 | 44 |
|
58 | | - return { |
59 | | - [name]: { |
60 | | - "Average (ms)": (total / runs).toFixed(3), |
61 | | - "Operations (op/s)": Math.round(runs / (total / 1000)).toLocaleString(), |
62 | | - "Total (s)": (total / 1000).toFixed(2), |
63 | | - }, |
64 | | - }; |
65 | | -}; |
66 | | - |
67 | | -(async () => { |
68 | | - const josh = new Josh({ |
69 | | - name: "josh", |
70 | | - provider: JoshSqlite, |
| 45 | + spinner.update({ |
| 46 | + text: "Setting up database", |
71 | 47 | }); |
72 | 48 |
|
73 | | - const joshResults = await benchmark("Josh (SQLite)", { |
74 | | - set: async (key, value) => { |
75 | | - await josh.set(key, value); |
76 | | - }, |
77 | | - get: async (key) => { |
78 | | - await josh.get(key); |
79 | | - }, |
80 | | - clear: async () => { |
81 | | - await josh.delete(josh.all); |
82 | | - }, |
83 | | - }); |
| 49 | + await db.setMany(setInitialManyData); |
84 | 50 |
|
85 | | - const brine_postgres = new Brine<string>( |
86 | | - BrineDatabases.postgres.build({ |
87 | | - host: "localhost", |
88 | | - port: 5432, |
89 | | - user: "root", |
90 | | - password: "root", |
91 | | - database: "brine", |
92 | | - }), |
| 51 | + const setManyData: [string, string][] = Array.from( |
| 52 | + { length: 100 }, |
| 53 | + (_, i) => [`key-many-${i}`, randomData(100)], |
93 | 54 | ); |
94 | 55 |
|
95 | | - await brine_postgres.init(); |
96 | | - |
97 | | - const brineResults = await benchmark("Brine (Postgres)", { |
98 | | - set: async (key, value) => { |
99 | | - await brine_postgres.set(key, value); |
100 | | - }, |
101 | | - get: async (key) => { |
102 | | - await brine_postgres.get(key); |
103 | | - }, |
104 | | - clear: async () => { |
105 | | - await brine_postgres.clear(); |
106 | | - }, |
| 56 | + bench |
| 57 | + .add("get", async () => { |
| 58 | + await db.get(`key-${Math.floor(Math.random() * 1000)}`); |
| 59 | + }) |
| 60 | + .add("set", async () => { |
| 61 | + await db.set(`key-${Math.floor(Math.random() * 1000)}`, randomData(100)); |
| 62 | + }) |
| 63 | + .add("count", async () => { |
| 64 | + await db.count(); |
| 65 | + }) |
| 66 | + .add("setMany", async () => { |
| 67 | + await db.setMany(setManyData); |
| 68 | + }); |
| 69 | + |
| 70 | + spinner.update({ |
| 71 | + text: "Running warmup", |
107 | 72 | }); |
108 | 73 |
|
109 | | - const brine_sqlite = new Brine<string>( |
110 | | - BrineDatabases.sqlite.file("./data/brine.sqlite"), |
111 | | - ); |
| 74 | + await bench.warmup(); |
112 | 75 |
|
113 | | - await brine_sqlite.init(); |
114 | | - |
115 | | - const brineResultsSqlite = await benchmark("Brine (SQLite)", { |
116 | | - set: async (key, value) => { |
117 | | - await brine_sqlite.set(key, value); |
118 | | - }, |
119 | | - get: async (key) => { |
120 | | - await brine_sqlite.get(key); |
121 | | - }, |
122 | | - clear: async () => { |
123 | | - await brine_sqlite.clear(); |
124 | | - }, |
| 76 | + spinner.update({ |
| 77 | + text: "Running benchmarks", |
125 | 78 | }); |
126 | 79 |
|
127 | | - const brine_memory = new Brine<string>(BrineDatabases.sqlite.memory); |
| 80 | + await bench.run(); |
| 81 | + await db.close(); |
128 | 82 |
|
129 | | - await brine_memory.init(); |
| 83 | + spinner.stop(); |
130 | 84 |
|
131 | | - const brineResultsMemory = await benchmark("Brine (Memory)", { |
132 | | - set: async (key, value) => { |
133 | | - await brine_memory.set(key, value); |
134 | | - }, |
135 | | - get: async (key) => { |
136 | | - await brine_memory.get(key); |
137 | | - }, |
138 | | - clear: async () => { |
139 | | - await brine_memory.clear(); |
140 | | - }, |
141 | | - }); |
| 85 | + // clear line |
| 86 | + process.stdout.moveCursor(0, -1); |
| 87 | + process.stdout.clearLine(1); |
142 | 88 |
|
143 | | - spinner.success({ text: "Benchmarking complete!" }); |
144 | | - console.table({ |
145 | | - ...joshResults, |
146 | | - ...brineResults, |
147 | | - ...brineResultsSqlite, |
148 | | - ...brineResultsMemory, |
149 | | - }); |
150 | | -})(); |
| 89 | + const table = bench.table(); |
| 90 | + const finalTable: Record<string, string | number>[] = []; |
| 91 | + |
| 92 | + console.log(`😃 Results for: ${name}\n`); // Add Average Time (ms) column based on "Average Time (ns)" column |
| 93 | + |
| 94 | + for (const row of table) { |
| 95 | + if (!row) continue; |
| 96 | + if (typeof row["Average Time (ns)"] !== "number") continue; |
| 97 | + |
| 98 | + finalTable.push({ |
| 99 | + ...row, |
| 100 | + "Average Time (ms)": (row["Average Time (ns)"] / 1000000).toFixed(3), |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + console.table(finalTable); |
| 105 | +}; |
| 106 | + |
| 107 | +(async () => { |
| 108 | + const login = { |
| 109 | + user: "root", |
| 110 | + password: "root", |
| 111 | + database: "brinedb", |
| 112 | + }; |
| 113 | + |
| 114 | + const sqlite_memory = new Brine(BrineDatabases.sqlite.memory); |
| 115 | + const sqlite_file = new Brine(BrineDatabases.sqlite.file("benchmark.sqlite")); |
| 116 | + const postgres = new Brine(BrineDatabases.postgres.build(login)); |
| 117 | + const mysql = new Brine(BrineDatabases.mysql.build(login)); |
| 118 | + const mariadb = new Brine( |
| 119 | + BrineDatabases.mysql.build({ |
| 120 | + ...login, |
| 121 | + port: 3307, |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + await benchme("SQLite (Memory)", sqlite_memory); |
| 126 | + await benchme("SQLite (File)", sqlite_file); |
| 127 | + await benchme("PostgreSQL", postgres); |
| 128 | + await benchme("MySQL", mysql); |
| 129 | + await benchme("MariaDB", mariadb); |
| 130 | + |
| 131 | + console.log("✅ All benchmarks complete"); |
| 132 | +})().catch(console.error); |
0 commit comments