performance - Nginx + uWSGI + Flask 应用程序性能测试与 ab

标签 performance nginx flask uwsgi apachebench

我在尝试对我的 Flask 应用程序进行基准测试时遇到了 Nginx + uWSGI + flask 的一些问题。正如我的测试所示,这种组合非常缓慢。我在具有 4 个内核和 4 GB RAM 的 Ubuntu 12.04 VM 上全新安装了 Nginx 1.1.19 和 uWSGI 2.0。 (Nginx 和 uWSGI 配置如下)

我做了一个 Nginx 的基准测试,它本身提供一个静态的 20 字节文件,我能够获得高达 80k req/sec 的速度。然后我做了一个 Nginx + uWSGI + 一个非常基本的 flask 应用程序的基准测试( flask 网站上的 Hello world 示例),我只能获得最大 8k req/sec(减少 10 倍)

我打开登录 Nginx 和 uWSGI(加上 stats 套接字)并格式化日志以打印两者的请求处理时间,这是我能够收集的内容:

uWSGI 平均值请求时间 = 0 毫秒
Nginx 平均请求时间 = 125 毫秒 (Nginx 日志时间包括在 uWSGI 中花费的时间)

我用我的 flask 应用程序做了同样的测试,结果遵循相同的模式

uWSGI 平均值请求时间 = 4ms
Nginx 平均请求时间 = 815ms

问题: Nginx 和 uWSGI 之间的通信似乎花费了大量时间。有没有人见过这个问题???我已经为 Nginx 和 uWSGI 尝试了各种配置,结果都一样。

请注意,我使用 apachebench (ab) 在本地 VM 和远程机器上进行了测试,结果相同。

Nginx 配置

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;
                }
        }
}

uWSGI conf (相关部分)
[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

这是 Nginx + uWSGI 的常见行为吗?我的配置有什么明显不正确的地方吗?我在带有 Ubuntu 12.04 的 4 核/4 GB RAM Xen VM 上运行它。

提前致谢。

最佳答案

我猜你每秒发送的请求比同步部分(uWSGI + Flask)可以处理的要多得多。这使得请求大部分时间都卡在异步部分 (nginx) 上。

关于performance - Nginx + uWSGI + Flask 应用程序性能测试与 ab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104066/

相关文章:

android - 如何以编程方式启用/关闭移动数据

ios - Storyboard中的 View 太多 - Xcode 运行缓慢

php - NGINX + PHP-FPM-是否应该将它们放在单独的容器中?

docker - 如何将 nginx docker 镜像暴露到 80 以外的端口?

javascript - 将 chessbord.js 与 Flask 集成

java - 对于任务关键型工作负载,64 位 JVM 与 32 位一样好吗?

MySQL 分区 (innoDB) - 大表

php - nGinx 和 PhpMyAdmin 重定向问题上的 Varnish

python - 使用 Flask-classy 查看特定的上下文处理器

python - Flask 在我的包的 'static' 目录中找不到文件