关于gevent 结合uwgsi下的一个测试总结:

背景

本来想结合一下uwgsi和gevent 一起使用来提供并发处理,可是使用AB测试发现,似乎没多少作用,不知道是不是自己什么地方有错误配置,整体上效果也不是很理想。

但是在测试过程中也有些其他小收获,再次进行记录一下。

测试

单实例的 -------相关配置文件:
nginx:

server {
    listen      10022;
    charset     utf-8;
    root        /data/www/KnowledgePay;
    server_name 127.0.0.1;

        location /favicon.ico {
                log_not_found off;
        access_log off;
        }

    location / {
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /data/www/KnowledgePay;
        uwsgi_param UWSGI_CHDIR /data/www/KnowledgePay;
        uwsgi_param UWSGI_SCRIPT main; # 对应main.py
        uwsgi_pass  127.0.0.1:10021;
        proxy_connect_timeout      3; #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout         6; #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout         6; #连接成功后,后端服务器响应时间(代理接收超时)
    }

    access_log  /data/logs/nginx/KnowledgePay_access.log  main;
}

uwgis:

[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10021
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
limit-as = 512
listen = 2014
reload-on-as = 256
reload-on-rss = 192
processes = 2
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true
gevent = 2014

main.py


#!/usr/bin/evn python
# coding=utf-8
"""
Author = zyx
@Create_Time: 2018/4/14 0:58
@version: v1.0.0
@Contact: 308711822@qq.com
@File: main.py
@文件功能描述:
"""

import logging
import os
import sys

# from gevent import monkey
# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
# monkey.patch_all()

import gevent.monkey
gevent.monkey.patch_all()


import bottle
from beaker.middleware import SessionMiddleware
from bottle import TEMPLATE_PATH
from bottle import default_app, run, get, error, hook, response, request, install, HTTPResponse

from basic.net import web_helper

#############################################
# 初始化bottle框架相关参数----应用程序的主入口文件
# 调用的时候
#############################################

# 获取本脚本所在的路径
pro_path = os.path.split(os.path.realpath(__file__))[0]
sys.path.append(pro_path)

# 设置当前bottle运行提交的最大数据值为2M
bottle.BaseRequest.MEMFILE_MAX = 1024 * 1024 * 2

# 定义静态文件目录路径
static_path = os.path.join(pro_path, 'template')

# 定义模板路径
TEMPLATE_PATH.append(os.path.join(pro_path, 'template'))


@get('/')
def callback():
    return '嗨!小钟同学!'


@get('/log/')
def callback():
    pass


# @error(500)
# def miss(code):
#     #     # 错误页面,一般来说,可以在网站制定一个404的HTML页面,然后用return template('404')去访问404这个页面
#     pass
#     return web_helper.return_msg('500', '服务器系统故障')
#
#
# import traceback
#
#
# @error(403)
# def miss(code):
#     # 错误页面,一般来说,可以在网站制定一个404的HTML页面,然后用return template('404')去访问404这个页面
#     pass
#     return web_helper.return_msg('403', '没权限')


@error(404)
def miss(code):
    # 错误页面,一般来说,可以在网站制定一个404的HTML页面,然后用return template('404')去访问404这个页面
    pass


def allow_cross_domain(fn):
    def _enable_cors(*args, **kwargs):
        # set cross headers
        response.headers['Access-Control-Allow-Origin'] = '*'
        response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,OPTIONS'
        allow_headers = 'Referer, Accept, Origin, User-Agent,X-Requested-With, Content-Type, X-File-Name'
        response.headers['Access-Control-Allow-Headers'] = allow_headers

        print('打印!!request.method!', request.method)
        if request.method == 'OPTIONS':
            # actual request; reply with the actual response
            res = response.copy(cls=HTTPResponse)
            res.status = 200
            res.body = ''
            raise res
            response.close()
            return fn(*args, **kwargs)

    return _enable_cors


@hook('before_request')
def validate():
    """
    钩子函数,处理请求路由之前需要做什么的事情
    :return:
    """

    # # 让bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求
    # REQUEST_METHOD = request.environ.get('REQUEST_METHOD')
    # HTTP_ACCESS_CONTROL_REQUEST_METHOD = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_METHOD')
    # if REQUEST_METHOD == 'OPTIONS' and HTTP_ACCESS_CONTROL_REQUEST_METHOD:
    #     request.environ['REQUEST_METHOD'] = HTTP_ACCESS_CONTROL_REQUEST_METHOD

    # 获取当前访问的Url路径
    path_info = request.environ.get("PATH_INFO")
    # 过滤不用做任何操作的路由
    if path_info in ['/favicon.ico', '/check_err/', '/log/']:
        return ''
    # 记录客户端提交参数信息----实测是成功
    # web_helper.write_request_log(path_info)
    # response.headers['Access-Control-Allow-Origin'] = '*'
    # response.headers['Access-Control-Allow-Credentials'] = 'true'


@hook('after_request')
# @allow_cross_domain
def enable_cors():
    """
    钩子函数,处理请求路由之后需要做什么的事情
    :return:
    """
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Credentials'] = 'true'


# def validation(auth, auth_value):
#     # print('受伤的',auth, auth_value)
#     return True

# 安装JWT插件
# install(JwtPlugin(validation, 'xiaozhong520', algorithm='HS256'))

# 导入 API 路由模块
from business.logic.api_routes import login, register, get_course, get_course_content
from basic.net import session_helper
from basic.cache import redis_cache_helper

# 导入缓存配置模块
from business.configs import redis_config
# 导入日志模块
from basic.utils import logging_helper

# 导入的JWT模块
from basic.jwt.bottlejwt import JwtPlugin

# 函数主入口
if __name__ == '__main__':
    # 日志初始化:
    # 系统日志
    logging_helper.sys_log_init('sys', pro_path)
    # 请求日志
    logging_helper.req_log_init('info', pro_path)
    # 错误日志
    logging_helper.error_log_init('error', pro_path)

    # 初始化缓存库信息配置
    redis_cache_helper.init(_redis=redis_config.const)
    # 启动前都先把缓存清理一次,仅仅测试使用
    # redis_cache_helper.flush()
    # 启动程序
    app_argv = SessionMiddleware(default_app(), session_helper.session_opts)

    run(app=app_argv, host='127.0.0.1', port=8188, debug=True, reloader=True)
else:
    # 系统日志
    logging_helper.sys_log_init('sys', pro_path)
    # 请求日志
    logging_helper.req_log_init('info', pro_path)
    # 错误日志
    logging_helper.error_log_init('error', pro_path)
    # 初始化缓存库信息配置
    redis_cache_helper.init(_redis=redis_config.const)
    # 初始化email发送参数
    # mail_helper.set_mail(const.MAIL)
    application = SessionMiddleware(default_app(), session_helper.session_opts)

需要测试业务逻辑接口:

#!/usr/bin/evn python
# coding=utf-8
"""
Author = zyx
@Create_Time: 2018/4/25 10:48
@version: v1.0.0
@Contact: 308711822@qq.com
@File: get_course.py
@文件功能描述:
"""

from bottle import get, post, request
from basic.net import web_helper
from basic.utils.except_helper import exception_handling_2

from business.logic.api_db_logics import course_logic
from basic.utils import logging_helper

@get('/api/v1/course/get/')
@post('/api/v1/course/get/')
@exception_handling_2
def callback():
    pass
    '''获取课程列表'''
    # print('提交的参数', request.forms.__dict__)
    page_num = web_helper.check_args('page_num', 'page_num', is_check_null=False)  # 默认开启 不能为空的验证
    # page_num = 1
    logging_helper.req_info_write("33333333333333")
    print('参数信息',page_num)
    if not page_num:
        page_num = 1

    '''1:连接数据库并读取数据列表'''
    result_list, is_ends = course_logic.get_course_paginate(page_num=page_num)

    return_date = {
        "data": result_list
    }
    if is_ends and int(page_num) > 1:
        return web_helper.return_msg("9992", "加载完毕", return_date)

    if not result_list:
        return web_helper.return_msg("9991", "暂无课程", return_date)

    return web_helper.return_msg("0000", "获取成功", return_date)

数据操作逻辑处理:

#!/usr/bin/evn python
# coding=utf-8
# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +
#        ┏┓   ┏┓+ +
#    ┏┛┻━━━┛┻┓ + +
#    ┃       ┃  
#    ┃   ━   ┃ ++ + + +
#    ████━████ ┃+
#    ┃       ┃ +
#    ┃   ┻   ┃
#    ┃       ┃ + +
#    ┗━┓   ┏━┛
#      ┃   ┃           
#      ┃   ┃ + + + +
#      ┃   ┃    Codes are far away from bugs with the animal protecting   
#      ┃   ┃ +     神兽保佑,代码无bug  
#      ┃   ┃
#      ┃   ┃  +         
#      ┃    ┗━━━┓ + +
#      ┃        ┣┓
#      ┃        ┏┛
#      ┗┓┓┏━┳┓┏┛ + + + +
#       ┃┫┫ ┃┫┫
#       ┗┻┛ ┗┻┛+ + + +
# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +"""
"""
Author = zyx
@Create_Time: 2018/4/25 11:23
@version: v1.0.0
@Contact: 308711822@qq.com
@File: course_logic.py
@文件功能描述:
"""

from business.model_db.knowledgepay_model import session_scope, Course
from basic.cache import redis_cache_helper
from basic.utils import logging_helper

# 分页查询
def get_course_paginate(page_num=1):
    # # 缓存的Key
    # cache_key_course_paginate = 'get_course_paginate' + str(page_num)
    # # 获取缓存值
    # result_list = redis_cache_helper.get(cache_key_course_paginate)
    # # 判断是否有值
    # logging_helper.req_info_write("11111111111")
    # if result_list:
    #     print('直接的从缓存中返回')
    #     logging_helper.req_info_write("222222直接的从缓存中返回")
    #     if result_list == 'null':
    #         return [], True  # 查询没有结果的时候
    #     return result_list, False

    # 否则从数据库中进行读取
    print('否则从数据库中进行读取')
    with session_scope():
        result = Course.select().dicts().order_by(Course.id).paginate(int(page_num), 4)
        logging_helper.req_info_write("把对应的结果保存到缓存中")
        if not result:
            # 把对应的结果保存到缓存中---缓存穿透:处理
            # redis_cache_helper.set(cache_key_course_paginate, "null", timeout=20)
            return [], True  # 查询没有结果的时候
        # for row in result: result_list.append(row)
        result_list = [v for v in result]  # 使用列表推导式
        # 把对应的结果保存到缓存中

        # redis_cache_helper.set(cache_key_course_paginate, result_list, timeout=10)
        # 返回最终查询结果
        return result_list, False

数据库连接逻辑:

#!/usr/bin/evn python
# coding=utf-8
# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +
#        ┏┓   ┏┓+ +
#    ┏┛┻━━━┛┻┓ + +
#    ┃       ┃  
#    ┃   ━   ┃ ++ + + +
#    ████━████ ┃+
#    ┃       ┃ +
#    ┃   ┻   ┃
#    ┃       ┃ + +
#    ┗━┓   ┏━┛
#      ┃   ┃           
#      ┃   ┃ + + + +
#      ┃   ┃    Codes are far away from bugs with the animal protecting   
#      ┃   ┃ +     神兽保佑,代码无bug  
#      ┃   ┃
#      ┃   ┃  +         
#      ┃    ┗━━━┓ + +
#      ┃        ┣┓
#      ┃        ┏┛
#      ┗┓┓┏━┳┓┏┛ + + + +
#       ┃┫┫ ┃┫┫
#       ┗┻┛ ┗┻┛+ + + +
# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +"""
"""
Author = zyx
@Create_Time: 2018/5/3 12:31
@version: v1.0.0
@Contact: 308711822@qq.com
@File: pg_peewee_helper.py
@文件功能描述: 使用peewee封装的ORM
"""

from playhouse.pool import PooledPostgresqlExtDatabase
from business.configs import db_config
from peewee import *

# 使用连接池的情况
database = PooledPostgresqlExtDatabase(
    db_config.const.get('db_name'),
    max_connections=db_config.const.get('max_connections'),
    stale_timeout=db_config.const.get('stale_timeout'),  # 5 minutes.
    timeout=db_config.const.get('timeout'),
    **{'user': db_config.const.get('db_user'), 'host': db_config.const.get('db_host'),
       'password': db_config.const.get('db_pass'), 'port': db_config.const.get('db_port')})

# 不使用连接池的情况
# database = PostgresqlDatabase( db_config.const.get('db_name'),
#                                **{'user': db_config.const.get('db_user'), 'host': db_config.const.get('db_host'),
#                                   'password': db_config.const.get('db_pass'), 'port': db_config.const.get('db_port')})

# database = None


def GetSession():
    return database


from contextlib import contextmanager


@contextmanager
def session_scope():
    session = GetSession()
    if session.is_closed():
        # session.connection_context()
        session.connect()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()


def session_scope2():
    session = GetSession()
    with session.manual_commit():
        session.begin()  # Begin transaction explicitly.
        try:
            yield session
        except:
            session.rollback()  # Rollback -- an error occurred.
            raise
        else:
            try:
                session.commit()  # Attempt to commit changes.
            except:
                session.rollback()  # Error committing, rollback.
                raise


基于以上的配置文件下测试条件说明:

  • 测试1 --使用数据库连接池的情况,不使用缓存的情况下:
    测试命令:
[root@web1 ~]# ab -n 6000 -c 1000 http://127.0.0.1:10022/api/v1/course/get/?page_num=7

测试结果1:

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   28.131 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    213.29 [#/sec] (mean)
Time per request:       4688.502 [ms] (mean)
Time per request:       4.689 [ms] (mean, across all concurrent requests)
Transfer rate:          66.86 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.4      0      26
Processing:    47 4264 1135.2   4789    5208
Waiting:       21 4264 1135.2   4789    5208
Total:         47 4267 1128.1   4789    5209

Percentage of the requests served within a certain time (ms)
  50%   4789
  66%   4806
  75%   4827
  80%   4851
  90%   4908
  95%   4930
  98%   5007
  99%   5027
 100%   5209 (longest request)
[root@web1 ~]# 

测试结果说明:

Time taken for tests:   28.131 seconds
#测试总耗时
Complete requests:      6000
#请求完成数
Failed requests:        0 
#请求失败数
Requests per second:    213.29 [#/sec] (mean)
#吞吐率,平均每秒处理
Time per request:       4688.502 [ms] (mean)
//用户平均请求等待时间
Time per request:       4.689 [ms] (mean, across all concurrent requests)
//服务器平均请求处理时间,大家最关心的指标之三

Percentage of the requests served within a certain time (ms)
  50%   4789
  66%   4806
  75%   4827
  80%   4851
  90%   4908
  95%   4930
  98%   5007
  99%   5027
 100%   5209 (longest request)
#每个请求处理时间的分布情况,50%的处理时间在4789ms内,66%的处理时间在4806ms内...,一般主要关注点是:看90%的处理时间---

测试结果2:

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   28.039 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    213.98 [#/sec] (mean)
Time per request:       4673.247 [ms] (mean)
Time per request:       4.673 [ms] (mean, across all concurrent requests)
Transfer rate:          67.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   9.1      0      30
Processing:    37 4262 1177.7   4641    5878
Waiting:        9 4262 1177.7   4641    5878
Total:         37 4266 1169.5   4641    5878

Percentage of the requests served within a certain time (ms)
  50%   4641
  66%   4807
  75%   4857
  80%   4904
  90%   5278
  95%   5574
  98%   5773
  99%   5776
 100%   5878 (longest request)

===============================

  • 测试2 --不使用数据库连接池的情况,不使用缓存的情况下:


    image.png

    重启一下应用:

supervisor> restart know 

测试命令:

[root@web1 ~]# ab -n 6000 -c 1000 http://127.0.0.1:10022/api/v1/course/get/?page_num=7

测试结果1:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   62.372 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    96.20 [#/sec] (mean)
Time per request:       10395.260 [ms] (mean)
Time per request:       10.395 [ms] (mean, across all concurrent requests)
Transfer rate:          30.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.2      0      25
Processing:    49 9566 2421.3  10548   10621
Waiting:       24 9566 2421.4  10548   10621
Total:         49 9569 2414.4  10549   10621

Percentage of the requests served within a certain time (ms)
  50%  10549
  66%  10557
  75%  10566
  80%  10576
  90%  10592
  95%  10608
  98%  10615
  99%  10617
 100%  10621 (longest request)

测试结果2:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   60.297 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    99.51 [#/sec] (mean)
Time per request:       10049.465 [ms] (mean)
Time per request:       10.049 [ms] (mean, across all concurrent requests)
Transfer rate:          31.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   6.6      0      23
Processing:    43 9247 2316.7  10483   10569
Waiting:       20 9247 2316.7  10483   10569
Total:         43 9250 2310.5  10483   10569

Percentage of the requests served within a certain time (ms)
  50%  10483
  66%  10508
  75%  10517
  80%  10525
  90%  10541
  95%  10556
  98%  10562
  99%  10565
 100%  10569 (longest request)

测试说明:----数据变化比较大

Time taken for tests:   60.297 seconds
Requests per second:    99.51 [#/sec] (mean)
 50%  10483
  66%  10508
  75%  10517
  80%  10525
  90%  10541
  • 测试3 --使用数据库连接池的情况,且【使用了缓存】的情况下:

测试结果1:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   3.786 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    1584.90 [#/sec] (mean)
Time per request:       630.953 [ms] (mean)
Time per request:       0.631 [ms] (mean, across all concurrent requests)
Transfer rate:          496.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.7      0      27
Processing:    46  568 129.1    610     647
Waiting:       25  568 129.2    610     647
Total:         52  571 122.2    610     653

Percentage of the requests served within a certain time (ms)
  50%    610
  66%    627
  75%    633
  80%    639
  90%    643
  95%    643
  98%    645
  99%    646
 100%    653 (longest request)

测试结果2:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   3.784 seconds
Complete requests:      6000
Failed requests:        0
Write errors:           0
Total transferred:      1926000 bytes
HTML transferred:       594000 bytes
Requests per second:    1585.63 [#/sec] (mean)
Time per request:       630.664 [ms] (mean)
Time per request:       0.631 [ms] (mean, across all concurrent requests)
Transfer rate:          497.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   8.0      0      28
Processing:    38  570 120.6    612     655
Waiting:       23  570 120.6    612     655
Total:         52  574 113.3    612     667

Percentage of the requests served within a certain time (ms)
  50%    612
  66%    613
  75%    614
  80%    616
  90%    620
  95%    623
  98%    623
  99%    634
 100%    667 (longest request)

测试结果----变化也非常大,说明了使用缓存的好处:

Requests per second:    1585.63 [#/sec] (mean)---吞吐率提升了!
Time per request:       630.664 [ms] (mean)
Time per request:       0.631 [ms] (mean, across all concurrent requests)
 50%    612
  66%    613
  75%    614
  80%    616
  90%    620
  95%    623
  98%    623
  99%    634
  • 测试4 --使用数据库连接池的情况,且【使用了缓存】的情况下,
    修改UWSGI监听队列数:
image.png

[图片上传中...(image.png-5eff43-1525683334599-0)]

配置

修改的地方:

listen = 100
gevent = 100

测试结果1:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   7.307 seconds
Complete requests:      6000
Failed requests:        643
   (Connect: 0, Receive: 0, Length: 643, Exceptions: 0)
Write errors:           0
Non-2xx responses:      643
Total transferred:      1929215 bytes
HTML transferred:       637081 bytes
Requests per second:    821.13 [#/sec] (mean)
Time per request:       1217.834 [ms] (mean)
Time per request:       1.218 [ms] (mean, across all concurrent requests)
Transfer rate:          257.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.3      0      25
Processing:    32  655 1525.8     62    7244
Waiting:       26  655 1525.5     62    7244
Total:         51  658 1531.1     62    7256

Percentage of the requests served within a certain time (ms)
  50%     62
  66%     63
  75%     66
  80%     68
  90%   3053
  95%   4042
  98%   7252
  99%   7255
 100%   7256 (longest request)

测试结果2:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   8.730 seconds
Complete requests:      6000
Failed requests:        618
   (Connect: 0, Receive: 0, Length: 618, Exceptions: 0)
Write errors:           0
Non-2xx responses:      618
Total transferred:      1929090 bytes
HTML transferred:       635406 bytes
Requests per second:    687.30 [#/sec] (mean)
Time per request:       1454.971 [ms] (mean)
Time per request:       1.455 [ms] (mean, across all concurrent requests)
Transfer rate:          215.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   9.0      0      30
Processing:    16  650 1460.8     63    7238
Waiting:       16  650 1460.7     63    7238
Total:         43  654 1467.4     63    7256

Percentage of the requests served within a certain time (ms)
  50%     63
  66%     65
  75%     67
  80%     70
  90%   3050
  95%   3333
  98%   7223
  99%   7254
 100%   7256 (longest request)

测试说明----即便我们使用了缓存!!!一定的程度上影响到了请求处理!:

Complete requests:      6000
Failed requests:        618
Requests per second:    687.30 [#/sec] (mean)
Percentage of the requests served within a certain time (ms)
  50%     63
  66%     65
  75%     67
  80%     70
  90%   3050
  95%   3333
  98%   7223
  99%   7254
 100%   7256 (longest request)
  • 测试5 --使用数据库连接池的情况,且【使用了缓存】的情况下,
    修改UWSGI--的gevent = 100,提升一下这个:


    image.png
gevent = 2014

测试结果1:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   8.715 seconds
Complete requests:      6000
Failed requests:        666
   (Connect: 0, Receive: 0, Length: 666, Exceptions: 0)
Write errors:           0
Non-2xx responses:      666
Total transferred:      1929330 bytes
HTML transferred:       638622 bytes
Requests per second:    688.43 [#/sec] (mean)
Time per request:       1452.577 [ms] (mean)
Time per request:       1.453 [ms] (mean, across all concurrent requests)
Transfer rate:          216.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   7.8      0      28
Processing:    35  702 1574.3     63    7239
Waiting:       26  701 1574.0     63    7239
Total:         48  705 1580.3     63    7255

Percentage of the requests served within a certain time (ms)
  50%     63
  66%     64
  75%     70
  80%     85
  90%   3048
  95%   4104
  98%   7247
  99%   7255
 100%   7255 (longest request)

测试结果说明:毫无改善的效果!!!!

  • 测试6 ---在测速5的基础上修改nigix的配置超时时间:
    修改后的配置文件为:
server {
    listen      10022;
    charset     utf-8;
    root        /data/www/KnowledgePay;
    server_name 127.0.0.1;

        location /favicon.ico {
                log_not_found off;
        access_log off;
        }

    location / {
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /data/www/KnowledgePay;
        uwsgi_param UWSGI_CHDIR /data/www/KnowledgePay;
        uwsgi_param UWSGI_SCRIPT main; # 对应main.py
        uwsgi_pass  127.0.0.1:10021;
        uwsgi_send_timeout         600;        # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
        uwsgi_connect_timeout      600;   # 指定连接到后端uWSGI的超时时间。
        uwsgi_read_timeout         600;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
        proxy_connect_timeout      6; #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout         12; #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout         12; #连接成功后,后端服务器响应时间(代理接收超时)
    }

    access_log  /data/logs/nginx/KnowledgePay_access.log  main;
}

测试结果1:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   7.618 seconds
Complete requests:      6000
Failed requests:        691
   (Connect: 0, Receive: 0, Length: 691, Exceptions: 0)
Write errors:           0
Non-2xx responses:      691
Total transferred:      1929455 bytes
HTML transferred:       640297 bytes
Requests per second:    787.61 [#/sec] (mean)
Time per request:       1269.672 [ms] (mean)
Time per request:       1.270 [ms] (mean, across all concurrent requests)
Transfer rate:          247.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.3      0      25
Processing:    32  839 1894.7     63    7241
Waiting:       22  839 1894.6     63    7241
Total:         47  842 1900.3     64    7256

Percentage of the requests served within a certain time (ms)
  50%     64
  66%     67
  75%     72
  80%     84
  90%   3048
  95%   7252
  98%   7254
  99%   7255
 100%   7256 (longest request)
[root@web1 ~]#

测试结果2:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   7.304 seconds
Complete requests:      6000
Failed requests:        609
   (Connect: 0, Receive: 0, Length: 609, Exceptions: 0)
Write errors:           0
Non-2xx responses:      609
Total transferred:      1929045 bytes
HTML transferred:       634803 bytes
Requests per second:    821.45 [#/sec] (mean)
Time per request:       1217.363 [ms] (mean)
Time per request:       1.217 [ms] (mean, across all concurrent requests)
Transfer rate:          257.91 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   7.8      0      27
Processing:    32  762 1796.6     64    7242
Waiting:       23  762 1796.5     63    7242
Total:         48  766 1802.3     64    7259

Percentage of the requests served within a certain time (ms)
  50%     64
  66%     65
  75%     66
  80%     69
  90%   3042
  95%   7249
  98%   7255
  99%   7258
 100%   7259 (longest request)
[root@web1 ~]# 

测试结果3:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   7.303 seconds
Complete requests:      6000
Failed requests:        771
   (Connect: 0, Receive: 0, Length: 771, Exceptions: 0)
Write errors:           0
Non-2xx responses:      771
Total transferred:      1929855 bytes
HTML transferred:       645657 bytes
Requests per second:    821.57 [#/sec] (mean)
Time per request:       1217.188 [ms] (mean)
Time per request:       1.217 [ms] (mean, across all concurrent requests)
Transfer rate:          258.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   8.1      0      28
Processing:    39  856 1854.2     65    7239
Waiting:       23  856 1854.1     65    7239
Total:         50  860 1860.2     65    7257

Percentage of the requests served within a certain time (ms)
  50%     65
  66%     70
  75%     75
  80%    509
  90%   3043
  95%   7250
  98%   7256
  99%   7256
 100%   7257 (longest request)

测试结果说明:

似乎 吞吐率有所提升一点点!!!
只是失败数还是依然那么高!
  • 测试7 ---一个会扑街的修改
    uwsgi修改:
[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10021
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
gevent = 2014
limit-as = 512
listen = 2014
reload-on-as = 256
reload-on-rss = 192
processes = 2
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true
enable-threads = true
threads = 4

image.png
image.png
image.png
image.png
image.png
  • 测试7 ---一个会扑街的修改---救活扑街的测试
    修改UWSGI
重要的修改:
添加:async = 4

具体配置:

[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10021
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
gevent = 2014
limit-as = 512
listen = 2014
reload-on-as = 256
reload-on-rss = 192
processes = 2
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true
enable-threads = true
threads = 4
async = 4

参考说明:


image.png
  • 测试8 ---所示例启动基于测试5:
    uwsgi --01
[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10021
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
gevent = 2014
limit-as = 512
listen = 100
reload-on-as = 256
reload-on-rss = 192
processes = 2
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true

uwsgi --02

[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10023
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
gevent = 2014
limit-as = 512
listen = 100
reload-on-as = 256
reload-on-rss = 192
processes = 2
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true
enable-threads = true
threads = 4
async = 4

nginx 配置:

upstream koows {
    server 127.0.0.1:10025 weight=1 max_fails=0 fail_timeout=5s;
    server 127.0.0.1:10024 weight=1 max_fails=0 fail_timeout=5s;
}


server {
    listen      10022;
    charset     utf-8;
    server_name 127.0.0.1;
    keepalive_timeout 4;

    location /favicon.ico {  
        log_not_found off;
        access_log off;
    } 
    
    location ~* ^/(api)/ {
        proxy_pass   http://koows;
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;   
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout      6; #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout         12; #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout         12; #连接成功后,后端服务器响应时间(代理接收超时)
    }

    access_log  off;
}

server {
    listen      10025;
    charset     utf-8;
    root        /data/www/KnowledgePay;
    server_name 127.0.0.1;

        location /favicon.ico {
                log_not_found off;
        access_log off;
        }

    location / {
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /data/www/KnowledgePay;
        uwsgi_param UWSGI_CHDIR /data/www/KnowledgePay;
        uwsgi_param UWSGI_SCRIPT main; # 对应main.py
        uwsgi_pass  127.0.0.1:10023;
        uwsgi_send_timeout         600;        # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
        uwsgi_connect_timeout      600;   # 指定连接到后端uWSGI的超时时间。
        uwsgi_read_timeout         600;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
        proxy_connect_timeout      6; #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout         12; #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout         12; #连接成功后,后端服务器响应时间(代理接收超时)
    }

    access_log  /data/logs/nginx/KnowledgePay_access.log  main;
}

server {
    listen      10024;
    charset     utf-8;
    root        /data/www/KnowledgePay;
    server_name 127.0.0.1;

        location /favicon.ico {
                log_not_found off;
        access_log off;
        }

    location / {
        include uwsgi_params;
        uwsgi_param UWSGI_PYHOME /data/www/KnowledgePay;
        uwsgi_param UWSGI_CHDIR /data/www/KnowledgePay;
        uwsgi_param UWSGI_SCRIPT main; # 对应main.py
        uwsgi_pass  127.0.0.1:10021;
        uwsgi_send_timeout         600;        # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
        uwsgi_connect_timeout      600;   # 指定连接到后端uWSGI的超时时间。
        uwsgi_read_timeout         600;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
        proxy_connect_timeout      6; #nginx跟后端服务器连接超时时间(代理连接超时)
        proxy_send_timeout         12; #后端服务器数据回传时间(代理发送超时)
        proxy_read_timeout         12; #连接成功后,后端服务器响应时间(代理接收超时)
    }

    access_log  /data/logs/nginx/KnowledgePay_access.log  main;
}

supervisor

[program:know]
command=/usr/bin/uwsgi /etc/uwsgi/know_test1.ini    ; supervisord将要执行的运行python服务的命令
directory=/data/www/KnowledgePay
stdout_logfile=/data/logs/supervisord/KnowledgePay_supervisord.log    ; supervisord当前这个test服务运行产生的日志存储路径,方便我们查看运行情况
socket-timeout=3
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT


[program:know2]
command=/usr/bin/uwsgi /etc/uwsgi/know_test2.ini    ; supervisord将要执行的运行python服务的命令
directory=/data/www/KnowledgePay
stdout_logfile=/data/logs/supervisord/KnowledgePay_supervisord.log    ; supervisord当前这个test服务运行产生的日志存储路径,方便我们查看运行情况
socket-timeout=3
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT

测试结果:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   7.361 seconds
Complete requests:      6000
Failed requests:        553
   (Connect: 0, Receive: 0, Length: 553, Exceptions: 0)
Write errors:           0
Non-2xx responses:      553
Total transferred:      1928765 bytes
HTML transferred:       631051 bytes
Requests per second:    815.08 [#/sec] (mean)
Time per request:       1226.873 [ms] (mean)
Time per request:       1.227 [ms] (mean, across all concurrent requests)
Transfer rate:          255.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5  10.2      0      34
Processing:     1  749 990.3    273    7285
Waiting:        1  749 990.2    270    7285
Total:          1  754 996.2    285    7310

Percentage of the requests served within a certain time (ms)
  50%    285
  66%   1028
  75%   1115
  80%   1144
  90%   2835
  95%   3098
  98%   3116
  99%   3130
 100%   7310 (longest request)

修改进程数之后:
processes = 8


image.png

修改进程数为4:
processes = 4

[root@web1 ~]# ab -n 6000 -c 1000 http://127.0.0.1:10022/api/v1/course/get/?page_num=7
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   5.841 seconds
Complete requests:      6000
Failed requests:        451
   (Connect: 0, Receive: 0, Length: 451, Exceptions: 0)
Write errors:           0
Non-2xx responses:      451
Total transferred:      1928255 bytes
HTML transferred:       624217 bytes
Requests per second:    1027.25 [#/sec] (mean)
Time per request:       973.474 [ms] (mean)
Time per request:       0.973 [ms] (mean, across all concurrent requests)
Transfer rate:          322.40 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   37 165.2      1    1019
Processing:     7  679 795.3    370    3454
Waiting:        7  679 795.5    369    3454
Total:          7  716 803.7    385    4115

Percentage of the requests served within a certain time (ms)
  50%    385
  66%    584
  75%   1088
  80%   1124
  90%   1453
  95%   3045
  98%   3120
  99%   3251
 100%   4115 (longest request)
[root@web1 ~]# 

对比单例和进程数为2:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 600 requests
Completed 1200 requests
Completed 1800 requests
Completed 2400 requests
Completed 3000 requests
Completed 3600 requests
Completed 4200 requests
Completed 4800 requests
Completed 5400 requests
Completed 6000 requests
Finished 6000 requests


Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            10022

Document Path:          /api/v1/course/get/?page_num=7
Document Length:        99 bytes

Concurrency Level:      1000
Time taken for tests:   4.402 seconds
Complete requests:      6000
Failed requests:        129
   (Connect: 0, Receive: 0, Length: 129, Exceptions: 0)
Write errors:           0
Non-2xx responses:      129
Total transferred:      1926645 bytes
HTML transferred:       602643 bytes
Requests per second:    1362.90 [#/sec] (mean)
Time per request:       733.730 [ms] (mean)
Time per request:       0.734 [ms] (mean, across all concurrent requests)
Transfer rate:          427.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    5   8.5      1      29
Processing:    31  511 594.0    262    3174
Waiting:        2  510 593.9    262    3173
Total:         31  516 596.4    265    3196

Percentage of the requests served within a certain time (ms)
  50%    265
  66%    345
  75%    470
  80%   1142
  90%   1299
  95%   1490
  98%   3032
  99%   3124
 100%   3196 (longest request)
[root@web1 ~]# 

关于uwgsi优化配置注意事项:

1:线程不能随意的开启,如果非要开启的话,记得需要开启异步处理项
如下
enable-threads = true
threads = 4
async = 4

2: <listen>2048</listen>项是提高并发的关键参数,参数过小的,并发大的时候请求就容易被丢弃处理
修改这个值的方式是:
可以通过 /proc/sys/net/somaxconn 和 /proc/sys/net/ipv4/tcp_max_syn_backlog 对TCP socket进行调整
修改系统参数

修改/etc/sysctl.conf文件,添加或者修改这几个参数值

对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了
net.core.somaxconn = 262144
​表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
net.ipv4.tcp_max_syn_backlog = 8192
网卡设备将请求放入队列的长度
net.core.netdev_max_backlog = 65536

修改完成之后要记得 sysctl -p 重新加载参数

3:增加(或减少)超时时间是重要的,修改socket监听队列大小也是如此。

4:考虑线程。如果你不需要线程,那么不要启用它们。

5:总是记住在生产环境上启用master进程。见 ProcessManagement 。

6:增加worker不是意味着“增加性能”,因此,基于你的应用的性质,为 workers 选项选择一个合适的值 (受IO限制,受CPU限制,IO等待……)
processes = 2 * cpucores 最佳processes 设置值

7:post-buffering的配置可以有效帮应用自动读取相关POST提交请求体信息

8:总是记住在生产环境上启用master进程。见 ProcessManagement 。

9:检查应用的内存使用 memory-report
10:建议不要以root用户运行uWSGI实例。如果非要你可以作为建议确保确保使用 uid 和 gid 选项来移除权限。

11:如果 (Linux) 服务器似乎有大量的idle状态的worker,但性能仍然不佳,需要看看 ip_conntrack_max 系统变量 (/proc/sys/net/ipv4/ip_conntrack_max) 的值,然后增加它以看看是否有帮助。

12:如果uwsgi日志中开始收到”invalid request block size”,这可能意味着你需要更大的缓存了。通过buffer-size 选项来增加它(最大值为65535)。

13:post-buffering
打开http body缓冲, 如果HTTP body的大小超过指定的限制,那么就保存到磁盘上.

14:post-buffering = 8192

命令行配置
–post-buffering 8192
:15:post-buffering-bufsize
设置在post缓冲时的 内部缓冲区大小(分配用于读取socket 流的chunk的大小)

16:post-buffering-bufsize 65536 将 分配64K的内存,作为 socket recv()函数的缓冲区. 对于128K的body, 需要两个循环/两次系统调用

17:async: 打开异步模式

18:在10s钟内没有活动就关闭连接:–socket-timeout 10 (默认是4s)

19:buffer-size
为分析uwsgi包准备的内部缓冲区大小,默认是4K
–buffer-size 32768

-s 指定unix socket路径或者tcp 的地址和端口号
-p worker进程数量
-d daemonize


------------------------2018年5月8日 00:01:26新增实时查看uwsgi连接情况-------------------------------------

需要在配置上添加对应的:

stats = 127.0.0.1:17178

其他配置方式:

--stats 127.0.0.1:1717
--stats /tmp/statsock
--stats :5050
--stats @foobar
# Any of the above socket types can also return stats using HTTP
--stats 127.0.0.1:1717 --stats-http

完整配置示例:

[uwsgi]
plugin = python3.6.4
socket = 127.0.0.1:10023
chdir = /data/www/KnowledgePay/
wsgi-file = /data/www/KnowledgePay/main.py
gevent = 2014
limit-as = 512
listen = 100
reload-on-as = 256
reload-on-rss = 192
processes = 4
max-requests = 2000
pythonpath = /data/www/KnowledgePay/
log-maxsize = 10000000
madisable-logging = true
master = true
vacuum = true
no-orphans = true
stats = 127.0.0.1:17178

然后查看命令:

[root@web1 ~]# uwsgi --connect-and-read 127.0.0.1:17177

使用uwsgitop查看uwsgi连接情况:

# pip install uwsgitop

查看命令:

[root@web1 ~]# uwsgitop 127.0.0.1:17177

显示结果:

uwsgi-2.0.15 - Tue May  8 00:07:18 2018 - req: 8713 - RPS: 0 - lq: 0 - tx: 2.1M
node: 127.0.0.1 - cwd: /data/www/KnowledgePay - uid: 0 - gid: 0 - masterpid: 18034
 WID    %       PID     REQ     RPS     EXC     SIG     STATUS  AVG     RSS     VSZ     TX      ReSpwn  HC      RunT    LastSpwn
 3      25.5    18203   2221    0       0       0       idle    2ms     36.4M   371.8M  542.2K  4       0       9804.818        00:06:34
 2      25.4    18201   2214    0       0       0       idle    2ms     36.4M   371.8M  540.5K  4       0       12883.989       00:06:34
 4      25.1    18202   2190    0       0       0       idle    1ms     36.4M   371.8M  534.7K  4       0       72851.409       00:06:34
 1      24.0    18204   2088    0       0       0       idle    2ms     36.4M   371.8M  509.8K  4       0       8589.721        00:06:34

参数说明:

WID : 工作索引(我们启动的多少个工作者的索引)ID
% : 使用占比
PID : 工作进程PID
REQ :自从上次(重新)生成后,工人执行的请求数
RPS : 每秒请求数
EXC :异常情况次数
SIG : 信号ID
STATUS :是繁忙状态还是空闲状态。
AVG : 平均请求时间
RSS : 工作者占用常驻内存(Worker RSS (Resident Set Size, see linux memory management))
VSZ :工作者占用虚拟内存(Worker VSZ (Virtual Memory Size, see linux memory management)
TX :工作发送的数据量(How much data was transmitted by the worker)
RunT :工作者运行时常(How long the worker has been running)

一般基础优化:
1: 禁用ipv6,加强系统安全性,并提高系统整体性能

2:调整linux最大文件打开数
查看/etc/security/limit.conf

在系统的/etc/rc.local添加相关内容

ulimit –SHn 65535

3:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355

推荐阅读更多精彩内容