看到jsconf2010.pdf写的关于buffer对象和string字符串相加的对比图表。发现buffer对象响应的变化很大,眼见为实,因此证实一下:
准备:
apache ab模拟并发,编写两份代码
os: debian 6.0
cpu: Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
mem: 4G
代码:
var http = require('http'), string = '', buffer=''; for(i = 0; i < 16*1024; i++) { string +='d'; } var server = http.createServer( function(req, res) { res.writeHead(200); res.end(string, 'ascii'); } ); server.listen(1339, '127.0.0.1'); buffer = new Buffer(16*1024); for(i=0; i < buffer.length; i++) { buffer[i] = 100; } var server2 = http.createServer( function(req, res) { res.writeHead(200); res.end(buffer); } ); server2.listen(1340, '127.0.0.1'); console.log('running');
说明:定义两个监听,实现不同的字符串拼接,并监听不同的端口,方便测试。
运行:
/usr/mytools/httpd/bin/ab -c 300 -n 10000 http://127.0.0.1:1339/usr/mytools/httpd/bin/ab -c 300 -n 10000 http://127.0.0.1:1340结果:
madding@wmmad:~$ /usr/mytools/httpd/bin/ab -c 300 -n 10000 http://127.0.0.1:1339/ This is ApacheBench, Version 2.3 <$Revision: 655654 {1}gt; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (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 Completed 10000 requests Finished 10000 requests Server Software: Server Hostname: 127.0.0.1 Server Port: 1339 Document Path: / Document Length: 16384 bytes Concurrency Level: 300 Time taken for tests: 3.602 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 164220000 bytes HTML transferred: 163840000 bytes Requests per second: 2776.02 [#/sec] (mean) Time per request: 108.068 [ms] (mean) Time per request: 0.360 [ms] (mean, across all concurrent requests) Transfer rate: 44519.36 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 36 326.6 0 3008 Processing: 1 47 29.8 46 570 Waiting: 1 47 29.8 46 570 Total: 1 83 328.9 46 3094 Percentage of the requests served within a certain time (ms) 50% 46 66% 60 75% 68 80% 72 90% 81 95% 90 98% 141 99% 3033 100% 3094 (longest request) madding@wmmad:~$ /usr/mytools/httpd/bin/ab -c 300 -n 10000 http://127.0.0.1:1340/ This is ApacheBench, Version 2.3 <$Revision: 655654 {1}gt; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (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 Completed 10000 requests Finished 10000 requests Server Software: Server Hostname: 127.0.0.1 Server Port: 1340 Document Path: / Document Length: 16384 bytes Concurrency Level: 300 Time taken for tests: 1.167 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 164220000 bytes HTML transferred: 163840000 bytes Requests per second: 8565.63 [#/sec] (mean) Time per request: 35.024 [ms] (mean) Time per request: 0.117 [ms] (mean, across all concurrent requests) Transfer rate: 137368.00 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.9 0 9 Processing: 0 16 12.5 15 611 Waiting: 0 15 12.4 15 610 Total: 1 16 12.5 15 612 Percentage of the requests served within a certain time (ms) 50% 15 66% 19 75% 21 80% 23 90% 26 95% 30 98% 33 99% 37 100% 612 (longest request)说明:在300个并发,10000个请求下,采用buffer方式构造对象的性能优化是和之前的3倍以上。