-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.ts
109 lines (95 loc) · 3.04 KB
/
group.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
import { type Worker, type WorkerManager } from "./types";
/**
* Creates a group with a specified number of participants and measures performance
*
* @param creator - The worker that will create the group
* @param allWorkers - Record of all available workers
* @param batchSize - Number of participants to include in the group
* @param installationsPerUser - Number of installations per user (for logging purposes)
* @returns Object containing group information and performance metrics
*/
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 and total ${
batchSize * installationsPerUser
} installations`;
console.time(logLabel);
// Create the group with the specified number of participants
const group = await creator.client?.conversations.newGroup(
allWorkers
.getWorkers()
.map((worker) => worker.client.inboxId)
.slice(0, batchSize),
);
// Get group members and count installations
const members = await group?.members();
let totalInstallations = 0;
for (const member of members ?? []) {
totalInstallations += member.installationIds.length;
}
console.log(`Group created with id ${group?.id}`);
console.log(`Total members: ${members?.length}`);
console.log(`Total installations: ${totalInstallations}`);
console.timeEnd(logLabel);
const endTime = performance.now();
return {
groupId: group?.id,
memberCount: members?.length ?? 0,
totalInstallations,
executionTimeMs: endTime - startTime,
};
}
/**
* Creates multiple groups with increasing batch sizes
*
* @param creator - The worker that will create the groups
* @param allWorkers - Record of all available workers
* @param startBatchSize - Initial batch size
* @param batchIncrement - How much to increase batch size for each iteration
* @param maxParticipants - Maximum number of participants to include
* @param installationsPerUser - Number of installations per user
* @returns Array of results from each group creation
*/
export async function createGroupsWithIncrementalBatches(
creator: Worker,
allWorkers: WorkerManager,
startBatchSize: number = 5,
batchIncrement: number = 5,
maxParticipants: number,
installationsPerUser: number,
): Promise<
Array<{
batchSize: number;
groupId: string | undefined;
memberCount: number;
totalInstallations: number;
executionTimeMs: number;
}>
> {
const results = [];
let currentBatchSize = startBatchSize;
while (currentBatchSize <= maxParticipants) {
const result = await createGroupWithBatch(
creator,
allWorkers,
currentBatchSize,
installationsPerUser,
);
results.push({
batchSize: currentBatchSize,
...result,
});
currentBatchSize += batchIncrement;
}
return results;
}