Optimize initial storage service fetch

This commit is contained in:
Fedor Indutny 2022-03-09 10:22:34 -08:00 committed by GitHub
parent cc51cdccc7
commit a72cf075ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 172 additions and 36 deletions

View file

@ -0,0 +1,67 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-await-in-loop, no-console */
import { StorageState } from '@signalapp/mock-server';
import { Bootstrap, saveLogs } from './fixtures';
const CONTACT_COUNT = 1000;
(async () => {
const contactNames = new Array<string>();
for (let i = 0; i < CONTACT_COUNT; i += 1) {
contactNames.push(`Contact ${i}`);
}
const bootstrap = new Bootstrap({
benchmark: true,
});
await bootstrap.init();
const { phone, server } = bootstrap;
let state = StorageState.getEmpty();
for (const [i, profileName] of contactNames.entries()) {
const contact = await server.createPrimaryDevice({
profileName,
});
state = state.addContact(contact, {
// Make sure we fetch profile from the server
givenName: `Loading ${profileName}...`,
identityKey: contact.publicKey.serialize(),
profileKey: contact.profileKey.serialize(),
});
if (i >= contactNames.length - 4) {
state = state.pin(contact);
}
}
await phone.setStorageState(state);
const start = Date.now();
const app = await bootstrap.link();
try {
const window = await app.getWindow();
const leftPane = window.locator('.left-pane-wrapper');
const item = leftPane.locator(
'_react=BaseConversationListItem' +
`[title = ${JSON.stringify(contactNames[contactNames.length - 1])}]`
);
await item.waitFor();
const duration = Date.now() - start;
console.log(`Took: ${(duration / 1000).toFixed(2)} seconds`);
} catch (error) {
await saveLogs(bootstrap);
throw error;
} finally {
await app.close();
await bootstrap.teardown();
}
})();

View file

@ -59,6 +59,8 @@ export type BootstrapOptions = Readonly<{
linkedDevices?: number;
contactCount?: number;
contactNames?: ReadonlyArray<string>;
contactPreKeyCount?: number;
}>;
type BootstrapInternalOptions = Pick<BootstrapOptions, 'extraConfig'> &
@ -66,6 +68,7 @@ type BootstrapInternalOptions = Pick<BootstrapOptions, 'extraConfig'> &
benchmark: boolean;
linkedDevices: number;
contactCount: number;
contactNames: ReadonlyArray<string>;
}>;
//
@ -107,12 +110,13 @@ export class Bootstrap {
this.options = {
linkedDevices: 5,
contactCount: MAX_CONTACTS,
contactNames: CONTACT_NAMES,
benchmark: false,
...options,
};
assert(this.options.contactCount <= MAX_CONTACTS);
assert(this.options.contactCount <= this.options.contactNames.length);
}
public async init(): Promise<void> {
@ -123,11 +127,16 @@ export class Bootstrap {
const { port } = this.server.address();
debug('started server on port=%d', port);
const contactNames = CONTACT_NAMES.slice(0, this.options.contactCount);
const contactNames = this.options.contactNames.slice(
0,
this.options.contactCount
);
this.privContacts = await Promise.all(
contactNames.map(async profileName => {
const primary = await this.server.createPrimaryDevice({ profileName });
const primary = await this.server.createPrimaryDevice({
profileName,
});
for (let i = 0; i < this.options.linkedDevices; i += 1) {
// eslint-disable-next-line no-await-in-loop