flask笔记:11:gunicorn+gevent+nginx+flask部署,使用siege进行服务器压力测试
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash

·
环境是macos sierra 10.12.3
python3.5
先安装nginx
安装,解压tar.gz文件
Linux 下 权限chmod a+rwx *
./configure --without-http_rewrite_module
make && make install
启动:sudo /usr/local/nginx/sbin/nginx
打开浏览器,127.0.0.1
安装位置/usr/local/nginx
也可以使用brew安装
修改端口:
/usr/local/nginx/conf/nginx.conf
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
也可以使用brew安装
1,brew search nginx
2,brew install nginx
3,启动nginx ,sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了。
4,#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
5,#打开 nginx
sudo nginx
安装flask,gunicorn,gevent 使用pip install
sudo nginx
安装flask,gunicorn,gevent 使用pip install
配置nginx反向代理:
修改
nginx.conf文件
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#注释以下四行
#location / {
#root html;
#index index.html index.htm;
#}
#添加以下内容
location / {
try_files @uri @pp;
}
location @pp {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:5000;
#反向代理的这个IP和端口
}
创建flask项目:
目录结构:
flask_nginx
|——static(静态文件)
|——templates(模版)
|——flask_nginx.py(flask启动文件)
|——gun.conf(gunicorn配置文件)
|——debug.log(gunicorn日志文件)
编写flask_nginx.py
编写gun.conf
启动:gunicorn -c gun.conf(gunicorn配置文件) flask_nginx(flask启动文件):app
编写flask_nginx.py
from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
@app.route("/")
def index():
return "Hello World"
#项目的代理设置
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == "__main__":
app.run()
编写gun.conf
#监听本机的5000端口
bind='127.0.0.1:5000'
#开启4个进程
workers=4
backlog=2048
#工作模式为gevent
worker_class="gevent"
debug=True
proc_name='gunicorn.pid'
#记录PID
pidfile='debug.log'
loglevel=‘debug'
gun.conf也可以是一个py文件:
编写config.py:
import gevent.monkey
import multiprocessing
gevent.monkey.patch_all()
#监听本机的5000端口
bind='0.0.0.0:5000'
preload_app = True
#开启进程
#workers=4
workers = multiprocessing.cpu_count() * 2 + 1
#每个进程的开启线程
threads = multiprocessing.cpu_count() * 2
backlog=2048
#工作模式为gevent
worker_class="gevent"
# debug=True
#如果不使用supervisord之类的进程管理工具可以是进程成为守护进程,否则会出问题
daemon = True
#进程名称
proc_name='gunicorn.pid'
#进程pid记录文件
pidfile='app_pid.log'
loglevel='debug'
logfile = 'debug.log'
accesslog = 'access.log'
access_log_format = '%(h)s %(t)s %(U)s %(q)s'
errorlog = 'error.log’
启动:gunicorn -c gun.conf(gunicorn配置文件) flask_nginx(flask启动文件):app
启动:gunicorn -c config.py(gunicorn配置文件) flask_nginx(flask启动文件):app
启动nginx: sudo /usr/local/nginx/sbin/nginx
打开浏览器输入nginx的ip端口访问,出现Hello World为成功
下面测试一下post请求,不作对比,只是用 siege发起post请求
修改flask_nginx.py测试post请求:
测试命令:
siege -T ‘application/json’ -c 100 -r 5 'http://127.0.0.1:5000/ POST </Users/zjl/Desktop/postfile.json'
测试服务器性能:
linux下可以用Apache的ab
mac下使用siege
安装siege:
brew search siege(先查询有没有)
brew install siege(安装)
使用:
测试前,前面先把系统的端口限制数改大,看看Mac下面的默认限制
ulimit -a
会看到mac下最大数:
open files (-n) 2560
修改成10000
ulimit -n 10000 (网上说mac下只能开这么多,linux可以开到6w)
在用 ulimit -a 检查一下
如果发现已经修改了就ok了:
open files (-n) 10000
接下来把cpu检查打开,通常我们只看使用率最高的那个即可:
top -n1(n1就是第一个,如果要查看前四个就n4)
按q退出
Siege命令常用参数
-c 500 指定并发数500
-r 5 指定测试的次数5
-f urls.txt 制定url的文件
-i internet系统,随机发送url
-b 请求无需等待 delay=0
-t 5 持续测试5分钟,默认是分,5s为5秒
# -t和-r不能同时使用
模拟1000个并发向URL发送5次
测试:siege -c 1000 -r 5 http://127.0.0.1:8088(gunicorn+gevent+nginx+flask)
结果:
测试:siege -c 1000 -r 5 http://127.0.0.1:5000(gunicorn+gevent+flask)
结果:
Transactions(完成次数)
Availability(可用性)
Elapsed time(总共使用时长)
Data transferred(数据传输)
Response time(响应时间,显示网络连接的速度)
Transaction rate(平均每秒完成的处理次数)
Throughput(平均每秒传送数据)
Concurrency(实际最高并发连接数)
Successful transactions(成功处理次数)
Failed transactions(失败处理次数)
Longest transaction(最长传输时长)
Shortest transaction(最短传输时长)
下面测试一下post请求,不作对比,只是用 siege发起post请求
修改flask_nginx.py测试post请求:
# -*-coding:utf-8 -*-
__author__ = "ayou"
from flask import Flask
import flask_restful as restful
from flask_restful import reqparse
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
api = restful.Api(app)
parser = reqparse.RequestParser()
parser.add_argument('name', type=str)
parser.add_argument('age', type=int)
class HelloWorld(restful.Resource):
def get(self):
return {'hello': 'world'}
def post(self):
args = parser.parse_args()
return {"name":args["name"],"age":args["age"]}
api.add_resource(HelloWorld, '/')
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == "__main__":
app.run(debug=True)
post请求模拟:
创建一个postfile.json文件,内容是post参数
例如:
{
"name": "aaa",
"age": 111
}
测试命令:
siege -T ‘application/json’ -c 100 -r 5 'http://127.0.0.1:5000/ POST </Users/zjl/Desktop/postfile.json'




A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
所有评论(0)