关于web.py + gevent + nginx的那些事儿

一直都是用的web.py,因为简单。
也用gevent,因为效率。

最近要布置一个api,直接web.py,写的是快,速度似乎有些跟不上了

C:\Users\iyuan>ab -n 10000 -c 3 http://172.16.0.98:8080/?t=32424&x=982/
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.0.98 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software: CherryPy/3.1.2
Server Hostname: 172.16.0.98
Server Port: 8080

Document Path: /?t=32424
Document Length: 4 bytes

Concurrency Level: 3
Time taken for tests: 26.884000 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 960000 bytes
HTML transferred: 40000 bytes
Requests per second: 371.97 [#/sec] (mean)
Time per request: 8.065 [ms] (mean)
Time per request: 2.688 [ms] (mean, across all concurrent requests)
Transfer rate: 34.85 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.9 2 38
Processing: 2 5 3.7 4 42
Waiting: 1 4 3.3 3 42
Total: 3 7 4.9 5 49

Percentage of the requests served within a certain time (ms)
50% 5
66% 7
75% 10
80% 11
90% 14
95% 18
98% 22
99% 24
100% 49 (longest request)


加上了gevent,对一些可能出现等待的东东封装一下,速度看似有好转:
C:\Users\iyuan>ab -n 10000 -c 3 http://172.16.0.98:8080/
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.0.98 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software: gevent/0.13
Server Hostname: 172.16.0.98
Server Port: 8080

Document Path: /
Document Length: 4 bytes

Concurrency Level: 3
Time taken for tests: 23.223000 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1380000 bytes
HTML transferred: 40000 bytes
Requests per second: 430.61 [#/sec] (mean)
Time per request: 6.967 [ms] (mean)
Time per request: 2.322 [ms] (mean, across all concurrent requests)
Transfer rate: 58.00 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.8 1 15
Processing: 2 5 1.6 6 22
Waiting: 2 5 1.5 5 22
Total: 3 6 1.8 7 26

Percentage of the requests served within a certain time (ms)
50% 7
66% 7
75% 7
80% 7
90% 8
95% 9
98% 12
99% 14
100% 26 (longest request)


记得看过一个神帖,关于各种python HttpServer的测评,干脆也简单来个,下面的是纯gevent的:

C:\Users\iyuan>ab -n 10000 -c 3 http://172.16.0.98:8080/?t=32424&x=982/
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.0.98 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software: gevent/0.13
Server Hostname: 172.16.0.98
Server Port: 8080

Document Path: /?t=32424
Document Length: 0 bytes

Concurrency Level: 3
Time taken for tests: 14.688000 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1140000 bytes
HTML transferred: 0 bytes
Requests per second: 680.83 [#/sec] (mean)
Time per request: 4.406 [ms] (mean)
Time per request: 1.469 [ms] (mean, across all concurrent requests)
Transfer rate: 75.78 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.5 1 15
Processing: 1 3 0.8 3 17
Waiting: 1 2 1.0 2 17
Total: 2 4 0.9 4 21

Percentage of the requests served within a certain time (ms)
50% 4
66% 4
75% 5
80% 5
90% 5
95% 5
98% 6
99% 7
100% 21 (longest request)


当然,结果比较着看还不错,不过一如既往的与测评贴差的很远

好吧,上通常用的配置,fast-cgi起5个web.py版的,由nginx接上,结果与单gevent差不多:
C:\Users\iyuan>ab -n 10000 -c 3 http://webrpc-test.ex-sandbox.com/?t=32424&x=982

This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking webrpc-test.ex-sandbox.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software: nginx/0.6.37
Server Hostname: webrpc-test.ex-sandbox.com
Server Port: 80

Document Path: /?t=32424
Document Length: 4 bytes

Concurrency Level: 3
Time taken for tests: 15.892000 seconds
Complete requests: 10000
Failed requests: 361
(Connect: 0, Length: 361, Exceptions: 0)
Write errors: 0
Non-2xx responses: 361
Total transferred: 1032021 bytes
HTML transferred: 46137 bytes
Requests per second: 629.25 [#/sec] (mean)
Time per request: 4.768 [ms] (mean)
Time per request: 1.589 [ms] (mean, across all concurrent requests)
Transfer rate: 63.37 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.7 1 11
Processing: 1 3 1.8 3 137
Waiting: 1 2 1.9 3 137
Total: 2 4 2.0 4 139

Percentage of the requests served within a certain time (ms)
50% 4
66% 5
75% 5
80% 5
90% 5
95% 7
98% 10
99% 11
100% 139 (longest request)


对照着gevent似乎也比较可以接受,如果没有丢包的话。好吧,如此严重的丢包有些不太让人接受。

考虑到url,访问权限之类的问题,nginx是必须的。
突然有点厌烦这一堆测试数据,特别是没有成就感的状态下。不过本着有始有终的原则,最后一组也做了吧,nginx proxy gevent,起来两个独立gevent,由nginx 飘着,
速度时快时慢,不过倒是没丢数据,rps也在400+。


综合考虑,采用 nginx 代理 gevent模式,速度可以接受,稳定也是关键啊。
不过 web.py + gevent.queue + fcgi + nginx也是不错的选择,不过这个适用于非及时性的server

有点凌乱了,就这么着吧
内容概要:本文介绍了一个关于超声谐波成像中幅度调制聚焦超声所引起全场位移和应变的分析模型,并提供了基于Matlab的代码实现。该模型旨在精确模拟和分析在超声谐波成像过程中,由于幅度调制聚焦超声作用于生物组织时产生的力学效应,包括全场的位移与应变分布,从而为医学成像和治疗提供理论支持和技术超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)手段。文中详细阐述了模型构建的物理基础、数学推导过程以及Matlab仿真流程,具有较强的理论深度与工程应用价值。; 适合人群:具备一定声学、生物医学工程或力学背景,熟悉Matlab编程,从事医学成像、超声技术或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中的力学建模与仿真分析;②支持高强度聚焦超声(HIFU)治疗中的组织响应预测;③作为教学案例帮助理解超声与组织相互作用的物理机制;④为相关科研项目提供可复用的Matlab代码框架。; 阅读建议:建议读者结合超声物理和连续介质力学基础知识进行学习,重点关注模型假设、偏微分方程的数值求解方法及Matlab实现细节,建议动手运行并修改代码以加深理解,同时可拓展应用于其他超声成像或治疗场景的仿真研究。
### 关于PAT Basic Level Practice的测试点及题目解析 #### 题目难度分级 PAT(Programming Ability Test)是由浙江大学举办的计算机程序设计能力考试,分为不同级别。其中乙级即Basic Level主要面向初学者,考察基本编程技能[^1]。 #### 测试点特点 对于PAT Basic Level中的某些特定题目而言,其测试点设置较为严格。例如,在处理字符串匹配类问题时,需要注意算法逻辑中何时应当终止循环以防止不必要的重复计算;而在涉及数值运算的问题里,则可能因为边界条件而增加复杂度[^3]。 #### 编程语言的选择影响 值得注意的是,尽管大部分简单题目可以作为学习某种新语言的良好实践材料,但在实际操作过程中可能会遇到由于所选语言特性而导致难以通过全部测试点的情况。比如Java在面对部分效率敏感型试题时表现不佳,这可能是由于该语言本身的执行速度相对较慢以及内存管理方式等因素造成的。因此有时不得不转而采用其他更适合解决此类问题的语言版本来完成解答[^2]。 ```cpp #include<bits/stdc++.h> using namespace std; int a[100000]; int c=1; void getPrime(){ int flag=0; for(int i=2;i<105000;i++){ flag=1; for(int j=2;j<=sqrt(i);j++){ if(i%j==0){ flag=0; break; } } if(flag==1) a[c++]=i; } } int main(){ int m,n,i,t=1; scanf("%d %d",&m,&n); getPrime(); for(i=m;i<=n;i++){ if(t%10==1){ printf("%d",a[i]); t++; }else{ printf(" %d",a[i]); t++; } if((t-1)%10==0) printf("\n"); } return 0; } ``` 上述C++代码展示了如何实现一个简单的质数打印功能,并且针对输出格式进行了特殊处理以满足特定要求。这段代码很好地体现了编写高效解决方案的重要性,尤其是在应对像PAT这样的在线评测系统时[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值