title: Extremely Poor Performance for Basic Node.js Service on OSX
body: <p>I'm relatively new to Node.js (been playing with it for a while, but just starting to use it for serious stuff). I was experiencing some very odd benchmark behavior when testing locally, and decided to build the most basic possible HTTP server and benchmark it with Apache Benchmark (ab).</p>
  
  <p>Here's my code:</p>
  
  <pre><code>var http = require('http');
  
  http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Hello, world!');
    res.end();
  }).listen(8888);
  </code></pre>
  
  <p>When I run ab like so:</p>
  
  <pre><code>ab -n 1000000 -c 100 'http://127.0.0.1:8888/'
  </code></pre>
  
  <p>I get the following results:</p>
  
  <pre><code>This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
  Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  Licensed to The Apache Software Foundation, http://www.apache.org/
  
  Benchmarking 127.0.0.1 (be patient)
  apr_socket_recv: Operation timed out (60)
  Total of 16413 requests completed
  </code></pre>
  
  <p>I'm using nodejs version 0.10.25. I've also set my ulimit to 8192 (the max) before running my server and ab -- so file descriptor limits can't be reached.</p>
  
  <p>Here's what I don't understand:</p>
  
  <ul>
  <li>Why is AB not able to make all 1 million requests? Why is it failing after only ~16000?</li>
  <li>Is this normal behavior? I was expecting to see a lot of throughput, and be able to finish the tests quickly.</li>
  </ul>
  
  <p>Thanks!</p>
  
  <p>Bonus: I've recorded a small screencast you can view here: <a href="http://recordit.co/9i0ja6GZqR" rel="nofollow noreferrer">http://recordit.co/9i0ja6GZqR</a> This shows what I'm talking about.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After seeing this post: <a href="https://stackoverflow.com/questions/9156537/why-does-a-simple-thin-server-stop-responding-at-16500-requests-when-benchmarkin?rq=1">Why does a simple Thin server stop responding at 16500 requests when benchmarking?</a> and running:</p>
  
  <pre><code>sudo sysctl -w net.inet.tcp.msl=1000
  </code></pre>
  
  <p>I was able to successfully complete my ab command with the following results:</p>
  
  <pre><code>This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
  Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  Licensed to The Apache Software Foundation, http://www.apache.org/
  
  Benchmarking 127.0.0.1 (be patient)
  Completed 100000 requests
  Completed 200000 requests
  Completed 300000 requests
  Completed 400000 requests
  Completed 500000 requests
  Completed 600000 requests
  Completed 700000 requests
  Completed 800000 requests
  Completed 900000 requests
  Completed 1000000 requests
  Finished 1000000 requests
  
  
  Server Software:
  Server Hostname:        127.0.0.1
  Server Port:            8888
  
  Document Path:          /
  Document Length:        13 bytes
  
  Concurrency Level:      100
  Time taken for tests:   338.545 seconds
  Complete requests:      1000000
  Failed requests:        0
  Write errors:           0
  Total transferred:      114000000 bytes
  HTML transferred:       13000000 bytes
  Requests per second:    2953.82 [#/sec] (mean)
  Time per request:       33.854 [ms] (mean)
  Time per request:       0.339 [ms] (mean, across all concurrent requests)
  Transfer rate:          328.84 [Kbytes/sec] received
  
  Connection Times (ms)
                min  mean[+/-sd] median   max
  Connect:        0   14 212.3      0    8363
  Processing:     0   20 114.8     11    4205
  Waiting:        0   20 114.2     11    4205
  Total:          0   34 240.8     11    8368
  
  Percentage of the requests served within a certain time (ms)
    50%     11
    66%     13
    75%     14
    80%     14
    90%     17
    95%     21
    98%     32
    99%    605
   100%   8368 (longest request)
  </code></pre>
  
  <p>Unfortunately, I'm unable to go beyond 100 concurrent requests -- if I go up to 200, for instance, I get the following:</p>
  
  <pre><code>rdegges at Randalls-MacBook-Pro in ~
  ○ ab -n 1000000 -c 200 'http://127.0.0.1:8888/'
  This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;
  Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  Licensed to The Apache Software Foundation, http://www.apache.org/
  
  Benchmarking 127.0.0.1 (be patient)
  apr_socket_recv: Connection reset by peer (54)
  </code></pre>
  
  <p>It seems like I'm essentially capped at 100 concurrent requests.</p>
  
  <p>The other thing I'm concerned about is the response time from the first ab command. It loks like the mean response time was about 33.854ms, which seems awfully high considering what the server is doing.</p>
  
  <p>So I guess my question is: what can I do to improve concurrent requests and decrease processing time per request? I'm sure I'm doing something wrong here.</p>
  
  <p>Thanks!</p>