web编程速度大比拼(nodejs go python)(非专业对比)
C10K问题的解决,涌现出一大批新框架,或者新语言,那么问题来了:到底谁最快呢?非专业程序猿来个非专业对比。
比较程序:输出Hello World!
测试程序:siege –c 100 –r 100 –b
例子包括:
1.go用http模块实现的helloworld
2.go用martini微框架实现的Helloworld
3.python3 python2 pypy分别用gevent server tornado实现的Hello world
4.python3 python2 pypy分别用微框架bottle+gevent实现的Hello world
5.NodeJS纯JS实现的Helloworld
6.NodeJS用express框架实现的Helloworld
测试平台:
公司老旧的奔腾平台 Pentium(R) Dual-Core CPU E6700 @ 3.20GHz
内存2GB(够弱了吧)
先来宇宙最快的GO的测试:
1 package main
2
3 import (
4 "fmt"
5 "net/http"
6 )
7
8 func sayhelloName(w http.ResponseWriter, r *http.Request){
9 fmt.Fprintf(w, "hello world!")
10 }
11
12 func main() {
13 http.HandleFunc("/", sayhelloName)
14 http.ListenAndServe(":9090", nil)
15 }
连续测试5次,成绩大体如下:
1 Transactions: 10000 hits
2 Availability: 100.00 %
3 Elapsed time: 4.11 secs
4 Data transferred: 0.11 MB
5 Response time: 0.03 secs
6 Transaction rate: 2433.09 trans/sec
7 Throughput: 0.03 MB/sec
8 Concurrency: 79.76
9 Successful transactions: 10000
10 Failed transactions: 0
11 Longest transaction: 0.20
12 Shortest transaction: 0.00
4.11秒,不错的成绩
再看NodeJS的例子:
1 var http = require("http");
2 http.createServer(function(request, response) {
3 response.writeHead(200, {"Content-Type": "text/plain"});
4 response.write("Hello World!");
5 response.end();
6 }).listen(8000);
7 console.log("nodejs start listen 8888 port!");
8
测试结果如下:
1 Transactions: 10000 hits
2 Availability: 100.00 %
3 Elapsed time: 5.00 secs
4 Data transferred: 0.11 MB
5 Response time: 0.04 secs
6 Transaction rate: 2000.00 trans/sec
7 Throughput: 0.02 MB/sec
8 Concurrency: 86.84
9 Successful transactions: 10000
10 Failed transactions: 0
11 Longest transaction: 0.17
12 Shortest transaction: 0.00
5秒,比Go稍微慢一点
接下来是Python,由于python自带的wsgiref服务器,只是一个参考实现,性能很差,所以这里选用了两个性能不错的WSGI服务器gevent、tornado
gevent代码如下:
1 #!/usr/bin/python
2 from gevent import pywsgi
3
4 def hello_world(env, start_response):
5 if env['PATH_INFO'] == '/':
6 start_response('200 OK', [('Content-Type', 'text/html')])
7 return ["hello world"]
8
9 print 'Serving on https://127.0.0.1:8000'
10 server = pywsgi.WSGIServer(('0.0.0.0', 8000), hello_world )
11 server.serve_forever()
12
tornado的代码如下:
1 from tornado import httpserver
2 from tornado import ioloop
3 def handle_request(request):
4 if request.uri=='/':
5 message = b"Hello World!"
6 request.write(b"HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (
7 len(message), message))
8 request.finish()
9
10 http_server = httpserver.HTTPServer(handle_request)
11 http_server.bind(8888)
12 http_server.start()
13 ioloop.IOLoop.instance().start()
14
由于python的例子要分别在python2 python3 pypy下跑,结果太多,我这里只给结果:
gevent:
python2:5.8秒,python3:7.5秒,pypy:4.8秒
有意思的是:pypy第一次跑用了6.8秒,第二次以后全是4.8秒(感觉原因是第一次由于jit浪费了一点时间)贴出某次pypy的成绩:
1 Transactions: 10000 hits
2 Availability: 100.00 %
3 Elapsed time: 4.77 secs
4 Data transferred: 0.10 MB
5 Response time: 0.04 secs
6 Transaction rate: 2096.44 trans/sec
7 Throughput: 0.02 MB/sec
8 Concurrency: 90.38
9 Successful transactions: 10000
10 Failed transactions: 0
11 Longest transaction: 0.13
12 Shortest transaction: 0.00
13
接下来是tornado:
python2:9.05秒,python3:8.6秒,pypy:5.95秒
同样:pypy第一次执行的时间为9.45秒,以后的每次只执行时间为5.9秒多一些
可以看出,pypy 与go nodejs性能相当,其中go最快为4.11秒,pypy+gevent与nodejs性能差不多,pypy稍好一点,pypy+tornado则稍慢于nodejs。
2。框架篇:
从上边例子可以看到,纯代码写起来还是比较麻烦的,一般我们都是用框架来写web,我选了几个轻量级的框架来输出helloworld:
go+martini
1 package main
2
3 import "github.com/codegangsta/martini"
4
5 func main() {
6 m := martini.Classic()
7 m.Get("/", func() string {
8 return "Hello world!"
9 })
10 m.Run()
11 }
12
运行时间为:
1 Transactions: 10000 hits
2 Availability: 100.00 %
3 Elapsed time: 4.69 secs
4 Data transferred: 0.11 MB
5 Response time: 0.04 secs
6 Transaction rate: 2132.20 trans/sec
7 Throughput: 0.02 MB/sec
8 Concurrency: 90.23
9 Successful transactions: 10000
10 Failed transactions: 0
11 Longest transaction: 0.17
12 Shortest transaction: 0.00
13
nodejs+express:
1 var express = require('express')
2 var app = express()
3
4 app.get('/', function (req, res) {
5 res.send('Hello World')
6 })
7
8 app.listen(3000)
用时:
1 Transactions: 10000 hits
2 Availability: 100.00 %
3 Elapsed time: 5.90 secs
4 Data transferred: 0.10 MB
5 Response time: 0.06 secs
6 Transaction rate: 1694.92 trans/sec
7 Throughput: 0.02 MB/sec
8 Concurrency: 96.44
9 Successful transactions: 10000
10 Failed transactions: 0
11 Longest transaction: 0.13
12 Shortest transaction: 0.01
13
python gevent+bottle:
1 from gevent import monkey
2 monkey.patch_all()
3 from bottle import run,get
4
5 @get("/")
6 def index():
7 return "Hello world!"
8
9 run(server='gevent')
10
用时:python2 10.05秒,python3:12.95秒,pypy:5.85秒
python tornado:
1 import tornado.httpserver
2 import tornado.ioloop
3 import tornado.web
4
5 class IndexHandler(tornado.web.RequestHandler):
6 def get(self):
7 self.write('Hello World!')
8
9 if __name__ == "__main__":
10 app = tornado.web.Application(handlers=[(r"/",IndexHandler)])
11 http_server = tornado.httpserver.HTTPServer(app)
12 http_server.listen(8000)
13 tornado.ioloop.IOLoop.instance().start()
14
用时:python2 11.85秒,python3:11.79秒,pypy:6.65秒
总结:可以看到,python在开启jit技术的pypy上web响应速度已经略优于nodejs,跟golang还有一定差距,但在同一数量级,标准python就稍微慢一些。
web编程速度大比拼(nodejs go python)(非专业对比)的更多相关文章
- python笔记之编程风格大比拼
python笔记之编程风格大比拼 虽然我的python age并不高,但我仍然愿意将我遇到的或者我写的有趣的python程序和大家一块分享,下面是我找到的一篇关于各类python程序员的编程风格的比较 ...
- python web编程-概念预热篇
互联网正在引发一场革命??不喜欢看概念的跳过,注意这里仅仅是一些从python核心编程一书的摘抄 这正是最激动人心的一部分了,web编程 Web 客户端和服务器端交互使用的“语言”,Web 交互的标准 ...
- Python 四大主流 Web 编程框架
Python 四大主流 Web 编程框架 目前Python的网络编程框架已经多达几十个,逐个学习它们显然不现实.但这些框架在系统架构和运行环境中有很多共通之处,本文带领读者学习基于Python网络框架 ...
- 系列文章--Python Web编程
我从网上找到了其他园友的文章,很不错,留着自己学习学习. Python Web编程(一)Python Web编程(二)Python Web编程(三)Python Web编程(四)Python Web编 ...
- python web编程 创建一个web服务器
这里就介绍几个底层的用于创建web服务器的模块,其中最为主要的就是BaseHTTPServer,很多框架和web服务器就是在他们的基础上创建的 基础知识 要建立一个Web 服务,一个基本的服务器和一个 ...
- python web编程-web客户端编程
web应用也遵循客户服务器架构 浏览器就是一个基本的web客户端,她实现两个基本功能,一个是从web服务器下载文件,另一个是渲染文件 同浏览器具有类似功能以实现简单的web客户端的模块式urllib以 ...
- python 教程 第十八章、 Web编程
第十八章. Web编程 import urllib2 LOGIN = 'jin' PASSWD = 'Welcome' URL = 'https://tlv-tools-qc:8443/qcbin/s ...
- Python web编程 初识TCP UDP
Python网络编程之初识TCP,UDP 这篇文章是读了<Python核心编程>第三版(Core Python Applications)的第二章网络编程后的自我总结. 如果有不到位或者错 ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
随机推荐
- viewPager使用时加载数据时显示IllegalStateException异常,解决不了。。。。
从newsPager中得到newsDetailTitles标题的详细内容,这是通过构造器传过来的.打印日志78行能打印,45行打印出来共size是12.但是程序出现了异常java.lang.Illeg ...
- 蓝桥杯D1
整数因式分解的问题.了解了一下筛法计算素数的算法.之前都是直接跑的sqrt(n)... 上方的genPrime()即筛法.大致意思是开一个标识数组,通过循环,下标为合数的位置置为false. #inc ...
- CRM后期修改实体,新增货币类型字段 需要注意的问题
货币类型字段新增 需要处理历史数据 否则编辑会报错 提示如果货币字段中存在值,则需要指定币种,请选择币种,然后重试 编辑时货币字段不显示¥符号.新增正常.第一次编辑提示错误保存后再编辑也正常.不是JS ...
- mvc 设置默认页技巧
打开网址:http://xxxx.com自动跳转==>http://xxx.com/home/index 设置route入口: routes.MapRoute( name: "Main ...
- iOS百度推送的基本使用
一.iOS证书指导 在 iOS App 中加入消息推送功能时,必须要在 Apple 的开发者中心网站上申请推送证书,每一个 App 需要申请两个证书,一个在开发测试环境下使用,另一个用于上线到 App ...
- response.getWriter().write()与out.print()的区别 (转)
来自:http://www.cnblogs.com/zhwl/p/3623688.html 1.首先介绍write()和print()方法的区别: (1).write():仅支持输出字符类型数据,字 ...
- poj1995-快速幂取模
#include<iostream> #define LL long long using namespace std; //快速幂算法 LL pow(LL a,LL b,int m){ ...
- c++中类模版中的static数据成员的定义
这个有点绕. 如下: template <typename T> class A{ ......... static std::allocate<T> alloc_; }; t ...
- HDU 2685 I won't tell you this is about number theory
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685 题意:求gcd(a^m - 1, a^n - 1) mod k 思路:gcd(a^m - 1, ...
- 进程间通信机制IPC
进程通信是指进程之间的信息交换.PV操作是低级通信方式,例如信号量,主要是进程间以及同一进程内不同线程之间的同步手段.髙级通信方式是指以较高的效率传输大量数据的通信方式.高级通信方法主要有以下三个类. ...