-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateEvents.js
47 lines (42 loc) · 1.23 KB
/
generateEvents.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
import { getDb, countMessagesInQueue } from "./db.js";
function generateRandomMessage() {
const topics = ["topic1", "topic2"];
const randomTopic = topics[Math.floor(Math.random() * topics.length)];
const header = {
topic: `${randomTopic}`,
};
const body = { data: Math.random().toString(36).substring(7) };
return { header, body };
}
async function pushMessagesToQueue() {
try {
const db = await getDb();
for (let i = 0; i < 100; i++) {
const message = generateRandomMessage();
await db.query({
text: `INSERT INTO queue (header, body) VALUES ($1, $2)`,
values: [message.header, message.body],
});
}
await db.end();
} catch (error) {
console.error("Error pushing messages to the queue:", error);
}
}
// push 10x100 messages to the queue in parallel
console.log("Starting to push messages to the queue with 10 workers...");
Promise.all([
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
pushMessagesToQueue(),
]).then(() => {
console.log("Database populated successfully.");
countMessagesInQueue();
});