0

I have created a websocket server using ratchet, and would like to monitor my servers CPU/Ram usage as the number of concurrent connections goes up. I've tried to do this in the dev console with a loop like so

var connections = new Array();
for(i = 0; i < 1000; i++){
    connections[i] = new WebSocket('ws://www.foo.bar:8080');
    connections[i].onopen = function(){
        connections[i].send('Connection #' + i +' here!');
    }
}

I've seen people load test WebSocket servers with a simple javascript script I just can't seem to figure out how they did it. Can anyone point me to such a script?

Native Coder
  • 1,792
  • 3
  • 16
  • 34
  • 1
    you need a for loop wrapper to preserve scope, just like the old setTimeout loop issues – dandavis Oct 04 '16 at 21:42
  • I can't say that I've encountered this issue. Care to elaborate? – Native Coder Oct 04 '16 at 21:45
  • 1
    if i answer, it's an insta dupe. search "for loop timeout", it's got to be on the top 10 questions ever – dandavis Oct 04 '16 at 21:46
  • OK. I did, but I don't see how those answers help me here? I guess I just don't understand the solution – Native Coder Oct 04 '16 at 21:47
  • 1
    just pretend you have a setTimeout. then your loop will work up to the max client connections, probably about 10 or so... – dandavis Oct 04 '16 at 21:50
  • WHAT are you talking about? Pretend that what is a setTimout? and why on earth would it cap at 10? I've seen people do this with thousands of connections? I'm a bit confused. LOL – Native Coder Oct 04 '16 at 21:51
  • 1
    pretend the for loop body contains a setTimeout... i call it a scope condom, but you just need an anon function wrapping the code if you want the open callback to operate. your browser caps at 10 sockets. – dandavis Oct 04 '16 at 21:55
  • Ok, that much makes sense. But then the question remains, how do these people do the load testing with thousands of connections? (It worked by the way. I just did it with 100 connections :D) – Native Coder Oct 04 '16 at 21:56
  • well, if a 100 work, why not 1000? (i don't think they will all really work, but you can try) – dandavis Oct 04 '16 at 21:59
  • I tried 1000, the browser runs out of resrouces at 255. But considering my server is a hyperthreaded i5 laptop, the results weren't horrible. 255 concurrent connections only used 50% of 1 virtual core. – Native Coder Oct 04 '16 at 22:02
  • interesting! can you actually send/receive 255 messages on those, or do they just open and die quietly? – dandavis Oct 04 '16 at 22:03
  • Let me PM you the results so that we aren't having a long discussion in the comments... – Native Coder Oct 05 '16 at 13:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125001/discussion-between-native-coder-and-dandavis). – Native Coder Oct 05 '16 at 13:53

0 Answers0