您的位置:首页 > 其它

搭建高性能web服务

2015-11-04 20:42 495 查看
要求:

安装实验环境与工具:nginx、gunicorn、gevent、curl、ab(apache 压力测试工具)等等

测试用程序: 自学教程 4.2 读写字典服务 definitions_readwrite.py。 请按需要在此基础上改写程序,必须使用 MongoDB 数据库。

准备 4 台电脑,记录下每台电脑配置。其中一台,安装 MongoDB ,不提供其他服务。

使用测试工具,测试单进程同步、Tornado 异步(需要改写程序和异步驱动)、GEvent 异步(需要使用wsgi方式启动)方式的性能。

使用测试工具,将前一步骤性能最好的方式,与多进程、多线程同步方式(用gunicorn wsgi方式启动)对比性能。

用 4 台电脑,组成一个性能最强的 web 服务

相关代码:

definitions_readwrite.py  

#user nginx;
worker_processes 4;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 4096;
use epoll;
}

http {
# Enumerate all the Tornado servers here
upstream frontends {
server 172.18.43.53:8001 weight=3;
server 172.18.42.169:8001 weight=4;
server 172.18.43.132:8001 weight=3;
}

include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

keepalive_timeout 65;
proxy_read_timeout 200;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/html text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;

# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;

server {
listen 80;

# Allow file uploads
client_max_body_size 50M;

location ^~ /static/ {
root /var/www;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}

location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends; }
}
}


View Code
环境配置:

$sudo pip install gunicorn
$sudo pip install greenlet
$sudo pip install eventlet
$sudo pip install gevent
$sudo apt-get install apache2-utils (用于压力测试)

实验步骤:

UML部署图:



以下的测试均在测试量为10000,并发量为100的情况下进行

1)进行单进程同步压力测试

  a)在A机上开启Mongodb服务, 命令如下:mongod.exe --dbpath "C:\Program Files\MongoDB\data\db" ;

  b)在B机中启动nginx;

  c)在C机, B机,D机中都运行python definition_readwrite.py作为应用服务器

  e)在C机中进行压力测试,命令为ab ab -n 1000 -c 10 http://localhost/ (localhost和端口号需要换成代理机的IP和端口号)

  f)可查看测试结果

2)其他的测试也类似,其中可通过查看如下的测试结果图比较

截图:

单进程同步:



tornado异步:



Gevent



多进程同步:



多线种同步:



说明 :1)要本地运行gunicorn时的命令一般为gunicorn code:application; 这样启动gunicorn,其默认作为一个监听127.0.01:8000的web server,可以通过本机访问,如果需要通过网络来访问gunicorn服务的话,需要使用-b命令来绑定不同的地址,同时设置监听端口,也就是需要使 用本机在网络中的IP地址加端口号;

  2)修改程序为tornado异步的,不仅方式要是异步的,同时也需要使用异步的mongodb;

  3) 用gevent运行程序,会自动将同步的程序的每个进程使其都支持IO阻塞异步的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: