• 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
    Content-Length: %d
    
    %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就稍微慢一些。

  • 相关阅读:
    十大最容易找工作的编程语言
    阿里云主机优惠购买后试用感受(送阿里云代金券)
    【目录】整理
    【git】关联远程分支
    【java】java基本用法记录
    【svn】本地文件夹同步到SVN
    【算法】Bert预训练源码阅读
    【算法】Attention is all you need
    【算法】Normalization
    【算法】BILSTM+CRF中的条件随机场
  • 原文地址:https://www.cnblogs.com/yafengabc/p/5431695.html
Copyright © 2020-2023  润新知