-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stress: send a message with
n_clients/total clients
in channel (#668)
Closes #654 <img width="1081" alt="image" src="https://github.com/user-attachments/assets/283b6330-132d-4db9-8f48-1e442e57b3ea"> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced messaging capabilities during chat stress tests, including real-time updates on connected clients. - Introduced statistics reporting to monitor client interactions and engagement in the chat. - **Improvements** - Clarified variable naming for event IDs related to chat messages for better readability. - Expanded the `ChatConfig` interface, allowing for flexible event handling. - **Bug Fixes** - Managed lifecycle of statistics reporting to ensure accurate performance monitoring during chat sessions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: texuf <[email protected]>
- Loading branch information
1 parent
db61ce6
commit f8c0392
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { dlogger } from '@river-build/dlog' | ||
import type { StressClient } from '../../utils/stressClient' | ||
import { ChatConfig } from './types' | ||
|
||
const logger = dlogger('stress:statsReporter') | ||
|
||
export function statsReporter(rootClient: StressClient, chatConfig: ChatConfig) { | ||
let canceled = false | ||
let lastReactionCount = 0 | ||
const interval = setInterval(() => { | ||
if (canceled) { | ||
return | ||
} | ||
void (async () => { | ||
if (chatConfig.kickoffMessageEventId && chatConfig.countClientsMessageEventId) { | ||
const reactionCount = countReactions( | ||
rootClient, | ||
chatConfig.announceChannelId, | ||
chatConfig.kickoffMessageEventId, | ||
) | ||
if (canceled) { | ||
return | ||
} | ||
if (lastReactionCount === reactionCount) { | ||
return | ||
} | ||
lastReactionCount = reactionCount | ||
await updateCountClients( | ||
rootClient, | ||
chatConfig.announceChannelId, | ||
chatConfig.countClientsMessageEventId, | ||
chatConfig.clientsCount, | ||
reactionCount, | ||
) | ||
} | ||
})() | ||
}, 5000) | ||
|
||
return () => { | ||
logger.info('canceled') | ||
clearInterval(interval) | ||
canceled = true | ||
} | ||
} | ||
|
||
export const updateCountClients = async ( | ||
client: StressClient, | ||
announceChannelId: string, | ||
countClientsMessageEventId: string, | ||
totalClients: number, | ||
reactionCounts: number, | ||
) => { | ||
logger.info(`Clients: ${reactionCounts}/${totalClients} 🤖`) | ||
return await client.streamsClient.sendChannelMessage_Edit_Text( | ||
announceChannelId, | ||
countClientsMessageEventId, | ||
{ | ||
content: { | ||
body: `Clients: ${reactionCounts}/${totalClients} 🤖`, | ||
mentions: [], | ||
attachments: [], | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
export const countReactions = ( | ||
client: StressClient, | ||
announceChannelId: string, | ||
rootMessageId: string, | ||
) => { | ||
const channel = client.streamsClient.stream(announceChannelId) | ||
if (!channel) { | ||
return 0 | ||
} | ||
const message = channel.view.events.get(rootMessageId) | ||
if (!message) { | ||
return 0 | ||
} | ||
|
||
const reactions = channel.view.timeline.filter((event) => { | ||
if (event.localEvent?.channelMessage.payload.case === 'reaction') { | ||
return event.localEvent?.channelMessage.payload.value.refEventId === rootMessageId | ||
} | ||
if ( | ||
event.decryptedContent?.kind === 'channelMessage' && | ||
event.decryptedContent?.content.payload.case === 'reaction' | ||
) { | ||
return event.decryptedContent?.content.payload.value.refEventId === rootMessageId | ||
} | ||
return | ||
}) | ||
|
||
return reactions.length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters