bench: use res.end() for chunked encoding
authorBen Noordhuis <info@bnoordhuis.nl>
Wed, 19 Dec 2012 13:09:10 +0000 (14:09 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Thu, 20 Dec 2012 10:44:10 +0000 (11:44 +0100)
Use res.end() for the final chunk so we can benchmark the 'hot path' shortcut
in lib/http.js that packs the headers and the body into a single packet.

benchmark/http_simple.js
benchmark/http_simple_auto.js

index 1b3cee7..236b046 100644 (file)
@@ -96,13 +96,12 @@ var server = http.createServer(function (req, res) {
                             'Transfer-Encoding': 'chunked' });
     // send body in chunks
     var len = body.length;
-    var step = ~~(len / n_chunks) || len;
+    var step = Math.floor(len / n_chunks) || 1;
 
-    for (var i = 0; i < len; i += step) {
-      res.write(body.slice(i, i + step));
+    for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
+      res.write(body.slice(i * step, i * step + step));
     }
-
-    res.end();
+    res.end(body.slice((n_chunks - 1) * step));
   } else {
     var content_length = body.length.toString();
 
index 3192635..f72cc01 100644 (file)
@@ -77,13 +77,12 @@ var server = http.createServer(function (req, res) {
                             "Transfer-Encoding": "chunked" });
     // send body in chunks
     var len = body.length;
-    var step = ~~(len / n_chunks) || len;
+    var step = Math.floor(len / n_chunks) || 1;
 
-    for (var i = 0; i < len; i += step) {
-      res.write(body.slice(i, i + step));
+    for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
+      res.write(body.slice(i * step, i * step + step));
     }
-
-    res.end();
+    res.end(body.slice((n_chunks - 1) * step));
   } else {
     var content_length = body.length.toString();