Quantcast
Channel: 软件应用 –笑遍世界
Viewing all articles
Browse latest Browse all 51

Python WSGI Server性能简易评估

$
0
0

WSGI概念

WSGI的全称是Web Server Gateway Interface,这是一个规范,描述了web server如何与web application交互、web application如何处理请求。

PEP 3333中详细定义了WSGI规范:https://www.python.org/dev/peps/pep-3333/

Python中常见的WSGI服务器有:Gunicorn, uWSGI,bjoern, Meinheld等。
本人用得较多的是Gunicorn,而Instagram(公开演讲中提到)使用的uWSGI。

测试脚本

一个最简单的application放在app.py中:

def application(environment, start_response):
    """
    The main WSGI Application. Doesn't really do anything
    since we're benchmarking the servers, not this code :)
    """

    start_response(
        '200 OK',  # Status
        [('Content-type', 'text/plain'), ('Content-Length', '2')]  # Headers
    )
    return ['OK']

Bjoern.wsgi文件:

import bjoern
from app import application

bjoern.run(
    wsgi_app=application,
    host='0.0.0.0',
    port=8000,
    reuse_port=True
)

Gunicorn+Meinheld脚本:

#!/usr/bin/env bash

PROCESSOR_COUNT=$(nproc)

# This formula is recommended in the Gunicorn documentation
# http://docs.gunicorn.org/en/stable/design.html#how-many-workers
GUNICORN_WORKER_COUNT=$(( PROCESSOR_COUNT * 2 + 1 ))

gunicorn -w ${GUNICORN_WORKER_COUNT} --worker-class="meinheld.gmeinheld.MeinheldWorker" -b 0.0.0.0:8400 app:application

其中,测试工具使用wrk工具https://github.com/wg/wrk,脚本如下:

#!/bin/bash

IP=192.168.122.140
PORTS=(8000 8100 8200 8300 8400)
CONNECTIONS=(100 500 1000 5000 10000)
THREADS=8
DURATION=30
BASE=$1

ulimit -n 10240


function perf() {
    echo "    Testing with $1 threads and $2 connections ..."
    ./wrk --duration $DURATION --threads $1 --connections "$2" "http://$IP:$3" > "$3_$1_$2.log"
}

for connections in "${CONNECTIONS[@]}"; do
    for port in "${PORTS[@]}"; do
        perf "$THREADS" "$connections" "$port"
        sleep 1
    done
done

代码脚本等,详见:https://github.com/smilejay/python/tree/master/wsgi_benchmarking_2018

测评结果

在某个2核4G内存的虚拟机上,用上面提到的最简易的程序进行评估,得到如下图的数据,表示100、500、1000、5000、10000并发连接数下每秒处理的请求数。

性能测评结论

鉴于对各个WSGI server的配置改变就回来较大的性能差异,以及本例只是一个简单的返回一个“OK”字符串的的示例,所以此处结论并不一定准确,请大家谨慎参考。
1. bjoern 和 Gunicorn+Meinheld 并列第一;但是bjoern可能过于简单,例如它不支持HTTP 1.1,所以可能不是在生产环境中最好的选择,不过对于提供后端REST API可能是个不错的选择。
2. uWSGI 性能高于 Gunicorn,二者都是非常成熟的项目,文档和各种支持都比较完善;当然Gunicorn与Meinheld配合使用时性能是极好的。
3. CherryPy 在本次测评中倒数第一,由于我对它并不熟悉,所以估计有配置方面的问题,因为参考资料中有一篇文章测试表明它比uWSGI的性能要好。

参考资料

  • https://github.com/wg/wrk (HTTP Benchmark测试工具)
  • https://github.com/jonashaag/bjoern/tree/master/bjoern
  • http://gunicorn.org/
  • http://meinheld.org/
  • https://uwsgi-docs.readthedocs.io/en/latest/index.html
  • http://docs.cherrypy.org/en/latest/index.html#
  • https://blog.appdynamics.com/engineering/an-introduction-to-python-wsgi-servers-part-1/
  • https://blog.appdynamics.com/engineering/a-performance-analysis-of-python-wsgi-servers-part-2/

Original article: Python WSGI Server性能简易评估

©2018 笑遍世界. All Rights Reserved.


Viewing all articles
Browse latest Browse all 51

Trending Articles