nginx,uwsgi,bottle,mako安装和性能测试

80294153 2011-11-30

上一篇nginx,uwsgi,bottle,virtualenv在centos6上安装及性能测试测试了nginx,uwsgi和bottle的性能

这篇继续测试一下mako模板性能

1、安装mako

tar -zvxf Mako-0.5.0.tar.gz 
cd Mako-0.5.0
python setup.py install

2、修改上篇nginx配置文件

/usr/local/nginx/conf/nginx.conf

worker_processes 8; #24线程cpu,8个nginx,16个uwsgi
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen      80;
        charset     utf-8;
        root        /application/venv/bottletest;
        server_name cow.com;
 
        location / {
            include uwsgi_params;
            uwsgi_param UWSGI_PYHOME /application/venv/bottletest;
            uwsgi_param UWSGI_CHDIR /application/venv/bottletest;
            uwsgi_param UWSGI_SCRIPT hello; # 对应hello.py
            uwsgi_pass  127.0.0.1:8888;
        }   
 
        location ^~ /static {
            root /application/venv/bottletest;
            access_log off;
        }   
 
        location /status {
            stub_status on;
            access_log  off;
        }
    }  
}

3、修改配置uwsgi的xml格式配置文件

/usr/local/uwsgi/bottletest.xml

<uwsgi>
    <socket>127.0.0.1:8888</socket>
    <home>/application/venv/bottletest</home>
    <chdir>/application/venv/bottletest</chdir>
    <python-path>/application/venv/bottletest</python-path>
    <module>[WSGI Script (hello)]</module>
    <limit-as>256MB</limit-as>
    <processes>16</processes> <!-- 进程数 -->
    <master/>
    <memory/>
    <logto>/usr/local/uwsgi/logs/bottletest.log</logto>
    <daemonize>/var/log/uwsgi.log</daemonize>
    <max-requests>10000</max-requests>
</uwsgi>

4、编辑python文件

/application/venv/bottletest/hello.py

from bottle import route, run, mako_view, default_app
@route('/hello/template/:names')
@mako_view('hello.html')
def template_hello(names):
   names = names.split(',')
   return dict(title='Hello World', names=names)
if __name__ == "__main__":
   run(host="192.168.0.90", port=8080)
else:
   application = default_app()

5、编辑mako模板文件

/application/venv/bottletest/view/index.html

<html>
 <head>
  <title>${title}</title>
 </head>
 <body>
  %for name in names:
    <p>Hello, <strong>${name}</strong></p>
  %endfor
 </body>
</html>

6、启动nginx和uwsgi

/usr/local/nginx/sbin/nginx
/usr/local/uwsgi/uwsgi -x /usr/local/uwsgi/bottletest.xml

7、测试访问结果

lynx --source http://192.168.0.90/hello/template/123,234,2343,21,adead,sa2221
<html>
 <head>
  <title>Hello World</title>
 </head>
 <body>
    <p>Hello, <strong>123</strong></p>
    <p>Hello, <strong>234</strong></p>
    <p>Hello, <strong>2343</strong></p>
    <p>Hello, <strong>21</strong></p>
    <p>Hello, <strong>adead</strong></p>
    <p>Hello, <strong>sa2221</strong></p>
 </body>
</html>

webbench模拟1000客户端测试,系统负载在3-4之间

./webbench -t 30 -c 1000 'http://192.168.0.90/hello/template/123,234'
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.0.90/hello/template/123,234
1000 clients, running 30 sec.

Speed=1202950 pages/min, 6295417 bytes/sec.
Requests: 601473 susceed, 2 failed.

webbench模拟5000客户端测试,系统负载在4左右

./webbench -t 30 -c 5000 'http://192.168.0.90/hello/template/123,234' 
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.0.90/hello/template/123,234
5000 clients, running 30 sec.

Speed=1313490 pages/min, 6872361 bytes/sec.
Requests: 656595 susceed, 150 failed.

总结

性能能够支持生产环境,cheetah的性能和mako差不多,但文档有些乱,且似乎不太活跃。

nginx+uwsgi+bottle+mako还是很不错的选择,由于数据库使用mongodb和redis,就不在测试ormapping的东西了。

相关推荐