Improve idle benchmarks
authorRyan Dahl <ry@tinyclouds.org>
Wed, 27 Oct 2010 09:52:49 +0000 (02:52 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 27 Oct 2010 09:52:49 +0000 (02:52 -0700)
benchmark/idle_clients.js
benchmark/idle_server.js

index 9bb7862..65988ab 100644 (file)
@@ -2,30 +2,41 @@ net = require('net');
 
 var errors = 0, connections = 0;
 
+var lastClose = 0;
+
+function maybeConnect (s) {
+  var now = new Date();
+  if (now - lastClose > 5000) {
+    // Just connect immediately
+    connect();
+  } else {
+    // Otherwise wait a little - see if this one is connected still. Just to
+    // avoid spinning at 100% cpu when the server totally rejects our
+    // connections.
+    setTimeout(function () {
+      if (s.writable && s.readable) connect();
+    }, 100);
+  }
+}
+
 function connect () {
   process.nextTick(function () {
     var s = net.Stream();
     var gotConnected = false;
     s.connect(9000);
+
     s.on('connect', function () {
       gotConnected = true;
       connections++;
-      connect();
+      maybeConnect(s);
     });
 
-    var haderror = false;
-
     s.on('close', function () {
       if (gotConnected) connections--;
-      //if (!haderror) connect();
-    });
-
-    s.on('end', function () {
-      s.end();
+      lastClose = new Date();
     });
 
     s.on('error', function () {
-      haderror = true;
       errors++;
     });
   });
index 5d08897..53afc77 100644 (file)
@@ -5,10 +5,6 @@ var errors = 0;
 
 server = net.Server(function (socket) {
 
-  socket.on('end', function () {
-    socket.end();
-  });
-
   socket.on('error', function () {
     errors++; 
   });