ab测试 uwsgi遇到的问题

本文探讨了在高并发场景下UWSGI服务器的表现及优化方法。通过调整配置参数,如增加listen值,有效提升了服务器处理大量并发请求的能力。

1 请求并发数目较大时,接收到的数目小于发送的数目


1.1  

描述:uwsgi正常返回302跳转

ab -n 5000 -c 250 -g test.log "192.168.50.20:9033/Mapping.do?×××××××&as=1234567"
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.50.20 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Finished 5000 requests


Server Software:        
Server Hostname:        192.168.50.20
Server Port:            9033


Document Path:          /Mapping.do?bfd_nid=behe&as=1234567
Document Length:        0 bytes


Concurrency Level:      250
Time taken for tests:   12.107652 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Non-2xx responses:      4990
Total transferred:      2095800 bytes
HTML transferred:       0 bytes
Requests per second:    412.96 [#/sec] (mean)
Time per request:       605.383 [ms] (mean)
Time per request:       2.422 [ms] (mean, across all concurrent requests)
Transfer rate:          168.98 [Kbytes/sec] received


Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  42.5      0    3000
Processing:    18  466 1595.9    151   12083
Waiting:        0  446 1536.9    150   12083
Total:         18  467 1601.7    151   12106


Percentage of the requests served within a certain time (ms)
  50%    151
  66%    165
  75%    198
  80%    251
  90%    389
  95%    485
  98%   9008
  99%  12036
 100%  12106 (longest request)


按照上边的结果所示,发送了5000个请求,4990个非2xx相应(Non-2xx responses:      4990)

而查看uwsgi.log发现也只接收到4990个请求。


1.2   只要降低并发就不会出现这样的问题

ab -n 50000 -c 100 -g test.log "192.168.50.20:9033/Mapping.do?bfd_nid=behe&as=1234567"
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.50.20 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Finished 50000 requests




Server Software:        
Server Hostname:        192.168.50.20
Server Port:            9033


Document Path:          /Mapping.do?bfd_nid=behe&as=1234567
Document Length:        0 bytes


Concurrency Level:      100
Time taken for tests:   85.735172 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Non-2xx responses:      50000
Total transferred:      21000000 bytes
HTML transferred:       0 bytes
Requests per second:    583.19 [#/sec] (mean)
Time per request:       171.470 [ms] (mean)
Time per request:       1.715 [ms] (mean, across all concurrent requests)
Transfer rate:          239.19 [Kbytes/sec] received


Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0     186
Processing:     4  170 123.1    136    1592
Waiting:        4  170 123.0    136    1592
Total:          4  170 123.1    136    1592


Percentage of the requests served within a certain time (ms)
  50%    136
  66%    148
  75%    159
  80%    169
  90%    265
  95%    403
  98%    540
  99%    650
 100%   1592 (longest request)


1.3 调大listen参数解决问题。

当listen参数变大时,可以提高并发的数目。按道理增大processes数量和增大listen队列长度都可以增大并发能力。但是好像使用listen参数,效果比较显著。难道是后端处理速度太慢,需要缓存更多?

uwsgi.ini:

[uwsgi]
http=0.0.0.0:9033
master=true
pythonpath=../
module=api
processes=30
listen=512
daemonize=uwsgi.log
pidfile=uwsgi.pid
vacuum=true
harakiri=5
harakiri-verbose=true
buffer-size=16384


uwsgi.ini:

[uwsgi]
#http=192.168.40.11:9034
http=0.0.0.0:9034
master=true
pythonpath=../
module=breapi
processes=32
listen=512
daemonize=./logs/uwsgi.log
pidfile=uwsgi.pid
vacuum=true
harakiri=5
harakiri-verbose=true
buffer-size=16384
stats = $(HOSTNAME):1734


测试:

ab   -n 30000 -c 1000 -g test.log "192.168.40.11:9034/Mapping.do?bfd_nid=behe&as=1234567" 
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.40.11 (be patient)
Completed 3000 requests
Completed 6000 requests
Completed 9000 requests
Completed 12000 requests
Completed 15000 requests
Completed 18000 requests
Completed 21000 requests
Completed 24000 requests
Completed 27000 requests
Finished 30000 requests




Server Software:        
Server Hostname:        192.168.40.11
Server Port:            9034


Document Path:          /Mapping.do?bfd_nid=behe&as=1234567
Document Length:        0 bytes


Concurrency Level:      1000
Time taken for tests:   12.411582 seconds
Complete requests:      30000
Failed requests:        0
Write errors:           0
Non-2xx responses:      29994
Total transferred:      12597480 bytes
HTML transferred:       0 bytes
Requests per second:    2417.10 [#/sec] (mean)
Time per request:       413.719 [ms] (mean)
Time per request:       0.414 [ms] (mean, across all concurrent requests)
Transfer rate:          991.17 [Kbytes/sec] received


Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4  99.8      0    3000
Processing:    43  299 983.0    126    9234
Waiting:        0  297 976.9    125    9234
Total:         50  304 993.5    126   12070


Percentage of the requests served within a certain time (ms)
  50%    126
  66%    129
  75%    132
  80%    134
  90%    141
  95%    616
  98%   3222
  99%   3632
 100%  12070 (longest request)

### uWSGI 编码问题解决方案 #### 背景分析 uWSGI 是一种高性能的应用程序容器服务器,广泛用于部署 Python Web 应用程序。然而,在实际应用中可能会遇到编码问题,尤其是在处理多语言字符集时。这类问题通常表现为 `UnicodeEncodeError` 或其他类似的异常。 当直接使用 Python 和 uWSGI 不会出现此类错误时,可以推测这是由于环境变量或配置文件中的编码设置不当引起的[^1]。 --- #### 常见原因及解决方法 ##### 1. **环境变量未正确设置** 如果系统的默认编码不是 UTF-8,则可能导致无法正确解析某些非 ASCII 字符串。可以通过在启动命令中显式指定环境变量来解决问题: ```bash uwsgi --env LANG=en_US.UTF-8 --env LC_ALL=en_US.UTF-8 ``` 上述命令通过 `-env` 参数设置了运行时的语言和区域选项,从而强制 uWSGI 使用 UTF-8 编码[^4]。 --- ##### 2. **ini 文件配置不完善** 在 `uwsgi.ini` 中也可以通过添加特定的参数来调整编码行为。以下是推荐的关键配置项及其作用: - `lang`: 设置应用程序使用的语言,默认可能为 C 的本地化版本。 - `chdir`: 切换工作目录至项目根路径。 - `wsgi-file`: 指定 WSGI 入口文件的位置。 - `master`: 启动主进程模式以提高稳定性。 - `processes`: 定义并发的工作线程数。 - `threads`: 单个工作进程中启用多个线程支持。 完整的示例配置如下所示: ```ini [uwsgi] module = wsgi:app master = true processes = 4 threads = 2 http-timeout = 30 socket = /tmp/uwsgi.sock chmod-socket = 660 vacuum = true die-on-term = true enable-threads = true lang = en_US.UTF-8 ``` 注意:这里的 `lang` 参数同样指定了全局编码方式[^2]。 --- ##### 3. **Python 代码内部的兼容性问题** 即使外部环境已经适配好,但如果 Python 程序本身存在硬编码字符串或其他潜在缺陷,仍然会引发冲突。建议遵循以下最佳实践: - 明确声明源文件采用 UTF-8 编码; - 对输入数据进行严格校验并转换为目标格式; 例如,在 Flask 或 Django 中初始化阶段加入以下片段可有效规避风险: ```python import sys reload(sys) sys.setdefaultencoding('utf-8') # 强制设定默认编码为 utf-8 (仅适用于 Python 2.x) ``` 对于现代 Python 版本(即 >=3),无需额外操作因为其内置支持 Unicode[^3]。 --- #### 总结 综上所述,针对 uWSGI 出现的编码难题可以从以下几个方面入手排查与修正——确认操作系统层面的基础属性是否匹配预期需求;优化服务端加载脚本里的各项指令组合;最后审视业务逻辑里是否存在遗漏之处待改进。只要按照以上指导方针逐一落实到位,绝大多数场景下的乱码现象都能迎刃而解。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值