-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups.ts
198 lines (176 loc) · 5.21 KB
/
groups.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import generatedInboxes from "@helpers/generated-inboxes.json";
import { type Worker, type WorkerManager } from "@workers/manager";
import { type Client, type Conversation, type Group } from "@xmtp/node-sdk";
/**
* Creates a group with specified participants and measures performance
*/
export async function createGroupWithBatch(
creator: Worker,
allWorkers: WorkerManager,
batchSize: number,
installationsPerUser: number,
): Promise<{
groupId: string | undefined;
memberCount: number;
totalInstallations: number;
executionTimeMs: number;
}> {
const startTime = performance.now();
const logLabel = `create group with ${batchSize} participants (${batchSize * installationsPerUser} installations)`;
console.time(logLabel);
const group = await creator.client?.conversations.newGroup(
allWorkers
.getWorkers()
.map((w) => w.client.inboxId)
.slice(0, batchSize),
);
const members = await group?.members();
const totalInstallations = (members ?? []).reduce(
(sum, m) => sum + (m?.installationIds.length ?? 0),
0,
);
console.log(
`Group created: ${group?.id} | Members: ${members?.length} | Installations: ${totalInstallations}`,
);
console.timeEnd(logLabel);
return {
groupId: group?.id,
memberCount: members?.length ?? 0,
totalInstallations,
executionTimeMs: performance.now() - startTime,
};
}
/**
* Gets workers that are members of a group
*/
export async function getWorkersFromGroup(
group: Conversation,
workers: WorkerManager,
): Promise<Worker[]> {
await group.sync();
const memberIds = (await group.members()).map((m) => m.inboxId);
return workers
.getWorkers()
.filter((w) => memberIds.includes(w.client.inboxId));
}
export interface StressTestConfig {
largeGroups: number[];
workerCount: number;
messageCount: number;
groupCount: number;
sizeLabel: string;
}
export async function createAndSendDms(
workers: WorkerManager,
receiverInboxId: string,
messageCount: number,
) {
try {
for (const sender of workers.getWorkers()) {
await sender.client.conversations.sync();
const dm = await sender.client.conversations.newDm(receiverInboxId);
for (let i = 0; i < messageCount; i++) {
await dm.send("hello");
}
}
return true;
} catch (error) {
console.error(error);
throw error;
}
}
export async function createAndSendInGroup(
workers: WorkerManager,
client: Client,
groupCount: number,
receiverInboxId: string,
) {
try {
const allInboxIds = workers.getWorkers().map((w) => w.client.inboxId);
allInboxIds.push(receiverInboxId);
for (let i = 0; i < groupCount; i++) {
const groupName = `Test Group ${i} ${allInboxIds.length}`;
const group = await client.conversations.newGroup(allInboxIds, {
groupName,
groupDescription: "Test group for stress testing",
});
await group.send(`Hello from the group! ${i}`);
}
return true;
} catch (error) {
console.error(error);
throw error;
}
}
export async function createLargeGroup(
client: Client,
memberCount: number,
receiverInboxId: string,
): Promise<Group | undefined> {
try {
const MAX_BATCH_SIZE = 10;
const initialMembers = generatedInboxes
.slice(0, 1)
.map((entry) => entry.inboxId);
initialMembers.push(receiverInboxId);
const groupName = `Large Group ${memberCount}: ${initialMembers.length}`;
const group = await client.conversations.newGroup(initialMembers, {
groupName,
groupDescription: `Test group with ${memberCount} members`,
});
await group.sync();
for (let i = 1; i < memberCount; i += MAX_BATCH_SIZE) {
const endIdx = Math.min(i + MAX_BATCH_SIZE, memberCount);
const batchMembers = generatedInboxes
.slice(i, endIdx)
.map((entry) => entry.inboxId);
if (batchMembers.length > 0) {
await group.addMembers(batchMembers);
await group.sync();
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
await group.send(`Hello from the group with ${memberCount} members`);
return group;
} catch (error) {
console.error(error);
throw error;
}
}
export async function createLargeGroups(
config: StressTestConfig,
workers: WorkerManager,
client: Client,
receiverInboxId: string,
conversation?: Conversation,
) {
for (const size of config.largeGroups) {
try {
if (conversation) {
await conversation.send(`Creating group with ${size} members...`);
}
const group = await createLargeGroup(client, size, receiverInboxId);
if (!group) {
if (conversation) {
await conversation.send(
`❌ Failed to create group with ${size} members`,
);
}
continue;
}
if (conversation) {
await conversation.send(
`✅ Successfully created group with ${size} members (ID: ${group.id})`,
);
await conversation.send(`📨 Sending messages to group ${group.id}...`);
}
} catch (error) {
if (conversation) {
await conversation.send(
`❌ Error creating group with ${size} members: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
}
return true;
}