-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.js
144 lines (130 loc) · 3.37 KB
/
benchmark.js
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
import { performance } from "node:perf_hooks";
const QUERY_COUNT = 100000; // Number of queries to benchmark
const KEY_LENGTH = 10; // Length of the key
const VALUE_LENGTH = 50; // Length of the value
function randomString(length) {
return Array.from({ length }, () =>
String.fromCharCode(65 + Math.floor(Math.random() * 26))
).join("");
}
function formatTime(ms) {
return (ms / 1000).toFixed(2);
}
async function runBenchmark(db, operation, action, data) {
console.log(`\n* Running Benchmark: ${operation}`);
const start = performance.now();
await action(db, data);
const duration = performance.now() - start;
const averageLatency = duration / QUERY_COUNT;
const operationsPerSecond = Math.floor(QUERY_COUNT / (duration / 1000));
console.log(`* Benchmark Complete: ${operation}`);
return {
Operation: operation,
"Total Time (s)": formatTime(duration),
"Avg Latency (ms)": averageLatency.toFixed(4),
"Ops/Second": operationsPerSecond.toLocaleString(),
};
}
export default async function benchmark(db, runtime, type) {
console.log(`Starting benchmark with ${QUERY_COUNT} queries...`);
console.log("Wait for all benchmarks to complete to print the table.");
const keys = Array.from({ length: QUERY_COUNT }, () =>
randomString(KEY_LENGTH)
);
const values = Array.from({ length: QUERY_COUNT }, () =>
randomString(VALUE_LENGTH)
);
const keyValuePairs = keys.map((key, index) => ({
key,
value: values[index],
}));
const benchmarks = [
{
name: "Set",
action: async (db, data) => {
for (const { key, value } of data) {
await db.set(key, value);
}
},
data: keyValuePairs,
},
{
name: "Multi Set",
action: async (db, data) => await db.multiSet(data),
data: keyValuePairs,
},
{
name: "Exists",
action: async (db, data) => {
for (const key of data) {
await db.exists(key);
}
},
data: keys,
},
{
name: "Get Expire",
action: async (db, data) => {
for (const key of data) {
await db.getExpire(key);
}
},
data: keys,
},
{
name: "Set Expire",
action: async (db, data) => {
const futureDate = new Date("2025-01-12");
for (const key of data) {
await db.setExpire(key, futureDate);
}
},
data: keys,
},
{
name: "Get",
action: async (db, data) => {
for (const key of data) {
await db.get(key);
}
},
data: keys,
},
{
name: "Multi Get",
action: async (db, data) => await db.multiGet(data),
data: keys,
},
{
name: "Delete",
action: async (db, data) => {
for (const key of data) {
await db.delete(key);
}
},
data: keys,
},
{
name: "Multi Delete",
action: async (db, data) => await db.multiDelete(data),
data: keys,
},
];
const results = [];
for (const benchmark of benchmarks) {
const result = await runBenchmark(
db,
benchmark.name,
benchmark.action,
benchmark.data
);
results.push(result);
}
console.log(
`\n${QUERY_COUNT} Queries Benchmark Summary ( ${runtime} ), ( ${type} ):`
);
console.table(results);
await db.cleanup();
await db.close();
console.log("\nBenchmark completed.");
}