signal-desktop/ts/test-mock/benchmarks/startup_bench.ts

125 lines
3.4 KiB
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-await-in-loop, no-console */
import { ReceiptType } from '@signalapp/mock-server';
2022-07-08 20:46:25 +00:00
import { debug, Bootstrap, stats, RUN_COUNT } from './fixtures';
const MESSAGE_BATCH_SIZE = 1000; // messages
const ENABLE_RECEIPTS = Boolean(process.env.ENABLE_RECEIPTS);
2023-03-13 23:41:47 +00:00
Bootstrap.benchmark(async (bootstrap: Bootstrap): Promise<void> => {
await bootstrap.linkAndClose();
2023-03-13 23:41:47 +00:00
const { server, contacts, phone, desktop } = bootstrap;
2023-03-13 23:41:47 +00:00
const messagesPerSec = new Array<number>();
2023-03-13 23:41:47 +00:00
for (let runId = 0; runId < RUN_COUNT; runId += 1) {
// Generate messages
const messagePromises = new Array<Promise<Buffer>>();
debug('started generating messages');
2023-03-13 23:41:47 +00:00
for (let i = 0; i < MESSAGE_BATCH_SIZE; i += 1) {
const contact = contacts[Math.floor(i / 2) % contacts.length];
const direction = i % 2 ? 'message' : 'reply';
2023-03-13 23:41:47 +00:00
const messageTimestamp = bootstrap.getTimestamp();
2023-03-13 23:41:47 +00:00
if (direction === 'message') {
messagePromises.push(
2023-03-13 23:41:47 +00:00
contact.encryptText(
desktop,
2023-03-13 23:41:47 +00:00
`Ping from mock server ${i + 1} / ${MESSAGE_BATCH_SIZE}`,
{
timestamp: messageTimestamp,
2023-03-13 23:41:47 +00:00
sealed: true,
}
)
);
if (ENABLE_RECEIPTS) {
messagePromises.push(
2023-03-13 23:41:47 +00:00
phone.encryptSyncRead(desktop, {
timestamp: bootstrap.getTimestamp(),
2023-03-13 23:41:47 +00:00
messages: [
{
senderUUID: contact.device.uuid,
timestamp: messageTimestamp,
},
],
})
);
}
2023-03-13 23:41:47 +00:00
continue;
}
messagePromises.push(
phone.encryptSyncSent(
desktop,
`Pong from mock server ${i + 1} / ${MESSAGE_BATCH_SIZE}`,
{
timestamp: messageTimestamp,
destinationUUID: contact.device.uuid,
}
)
);
if (ENABLE_RECEIPTS) {
messagePromises.push(
contact.encryptReceipt(desktop, {
timestamp: bootstrap.getTimestamp(),
messageTimestamps: [messageTimestamp],
type: ReceiptType.Delivery,
})
);
messagePromises.push(
contact.encryptReceipt(desktop, {
timestamp: bootstrap.getTimestamp(),
messageTimestamps: [messageTimestamp],
type: ReceiptType.Read,
})
);
}
2023-03-13 23:41:47 +00:00
}
2023-03-13 23:41:47 +00:00
debug('ended generating messages');
2023-03-13 23:41:47 +00:00
const messages = await Promise.all(messagePromises);
2023-03-13 23:41:47 +00:00
// Open the flood gates
{
debug('got synced, sending messages');
2023-03-13 23:41:47 +00:00
// Queue all messages
const queue = async (): Promise<void> => {
await Promise.all(
messages.map(message => {
return server.send(desktop, message);
})
);
};
2023-03-13 23:41:47 +00:00
const run = async (): Promise<void> => {
const app = await bootstrap.startApp();
const appLoadedInfo = await app.waitUntilLoaded();
2023-03-13 23:41:47 +00:00
console.log('run=%d info=%j', runId, appLoadedInfo);
2023-03-13 23:41:47 +00:00
messagesPerSec.push(appLoadedInfo.messagesPerSec);
2023-03-13 23:41:47 +00:00
await app.close();
};
2023-03-13 23:41:47 +00:00
await Promise.all([queue(), run()]);
}
2023-03-13 23:41:47 +00:00
}
2023-03-13 23:41:47 +00:00
// Compute human-readable statistics
if (messagesPerSec.length !== 0) {
console.log('stats info=%j', { messagesPerSec: stats(messagesPerSec) });
}
2023-03-13 23:41:47 +00:00
});