11import { WebSocket as WsWebSocket } from 'ws'
22import { WebSocket as UndiciWebSocket } from '../../index.js'
3- import { bench , run , group } from 'mitata'
3+ import { bench , run , lineplot } from 'mitata'
44
5- const __BINARY_SIZE__ = 1024 * 256
5+ const __BINARY_SIZE__ = 1024 * 512
66
77const binary = Buffer . alloc ( __BINARY_SIZE__ , '_' ) . toString ( 'utf-8' )
88
99const url = 'http://localhost:5001'
1010
1111const connections = [ ]
1212
13- group ( 'send' , ( ) => {
13+ lineplot ( ( ) => {
1414 {
1515 const ws = new WsWebSocket ( url )
1616 let _resolve
1717 ws . on ( 'message' , ( ) => {
1818 _resolve ( )
1919 } )
20- bench ( 'ws' , ( ) => {
21- return new Promise ( ( resolve , reject ) => {
22- ws . send ( binary )
23- _resolve = resolve
20+ bench ( 'ws ($messages)' , function * ( state ) {
21+ const messages = state . get ( 'messages' )
22+ const chunk = binary . subarray ( 0 , __BINARY_SIZE__ / messages )
23+ yield ( ) => new Promise ( ( resolve , reject ) => {
24+ for ( let i = 0 ; i < messages ; ++ i ) ws . send ( chunk )
25+ let id = 0
26+ _resolve = ( ) => {
27+ if ( ++ id === messages ) {
28+ resolve ( )
29+ }
30+ }
2431 } )
25- } )
32+ } ) . range ( 'messages' , 1 , 256 )
2633 connections . push ( ws )
2734 }
2835 {
@@ -31,12 +38,19 @@ group('send', () => {
3138 ws . addEventListener ( 'message' , ( ) => {
3239 _resolve ( )
3340 } )
34- bench ( 'undici' , ( ) => {
35- return new Promise ( ( resolve , reject ) => {
36- ws . send ( binary )
37- _resolve = resolve
41+ bench ( 'undici ($messages)' , function * ( state ) {
42+ const messages = state . get ( 'messages' )
43+ const chunk = binary . subarray ( 0 , __BINARY_SIZE__ / messages )
44+ yield ( ) => new Promise ( ( resolve , reject ) => {
45+ for ( let i = 0 ; i < messages ; ++ i ) ws . send ( chunk )
46+ let id = 0
47+ _resolve = ( ) => {
48+ if ( ++ id === messages ) {
49+ resolve ( )
50+ }
51+ }
3852 } )
39- } )
53+ } ) . range ( 'messages' , 1 , 256 )
4054 connections . push ( ws )
4155 }
4256} )
0 commit comments