| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | // 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'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-20 18:37:56 -04:00
										 |  |  | import { debug, Bootstrap, RUN_COUNT } from './fixtures'; | 
					
						
							|  |  |  | import { stats } from '../../util/benchmark/stats'; | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const MESSAGE_BATCH_SIZE = 1000; // messages
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ENABLE_RECEIPTS = Boolean(process.env.ENABLE_RECEIPTS); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  | Bootstrap.benchmark(async (bootstrap: Bootstrap): Promise<void> => { | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |   await bootstrap.linkAndClose(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |   const { server, contacts, phone, desktop } = bootstrap; | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |   const messagesPerSec = new Array<number>(); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |   for (let runId = 0; runId < RUN_COUNT; runId += 1) { | 
					
						
							|  |  |  |     // Generate messages
 | 
					
						
							|  |  |  |     const messagePromises = new Array<Promise<Buffer>>(); | 
					
						
							|  |  |  |     debug('started generating messages'); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07: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'; | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |       const messageTimestamp = bootstrap.getTimestamp(); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |       if (direction === 'message') { | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |         messagePromises.push( | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |           contact.encryptText( | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |             desktop, | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |             `Ping from mock server ${i + 1} / ${MESSAGE_BATCH_SIZE}`, | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |             { | 
					
						
							|  |  |  |               timestamp: messageTimestamp, | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |               sealed: true, | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |             } | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ENABLE_RECEIPTS) { | 
					
						
							|  |  |  |           messagePromises.push( | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |             phone.encryptSyncRead(desktop, { | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |               timestamp: bootstrap.getTimestamp(), | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |               messages: [ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                   senderUUID: contact.device.uuid, | 
					
						
							|  |  |  |                   timestamp: messageTimestamp, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |             }) | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07: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, | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |     debug('ended generating messages'); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |     const messages = await Promise.all(messagePromises); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |     // Open the flood gates
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       debug('got synced, sending messages'); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |       // Queue all messages
 | 
					
						
							|  |  |  |       const queue = async (): Promise<void> => { | 
					
						
							|  |  |  |         await Promise.all( | 
					
						
							|  |  |  |           messages.map(message => { | 
					
						
							|  |  |  |             return server.send(desktop, message); | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |       const run = async (): Promise<void> => { | 
					
						
							|  |  |  |         const app = await bootstrap.startApp(); | 
					
						
							|  |  |  |         const appLoadedInfo = await app.waitUntilLoaded(); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |         console.log('run=%d info=%j', runId, appLoadedInfo); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |         messagesPerSec.push(appLoadedInfo.messagesPerSec); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |         await app.close(); | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |       await Promise.all([queue(), run()]); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  |   // Compute human-readable statistics
 | 
					
						
							|  |  |  |   if (messagesPerSec.length !== 0) { | 
					
						
							|  |  |  |     console.log('stats info=%j', { messagesPerSec: stats(messagesPerSec) }); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:32:51 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-03-13 16:41:47 -07:00
										 |  |  | }); |