node php 在性能上差不多。模拟实际应用进行测试对比了。有点小失望。
发布于 1年前 作者 dreawu 1320 次浏览

最近研究 node 主要 express+mongoose 对mongodb进行写入测试。

先会生成自增的_id 然后插入另外一个集合。PHP也是这么做的。用ab测试过。性能上node没比php好。

node开启了多线程,异步写入mongodb。mongoose 开启了16个连接池。居然都比php差一点点。

是我没搞好?有高手一起研究的没?

14 回复

你这样测试,只能测试的mongodb 的性能吧…

而且node 那里来的多线程??是多进程吧…

实际项目,有大量的mongodb写入。这样的测试更接近实际项目。

你是怎么测试的?

node是按cpu数量开启多进程。

楼主测试的时候还是应该先找到整个系统的瓶颈,再去分析为什么用node没有性能上的提升…

这次node跟php都简单输出字符:fibo

结果还是一样。node没有比php好多少。甚至还差一点点。

node 代码如下:

var cluster = require(‘cluster’); var http = require(‘http’); var numCPUs = require(‘os’).cpus().length;

if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('online’, function(worker) { console.log(‘worker ' + worker.process.pid + ' start’); }); cluster.on('exit’, function(worker, code, signal) { console.log(‘worker ' + worker.process.pid + ' died’); }); } else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end(“fibo”); }).listen(8000); }

php代码如下:

<?php

echo 'fibo’;

?>

https://code.google.com/p/node-js-vs-apache-php-benchmark/wiki/Tests

你看下…??

这个是比较早的测试了,你按照他的方式在测试一次?

@youxiachai

测试了。结果一样。node比php 差。

看我日志: http://www.funfly.cn/archives/tech/nodejs/249.html

@dreawu

pi 计算那块做了没??

只是输出Hello world 部分,两者差距不大,我测试了好几次,拿最快的那次吧…其实这块两者差距不大…

Node.js

Server Software:        nodeJS
Server Hostname:        192.168.199.141
Server Port:            8080

Document Path:          /
Document Length:        18 bytes

Concurrency Level:      300
Time taken for tests:   0.175 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      154000 bytes
HTML transferred:       18000 bytes
Requests per second:    5713.96 [#/sec] (mean)
Time per request:       52.503 [ms] (mean)
Time per request:       0.175 [ms] (mean, across all concurrent requests)
Transfer rate:          859.33 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:    17   42  10.8     49      53
Waiting:       11   29  10.5     27      51
Total:         17   42  10.8     49      53

Percentage of the requests served within a certain time (ms)
  50%     49
  66%     50
  75%     50
  80%     51
  90%     51
  95%     52
  98%     52
  99%     53
 100%     53 (longest request)

PHP

Server Software:        Apache/2.4.4
Server Hostname:        192.168.199.141
Server Port:            80

Document Path:          /test.php
Document Length:        18 bytes

Concurrency Level:      300
Time taken for tests:   0.217 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      205000 bytes
HTML transferred:       18000 bytes
Requests per second:    4608.04 [#/sec] (mean)
Time per request:       65.104 [ms] (mean)
Time per request:       0.217 [ms] (mean, across all concurrent requests)
Transfer rate:          922.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:    20   53  11.4     58      68
Waiting:       20   42  10.0     41      67
Total:         20   53  11.4     58      68

Percentage of the requests served within a certain time (ms)
  50%     58
  66%     59
  75%     61
  80%     62
  90%     66
  95%     67
  98%     67
  99%     68
 100%     68 (longest request)

不过,Pi 计算部分,Node.js 完爆 PHP

Node.js

Server Software:        nodeJS
Server Hostname:        192.168.199.141
Server Port:            8080

Document Path:          /
Document Length:        23 bytes

Concurrency Level:      300
Time taken for tests:   4.659 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      159000 bytes
HTML transferred:       23000 bytes
Requests per second:    214.63 [#/sec] (mean)
Time per request:       1397.780 [ms] (mean)
Time per request:       4.659 [ms] (mean, across all concurrent requests)
Transfer rate:          33.33 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4  42.1      0     546
Processing:   529 1171 321.5   1076    1577
Waiting:      516 1016 265.8   1034    1576
Total:        529 1175 321.7   1076    1577

Percentage of the requests served within a certain time (ms)
  50%   1076
  66%   1523
  75%   1538
  80%   1543
  90%   1573
  95%   1575
  98%   1576
  99%   1576
 100%   1577 (longest request)

PHP 测了好几次,成绩最好的一次…

Server Software:        Apache/2.4.4
Server Hostname:        192.168.199.141
Server Port:            80

Document Path:          /pi.php
Document Length:        20 bytes

Concurrency Level:      300
Time taken for tests:   94.895 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      207000 bytes
HTML transferred:       20000 bytes
Requests per second:    10.54 [#/sec] (mean)
Time per request:       28468.628 [ms] (mean)
Time per request:       94.895 [ms] (mean, across all concurrent reques
Transfer rate:          2.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   2.0      0      64
Processing:   671 24366 7527.6  27773   30482
Waiting:      671 24364 7528.0  27770   30482
Total:        671 24366 7527.5  27773   30483

Percentage of the requests served within a certain time (ms)
  50%  27773
  66%  27910
  75%  28031
  80%  28241
  90%  29232
  95%  29734
  98%  29964
  99%  30149
 100%  30483 (longest request)

@dreawu 你测试一下PI 计算部分…Node.js 明显比PHP 强太多了…

@dreawu 我只有apache的…你,试一下,把pi 计算也测来看看…

@youxiachai 看了你的博客,目测也是win 下的吧…求你的lamp 优化配置攻略.怎么测试,Node.js 都是好过PHP…

最终用两台同网段的centos机器测试。NODEJS.确实比PHP快至少两倍以上。

我的测试,是生成自增_id写MONGODB 使用 express mongoose

#node单进程:

webbench -c500 -t60 http://192.168.9.76:3000/ Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76:3000/ 500 clients, running 60 sec.

Speed=7267 pages/min, 19257 bytes/sec. Requests: 7267 susceed, 0 failed.

#优化后的LNMP: webbench -c500 -t60 http://192.168.9.76/node/test_pi.php Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76/node/test_pi.php 500 clients, running 60 sec.

Speed=3936 pages/min, 14423 bytes/sec. Requests: 3936 susceed, 0 failed.

#node多进程:

webbench -c500 -t60 http://192.168.9.76:3000/ Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76:3000/ 500 clients, running 60 sec.

Speed=52772 pages/min, 139845 bytes/sec. Requests: 52772 susceed, 0 failed.

#node多进程写入mongodb:

webbench -c300 -t60 http://192.168.9.76:3000/ Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76:3000/ 300 clients, running 60 sec.

Speed=398412 pages/min, 1049170 bytes/sec. Requests: 398412 susceed, 0 failed.

#优化后的LNMP,PHP写入mongodb:

webbench -c300 -t60 http://192.168.9.76/node/test_mongodb.php Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76/node/test_mongodb.php 300 clients, running 60 sec.

Speed=181520 pages/min, 520357 bytes/sec. Requests: 181520 susceed, 0 failed.

#node单进程写入mongodb:

webbench -c300 -t60 http://192.168.9.76:3000/ Webbench - Simple Web Benchmark 1.5 Copyright © Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.9.76:3000/ 300 clients, running 60 sec.

Speed=64506 pages/min, 169865 bytes/sec. Requests: 64506 susceed, 0 failed.

回到顶部