Skip to content

Commit 59bcab9

Browse files
committed
chore: try perf test using reassure
1 parent 5bd1daa commit 59bcab9

File tree

6 files changed

+400
-10
lines changed

6 files changed

+400
-10
lines changed

package/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ xcuserdata
3232
coverage/
3333

3434
#################################################################################################
35+
.reassure/

package/jest-setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { FlatList, View } from 'react-native';
44
import mockRNCNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
55

66
import { registerNativeHandlers } from './src/native';
7+
import { configure } from 'reassure';
8+
9+
// For performance tests using reassure
10+
configure({ testingLibrary: 'react-native' });
711

812
// eslint-disable-next-line no-underscore-dangle
913

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
3+
import { cleanup } from '@testing-library/react-native';
4+
import { measureRenders } from 'reassure';
5+
6+
import { queryChannelsApi } from '../../../mock-builders/api/queryChannels';
7+
import { useMockedApis } from '../../../mock-builders/api/useMockedApis';
8+
import { generateChannel1, generateMember } from '../../../mock-builders/generator/channel';
9+
import { generateUser } from '../../../mock-builders/generator/user';
10+
import { getTestClientWithUser } from '../../../mock-builders/mock';
11+
import { Chat } from '../../Chat/Chat';
12+
import { ChannelList } from '../ChannelList';
13+
14+
describe('ChannelList', () => {
15+
let chatClient;
16+
let testChannel1;
17+
let testChannel2;
18+
let member;
19+
20+
beforeEach(async () => {
21+
jest.clearAllMocks();
22+
chatClient = await getTestClientWithUser({ id: 'khushal' });
23+
const user = generateUser({ id: 'khushal' });
24+
member = generateMember({ user });
25+
testChannel1 = generateChannel1({ members: [member] });
26+
testChannel2 = generateChannel1({ members: [member] });
27+
});
28+
29+
afterEach(cleanup);
30+
31+
test('ChannelList 10 times', async () => {
32+
useMockedApis(chatClient, [queryChannelsApi([testChannel1, testChannel2])]);
33+
34+
const scenario = async () => {};
35+
const props = {
36+
filters: {},
37+
};
38+
39+
await measureRenders(<ChannelList filters={props.filters} />, {
40+
wrapper: ({ children }) => <Chat client={chatClient}>{children}</Chat>,
41+
});
42+
});
43+
});

package/src/mock-builders/generator/channel.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { Channel, ChannelResponse } from 'stream-chat';
23
import { v4 as uuidv4 } from 'uuid';
34

45
import { generateUser, getUserDefaults } from './user';
@@ -92,6 +93,74 @@ export const generateChannel = (customValues: { [key: string]: any }) =>
9293
return { ...accumulated, [current]: customValues[current] };
9394
}, getChannelDefaults());
9495

96+
export const generateChannel1 = (options: { channel: any; config: any; members: any }) => {
97+
const { channel: optionsChannel, config, ...optionsBesidesChannel } = options;
98+
const idFromOptions = optionsChannel && optionsChannel.id;
99+
const type = (optionsChannel && optionsChannel.type) || 'messaging';
100+
const id = idFromOptions
101+
? idFromOptions
102+
: options.members && options.members.length
103+
? `!members-${uuidv4()}`
104+
: uuidv4();
105+
return {
106+
...optionsBesidesChannel,
107+
messages: [],
108+
members: [],
109+
channel: {
110+
type,
111+
id,
112+
created_at: '2020-04-28T11:20:48.578147Z',
113+
updated_at: '2020-04-28T11:20:48.578147Z',
114+
created_by: {
115+
id: 'vishal',
116+
role: 'user',
117+
created_at: '2020-04-27T13:05:13.847572Z',
118+
updated_at: '2020-04-28T11:21:08.357468Z',
119+
last_active: '2020-04-28T11:21:08.353026Z',
120+
banned: false,
121+
online: false,
122+
},
123+
frozen: false,
124+
config: {
125+
created_at: '2020-04-24T11:36:43.859020368Z',
126+
updated_at: '2020-04-24T11:36:43.859022903Z',
127+
name: 'messaging',
128+
typing_events: true,
129+
read_events: true,
130+
connect_events: true,
131+
search: true,
132+
reactions: true,
133+
replies: true,
134+
mutes: true,
135+
uploads: true,
136+
url_enrichment: true,
137+
message_retention: 'infinite',
138+
max_message_length: 5000,
139+
automod: 'disabled',
140+
automod_behavior: 'flag',
141+
commands: [
142+
{
143+
name: 'giphy',
144+
description: 'Post a random gif to the channel',
145+
args: '[text]',
146+
set: 'fun_set',
147+
},
148+
],
149+
...config,
150+
},
151+
...optionsChannel,
152+
},
153+
};
154+
};
155+
156+
export const generateMember = (options: { user: { id: string } }) => ({
157+
user_id: options.user.id,
158+
is_moderator: false,
159+
invited: false,
160+
role: 'member',
161+
...options,
162+
});
163+
95164
export const generateChannelResponse = (
96165
customValues: {
97166
channel?: Record<string, any>;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const StreamChat = require('stream-chat').StreamChat;
2+
3+
const client = StreamChat.getInstance('nq447pefn5by');
4+
const user = {
5+
id: 'khushalagarwalgetstreamio',
6+
token:
7+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoia2h1c2hhbGFnYXJ3YWxnZXRzdHJlYW1pbyJ9.9ZQA9dwRDagqmRJ-EPr8yKtnIR_O--GjKeXQ3-XLIoA',
8+
};
9+
10+
async function connectUserAndCreateChannels() {
11+
try {
12+
await client.connectUser({ id: user.id }, user.token);
13+
console.log('User Connected');
14+
await createPollAndSendToChannel();
15+
} catch (err) {
16+
console.error('Error connecting user:', err);
17+
}
18+
}
19+
20+
async function deleteAllChannels() {
21+
const channels = await client.queryChannels(
22+
{
23+
type: 'messaging',
24+
},
25+
{},
26+
{ limit: 100 },
27+
);
28+
for (const channel of channels) {
29+
await channel.delete();
30+
console.log(`Channel ${channel.id} deleted`);
31+
await new Promise((resolve) => setTimeout(resolve, 500));
32+
}
33+
}
34+
35+
async function createChannelsWithThrottle(index) {
36+
if (index >= 10) {
37+
console.log('All channels created!');
38+
return;
39+
}
40+
41+
try {
42+
const channel = client.channel('messaging', `channel-${index}`, {
43+
name: `Channel ${index}`,
44+
members: [user.id, 'tommaso-80'],
45+
});
46+
47+
await channel.create(); // Wait for channel creation
48+
await channel.sendMessage({ text: `Channel ${index}! This is a test message.` });
49+
console.log(`Channel ${index} created and message sent!`);
50+
51+
setTimeout(() => createChannelsWithThrottle(index + 1), 500); // Schedule next channel
52+
} catch (err) {
53+
console.error(`Error creating Channel ${index}:`, err);
54+
55+
// Retry with exponential backoff
56+
setTimeout(() => createChannelsWithThrottle(index), 500);
57+
}
58+
}
59+
60+
async function sendReactionToAMessage() {
61+
for (let i = 0; i < 100; i++) {
62+
const channel = client.channel('messaging', 'sample-app-channel-3');
63+
await channel.watch();
64+
const message = await channel.sendMessage({ text: 'Hello, world!' });
65+
const reaction = await channel.sendReaction(message.message.id, {
66+
type: 'love',
67+
user_id: `tommaso-${i}`,
68+
});
69+
console.log(`Reaction sent to message ${message.message.id} for user ${user.id}`);
70+
await new Promise((resolve) => setTimeout(resolve, 500));
71+
}
72+
}
73+
74+
const createPollAndSendToChannel = async () => {
75+
const channel = client.channel('messaging', 'sample-app-channel-0');
76+
await channel.watch();
77+
const poll = await client.polls.createPoll({
78+
allow_answers: true,
79+
name: 'What is the best Framework?',
80+
enforce_unique_vote: false,
81+
options: [
82+
{ text: 'Option 1' },
83+
{ text: 'Option 2' },
84+
{ text: 'Option 3' },
85+
{ text: 'Option 4' },
86+
{ text: 'Option 5' },
87+
],
88+
});
89+
const message = await channel.sendMessage({ poll_id: poll.id });
90+
console.log('Poll created and sent to channel:', poll.id, poll.state.getLatestValue().options);
91+
};
92+
93+
const castPollVotes = async () => {
94+
for (let i = 0; i < 96; i++) {
95+
await client.castPollVote(
96+
'25d2e42a-1c9d-4a1f-9844-385c49fdd0b1',
97+
'eb3356ee-4e71-4cd3-a6f8-fe95d8055dc2',
98+
{
99+
option_id: 'e858b508-7099-4368-8832-66504f863ed8',
100+
},
101+
`tommaso-${i}`,
102+
);
103+
await new Promise((resolve) => setTimeout(resolve, 500));
104+
}
105+
};
106+
107+
connectUserAndCreateChannels();

0 commit comments

Comments
 (0)