golang与node.js的http模块性能对比测试(go1)

前端之家收集整理的这篇文章主要介绍了golang与node.js的http模块性能对比测试(go1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

去年的时候,曾经简单对比了一下golang和nodejs的http模块的性能,见:golang与node.js的http对比测试

那时golang还没发布go1,http模块比nodejs差得很远。

go1出来已经有一段时间了,我知道go的http模块性能已经有比较大的提升,但是最近依然见到有人提起去年写的那篇文章,为避免产生对golang的误解,对于go1的最新测试结果如下。


测试是在Ubuntu 12.04 64位系统下进行的:

  1. qleelulu@nb:~$ uname -a
  2. Linux nb 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
  3. qleelulu@nb:~$ sudo dmidecode | grep cpu
  4. Socket Designation: cpu
  5. Version: Intel(R) Core(TM) i5 cpu M 480 @ 2.67GHz (注:双核4线程)

go的版本:

  1. qleelulu@nb:~$ go version
  2. go version go1

nodejs的版本:

  1. qleelulu@nb:~$ node -v
  2. v0.8.6

cpu测试

nodejs是单进程,只使用一个cpu,所以这里golang在代码中需要限制为只使用一个cpu

go的代码: (注:go代码都是使用 go build xxx.go 编译)

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "runtime"
  7. )
  8. func main() {
  9. // 限制为1个cpu
  10. runtime.GOMAXPROCS(1)
  11. http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) {
  12. fmt.Fprint(w,"Hello,world.")
  13. })
  14. log.Fatal(http.ListenAndServe(":8080", nil))
  15. }

golang请求结果:

  1. qleelulu@nb:~/sources/test$ curl -i http:127.0.0.1:8080/
  2. HTTP/1.1 200 OK
  3. Date: Sun,12 Aug 2012 07:21:00 GMT
  4. Transfer-Encoding: chunked
  5. Content-Type: text/plain; charset=utf-8
  6. Hello,world.
golang单cpu的ab测试结果:

  1. qleelulu@nb:~$ ab -c 100 -n 5000 http:127.0.0.1:8080/
  2. This is ApacheBench,Version 2.3 <$Revision: 655654 $>
  3. Server Software:
  4. Server Hostname: 127.0.0.1
  5. Server Port: 8080
  6. Document Path: /
  7. Document Length: 13 bytes
  8. Concurrency Level: 100
  9. Time taken for tests: 0.593 seconds
  10. Complete requests: 5000
  11. Failed requests: 0
  12. Write errors: 0
  13. Total transferred: 550000 bytes
  14. HTML transferred: 65000 bytes
  15. Requests per second: 8427.78 [#/sec] (mean)
  16. Time per request: 11.866 [ms] (mean)
  17. Time per request: 0.119 [ms] (mean,across all concurrent requests)
  18. Transfer rate: 905.33 [Kbytes/sec] received
  19. ...
共测试五次,五次结果分别如下:

Requests per second: 8427.78 [#/sec] (mean)
Requests per second: 7980.73 [#/sec] (mean)
Requests per second: 7509.63 [#/sec] (mean)
Requests per second: 8242.47 [#/sec] (mean)
Requests per second: 8898.19 [#/sec] (mean)

golang单cpu的webbench测试结果:

  1. qleelulu@nb:~$ webbench -t 30 -c 200 http:127.0.0.1:8080/
  2. Webbench - Simple Web Benchmark 1.5
  3. Copyright (c) Radim Kolar 1997-2004,GPL Open Source Software.
  4. Benchmarking: GET http:127.0.0.1:8080/
  5. 200 clients,running 30 sec.
  6. Speed=463124 pages/min,849060 bytes/sec.
  7. Requests: 231562 susceed,128); line-height:1.5!important">0 Failed.
共测试五次,结果如下:

Speed=463124 pages/min,849060 bytes/sec.
Speed=455322 pages/min,834757 bytes/sec.
Speed=461536 pages/min,846149 bytes/sec.
Speed=454798 pages/min,833803 bytes/sec.
Speed=468592 pages/min,859085 bytes/sec.

golang单CPU webbench测试时,CPU使用率如下:

nodejs 单CPU测试代码

  1. var http = require('http');
  2. http.createServer(function (req,res) {
  3. res.end('Hello,World.');
  4. }).listen(8080,'127.0.0.1');
  5. console.log('Server running at http://127.0.0.1:8080/');

nodejs请求结果:

  1. qleelulu@nb:~/$ curl -i http:45:23 GMT
  2. Connection: keep-alive
  3. Transfer-Encoding: chunked
  4. Hello,World.
为方便对比golang的返回结果,再贴一次golang的返回结果:

  1. golang请求结果:
  2. qleelulu@nb:~/sources/test$ curl -i http: nodejs的单cpu ab测试结果:

  3. 655654 $>
  4. Server Software:
  5. Server Hostname: 0.696 seconds
  6. Complete requests: 440000 bytes
  7. HTML transferred: 7185.91 [#/sec] (mean)
  8. Time per request: 13.916 [ms] (mean)
  9. Time per request: 0.139 [ms] (mean,128); line-height:1.5!important">617.54 [Kbytes/sec] received
@L_403_7@
  1. $ ab -c 127.0.0.1:8080/
  2. Requests per second: 14391.80 [#/sec] (mean)
  3. Requests per second: 14307.09 [#/sec] (mean)
  4. Requests per second: 14285.31 [#/sec] (mean)
  5. Requests per second: 15182.34 [#/sec] (mean)
  6. Requests per second: 14020.53 [#/sec] (mean)
golang的多CPU webbench测试结果,共测试5次,结果如下:

  1. $ webbench -t 127.0.0.1:8080/
  2. Speed=750418 pages/min,128); line-height:1.5!important">1375982 bytes/sec.
  3. Speed=750492 pages/min,128); line-height:1.5!important">1375909 bytes/sec.
  4. Speed=749128 pages/min,128); line-height:1.5!important">1373408 bytes/sec.
  5. Speed=749720 pages/min,128); line-height:1.5!important">1374486 bytes/sec.
  6. Speed=753080 pages/min,128); line-height:1.5!important">1380698 bytes/sec.
golang多CPU webbench测试时,CPU使用率如下:

nodejs测试代码
使用cluster,使用CPU数量减一个CPU

  1. var cluster = require('cluster');
  2. var http = require('http');
  3. var numcpus = require('os').cpus().length;
  4. if (cluster.isMaster) {
  5. Fork workers. fock num of cpuS - 1 works
  6. for (var i = 1; i < numcpus; i++) {
  7. cluster.fork();
  8. }
  9. cluster.on('exit',function(worker,code,signal) {
  10. console.log('worker ' + worker.process.pid + ' died');
  11. });
  12. cluster.on('fork',signal) {
  13. console.log('worker ' + worker.process.pid + ' is online');
  14. });
  15. } else {
  16. Workers can share any TCP connection
  17. In this case its a HTTP server
  18. http.createServer(function(req,res) {
  19. res.writeHead(200);
  20. res.end("hello world.");
  21. }).listen(8080);
  22. }
nodejs的多cpu ab测试结果,共测试5次,结果如下:

12694.96 [#/sec] (mean) Requests per second: 12606.68 [#/sec] (mean) Requests per second: 12712.68 [#/sec] (mean) Requests per second: 12851.16 [#/sec] (mean) Requests per second: 13005.08 [#/sec] (mean)
nodejs的多cpu webbench测试结果,共测试5次,结果如下:

791932 pages/min,128); line-height:1.5!important">1148301 bytes/sec. Speed=786516 pages/min,128); line-height:1.5!important">1140448 bytes/sec. Speed=797336 pages/min,128); line-height:1.5!important">1156140 bytes/sec. Speed=783182 pages/min,128); line-height:1.5!important">1135619 bytes/sec. Speed=785206 pages/min,128); line-height:1.5!important">1138551 bytes/sec.
nodejs多cpu webbench测试时,cpu使用率如下:

cpu测试结果来看,总体上来说go和nodejs的性能也还是差不多。
但从ab测试的结果来看,go的QPS要比nodejs高一点,
而从webbench测试结果来看,nodejs的QPS要比go高,但是go的bytes/sec又要比nodes高,可能是go的http返回头比nodes要稍微大一点的原因。

cpu测试时的cpu使用情况和单cpu测试时的情况差不多,
golang对4个cpu的占用率都在70%左右,
而node则是对3个cpu的占用在90%以上,还有一个cpu的占用率则要稍微低点。

从整体来看,golang和nodejs的http模块性能不相上下。 go1的性能还是比较喜人的,加上go语音的简洁,是个不错的选择(目前可能go的一些第三方模块都不是很成熟)。

猜你在找的Go相关文章