-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.ts
More file actions
110 lines (99 loc) · 3.87 KB
/
Copy pathbenchmark.ts
File metadata and controls
110 lines (99 loc) · 3.87 KB
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
import { LuffyDBClient } from './client';
async function stressBenchmark() {
const client = new LuffyDBClient(undefined, false);
const tableName = 'stress_test';
const totalRows = 20; // 10K rows for insertion
const concurrentOps = 50; // Number of concurrent operations
console.log("Starting stress benchmark...");
console.time('Stress Benchmark Total');
// Define the table with extended columns
try {
console.time('Define Table');
await client.defineTable(tableName, ['name', 'age', 'email', 'bio', 'status', 'score']);
console.timeEnd('Define Table');
} catch (error) {
console.error("Error during table definition:", error);
}
// Insert 10K rows concurrently
let rowIds: string[] = [];
try {
console.time('Insert 10K Rows');
const insertPromises = Array.from({ length: totalRows }, (_, i) =>
client.insert(tableName, {
name: `User${i}`,
age: (18 + (i % 60)).toString(),
email: `user${i}@example.com`,
bio: `Bio for user ${i} with some lengthy text to stress the system`,
status: i % 2 === 0 ? 'active' : 'inactive',
score: (Math.random() * 100).toFixed(2),
})
);
rowIds = await Promise.all(insertPromises);
console.timeEnd('Insert 10K Rows');
console.log(`Inserted ${rowIds.length} rows.`);
} catch (error) {
console.error("Error inserting rows:", error);
}
// Query all rows after insertion
try {
console.time('Query All Rows');
const allRows = await client.query(tableName);
console.log(`Total rows after insertion: ${allRows.length}`);
console.timeEnd('Query All Rows');
} catch (error) {
console.error("Error querying all rows:", error);
}
// Execute concurrent LIKE queries with LIMIT
try {
console.time('Concurrent LIKE Queries');
const likeQueries = Array.from({ length: concurrentOps }, (_, i) =>
client.query(tableName, {}, 100, { name: `User${i * 100}` })
);
const likeResults = await Promise.all(likeQueries);
const totalLikeResults = likeResults.reduce((sum, arr) => sum + arr.length, 0);
const averageLikeResults = totalLikeResults / concurrentOps;
console.log(`Average LIKE query returned ${averageLikeResults} rows.`);
console.timeEnd('Concurrent LIKE Queries');
} catch (error) {
console.error("Error during concurrent LIKE queries:", error);
}
// Perform concurrent updates on the first set of inserted rows
try {
console.time('Concurrent Updates');
const updatePromises = Array.from({ length: concurrentOps }, (_, i) =>
client.update(tableName, rowIds[i], {
score: (Math.random() * 100).toFixed(2),
status: 'updated',
})
);
await Promise.all(updatePromises);
console.timeEnd('Concurrent Updates');
console.log(`Performed ${concurrentOps} concurrent updates.`);
} catch (error) {
console.error("Error during concurrent updates:", error);
}
// Perform concurrent deletes on a different set of rows
try {
console.time('Concurrent Deletes');
const deletePromises = Array.from({ length: concurrentOps }, (_, i) =>
client.delete(tableName, rowIds[i + concurrentOps])
);
await Promise.all(deletePromises);
console.timeEnd('Concurrent Deletes');
console.log(`Performed ${concurrentOps} concurrent deletes.`);
} catch (error) {
console.error("Error during concurrent deletes:", error);
}
// Final query to assess state after modifications
try {
console.time('Query After Mods');
const remainingRows = await client.query(tableName, { status: 'active' }, 1000);
console.log(`Active rows after modifications: ${remainingRows.length}`);
console.timeEnd('Query After Mods');
} catch (error) {
console.error("Error querying rows after modifications:", error);
}
console.timeEnd('Stress Benchmark Total');
console.log("Stress benchmark completed.");
}
stressBenchmark().catch(console.error);