Skip to content

Commit

Permalink
fix: add benchamark tests, fix reference msgs' (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 authored Feb 5, 2024
1 parent fb4db88 commit 2001963
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/restapi/src/lib/chat/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { aesDecrypt } from './aes';
import { getEncryptedSecret } from './getEncryptedSecret';
import { getGroup } from '../getGroup';
import { cache } from '../../helpers/cache';
import { getCID } from '../ipfs';

const SIG_TYPE_V2 = 'eip712v2';

Expand Down Expand Up @@ -574,6 +575,18 @@ export const decryptAndVerifyMessage = async (
} catch (err) {
decryptedMessage.messageObj = decryptedMessageObj;
}

try {
if ((decryptedMessage.messageObj as any).reference) {
const reference = (decryptedMessage.messageObj as any).reference;
if (reference && reference.split(':').length === 1) {
const message: any = await getCID(reference, { env });
(decryptedMessage.messageObj as any).reference = message.cid;
}
}
} catch (err) {
// Ignore Dangling Reference
}
}
} catch (err) {
decryptedMessage.messageContent = decryptedMessage.messageObj =
Expand Down
1 change: 0 additions & 1 deletion packages/restapi/src/lib/chat/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export async function getCID(
const message: Message = response.data;
return message;
} catch (err) {
console.error(`[Push SDK] - API ${getCID.name}: `, err);
throw Error(`[Push SDK] - API ${getCID.name}: ${err}`);
}
}
156 changes: 156 additions & 0 deletions packages/restapi/tests/lib/benchmark/chatMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { ethers } from 'ethers';
import Constants from '../../../src/lib/constants';
import { PushAPI } from '../../../src/lib/pushapi/PushAPI';

const _env = Constants.ENV.PROD;

/**
* THIS TEST GROUP IS FOR BENCHMARKING CHAT PERFORMANCE
* These tests will be skipped
*/
describe.skip('CHAT MESSAGES', () => {
let account: string;
let account2: string;
let userAlice: PushAPI;
let userBob: PushAPI;

before(async () => {
// UserAlice
const WALLET_PRIVATE_KEY = process.env['WALLET_PRIVATE_KEY'];
const Pkey = `0x${WALLET_PRIVATE_KEY}`;
const signer = new ethers.Wallet(Pkey);
account = `eip155:${signer.address}`;
userAlice = await PushAPI.initialize(signer, {
env: _env,
alpha: { feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2] },
});

// UserBob
const WALLET2 = ethers.Wallet.createRandom();
const signer2 = new ethers.Wallet(WALLET2.privateKey);
account2 = `eip155:${signer2.address}`;
userBob = await PushAPI.initialize(signer2, {
env: _env,
alpha: { feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2] },
});

// Send Request to UserBob
await userAlice.chat.send(account2, {
content: 'Hello',
type: 'Text',
});
// Accept Request from UserAlice
await userBob.chat.accept(account);

console.log('Users initialized');
});

let currentChatmessages = 1;

/**
* HELPER FUNCTIONS
*/
/**
* EXCANGE MESSAGES BETWEEN TWO USERS
* asumme chat request is already sent and accepted
* @param userAlice
* @param userBob
*/
const exchangeMessages = async (
userAlice: PushAPI,
userBob: PushAPI,
account1: string,
account2: string,
messageCount: number
) => {
// Exchange some messages (50 each)
for (let i = currentChatmessages + 1; i <= messageCount; i++) {
if (i % 2 === 0) {
await userAlice.chat.send(account2, {
content: `Hello Bob${i}`,
type: 'Text',
});
} else {
await userBob.chat.send(account1, {
content: `Hey Alice${i}`,
type: 'Text',
});
}

if (i % 100 === 0) {
console.log('Messages exchanged : ', i);
}
}

currentChatmessages =
currentChatmessages > messageCount ? currentChatmessages : messageCount;
};

const getLatestMessageTime = async () => {
const startTime = new Date();
await userAlice.chat.latest(account2);
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();
console.log('Chat.latest - Duration in ms : ', duration);
};

const getHistoricalMessagesTime = async () => {
const startTime = new Date();
const messages = await userAlice.chat.history(account2, { limit: 30 });
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();
console.log('Chat.hostory - Duration in ms : ', duration);
};

const sendMessageAvgTime = async (msgCount: number) => {
const startTime = new Date();
const prevCount = currentChatmessages;
await exchangeMessages(userAlice, userBob, account, account2, msgCount);
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();
const messagesSent = currentChatmessages - prevCount;
console.log('Chat.send - Duration in ms : ', duration / messagesSent);
};

const sendRequestsToNewUsers = async (userCount: number) => {
for (let i = 0; i < userCount; i++) {
const WALLET = ethers.Wallet.createRandom();
const signer = new ethers.Wallet(WALLET.privateKey);
const account = `eip155:${signer.address}`;
await userAlice.chat.send(account, {
content: 'Hello',
type: 'Text',
});
}
};

it('Chat.list', async () => {
// await sendRequestsToNewUsers(29);
const startTime = new Date();
const chats = await userAlice.chat.list('CHATS', { limit: 10 });
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();
console.log(chats.length);
console.log('Chat.list - Duration in ms : ', duration);
});
it('10 messages', async () => {
await sendMessageAvgTime(10);
// await getLatestMessageTime();
// await getHistoricalMessagesTime();
});
it('50 messages', async () => {
await sendMessageAvgTime(50);
// await getLatestMessageTime();
// await getHistoricalMessagesTime();
});
it('100 messages', async () => {
await sendMessageAvgTime(100);
// await getLatestMessageTime();
// await getHistoricalMessagesTime();
});
it('250 messages', async () => {
// await sendMessageAvgTime(250);
await getLatestMessageTime();
await getHistoricalMessagesTime();
});
});
15 changes: 13 additions & 2 deletions packages/restapi/tests/lib/chat/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct im
import { expect } from 'chai';
import { ethers } from 'ethers';
import CONSTANTS from '../../../src/lib/constantsV2';
import { CHAT } from '../../../src/lib/types/messageTypes';

describe('PushAPI.chat functionality', () => {
let userAlice: PushAPI;
Expand All @@ -15,6 +16,7 @@ describe('PushAPI.chat functionality', () => {
let signer2: any;
let account2: string;
const MESSAGE = 'Hey There!!!';
const env = CONSTANTS.ENV.DEV;

beforeEach(async () => {
const WALLET1 = ethers.Wallet.createRandom();
Expand All @@ -25,8 +27,8 @@ describe('PushAPI.chat functionality', () => {
signer2 = new ethers.Wallet(WALLET2.privateKey);
account2 = WALLET2.address;

userAlice = await PushAPI.initialize(signer1);
userBob = await PushAPI.initialize(signer2);
userAlice = await PushAPI.initialize(signer1, { env });
userBob = await PushAPI.initialize(signer2, { env });
});

it('Should list request ', async () => {
Expand Down Expand Up @@ -117,4 +119,13 @@ describe('PushAPI.chat functionality', () => {
);
expect(decryptedMessagePayloads).to.be.an('array');
});
it('Should be able to parse old reaction Messages', async () => {
const message = await userAlice.chat.send(account2, {
type: CONSTANTS.CHAT.MESSAGE_TYPE.REACTION,
content: CHAT.REACTION.CLAP,
reference: 'bafyreigxrhne5bitpww7hfxsx7u6mghxix4ehfwghtl4aofnbui255rslm',
});
const requests = await userBob.chat.list('REQUESTS');
console.log(requests[0].msg.messageObj);
});
});

0 comments on commit 2001963

Please sign in to comment.