title: Nginx + uWSGI + Flask app performance test with ab
body: <p>I've run into some issue with Nginx + uWSGI + flask while trying to benchmark my flask app. The combination is quite slow as my tests have shown. I have a fresh installation of Nginx 1.1.19 and uWSGI 2.0 on a Ubuntu 12.04 VM with 4 cores and 4 GB RAM. (Nginx and uWSGI config below)</p>
  
  <p>I did a benchmark of Nginx by itself serving a static 20 byte file and I was able to get as much as 80k req/sec. I then did a benchmark of Nginx + uWSGI + a very basic flask app (Hello world example on the flask site) and I was only able to get a max of 8k req/sec (a factor of 10 reduction)</p>
  
  <p>I turned on logging in Nginx and uWSGI (plus stats socket) and formatted the logs to print the request processing time for both and here's what I was able to glean:</p>
  
  <p>uWSGI avg. req time = <strong>0ms</strong><br>
  Nginx avg. req time = <strong>125ms</strong> (Nginx log times includes time spent in uWSGI)</p>
  
  <p>I did the same test with my flask app and the result followed the same pattern</p>
  
  <p>uWSGI avg. req time = <strong>4ms</strong><br>
  Nginx avg. req time = <strong>815ms</strong></p>
  
  <p><strong>PROBLEM:</strong> It appears there's a huge amount of time spent in the communication between Nginx and uWSGI. Has anyone seen this problem before??? I've tried all kinds of configurations for Nginx and uWSGI all with the same result. </p>
  
  <p>Note that I did the tests, with apachebench (ab), both locally on the VM and from a remote machine with the same results. </p>
  
  <p><strong>Nginx conf</strong>  </p>
  
  <pre><code>user www-data;
  worker_processes 4;
  worker_rlimit_nofile 200000;
  pid /var/run/nginx.pid;
  
  events {
          worker_connections  10240;
          #multi_accept on;
          #use epoll;
  }
  
  http {
          sendfile on;
          tcp_nopush on;
          tcp_nodelay on;
          keepalive_timeout 10;
          types_hash_max_size 2048;
          client_body_timeout 10;
          send_timeout 2;
          gzip on;
          gzip_disable "msie6";
          keepalive_disable "msie6";
  
          include       /etc/nginx/mime.types;
          default_type  application/octet-stream;
  
          access_log  off;
          error_log  /var/log/nginx/error.log crit;
  
          log_format ugzip '$remote_addr - "$request" - $status $body_bytes_sent - [$request_time]';
  
          ##
          # Virtual Host Configs
          ##
          #include /etc/nginx/conf.d/*.conf;
          include /etc/nginx/sites-enabled/*;
  
          server {
                  listen       80;
                  server_name  localhost;
  
                  access_log  /var/log/nginx-access.log  ugzip  buffer=32k;
                  error_log  /var/log/nginx-error.log;
  
                  location /myapplication {
                          uwsgi_pass     unix:/tmp/bdds.sock;
                          include         uwsgi_params;
                          uwsgi_param UWSGI_SCHEME        $scheme;
                  }
          }
  }
  </code></pre>
  
  <p><strong>uWSGI conf</strong> (relevant parts)</p>
  
  <pre><code>[uwsgi]
  master                  = true
  listen                  = 40000
  chmod-socket            = 666
  socket                  = /tmp/bdds.sock
  workers                 = 8
  harakiri                = 60
  harakiri-verbose        = true
  reload-mercy            = 8
  logto                   = /var/log/uwsgi-app.log
  logformat               = %(proto) %(method) %(uri) - %(status) %(rsize) - [%(msecs)]  
  vacuum                  = true
  no-orphans              = true
  #cpu-affinity           = 1
  stats                   = /tmp/stats.sock
  </code></pre>
  
  <p>Is this a common behavior for Nginx + uWSGI? Is there something blatantly incorrect with my configuration? I'm running this on a 4 core/4 GB RAM Xen VM with Ubuntu 12.04.</p>
  
  <p>Thanks in advance.  </p>