疑惑?!node.js搭建静态文件[缓存]服务器与Apache性能比较
发布于 6 年前 作者 wangjiakai 6036 次浏览 最后一次编辑是 4 年前

疑惑?!node.js搭建静态文件[缓存]服务器与Apache性能比较

关于始终访问一个文件的时候,并不是每次都从磁盘中读取的问题,希望大家推荐一个随机访问的性能测试评估工具:) 这里的性能测试工具是Apache自带的ab工具 ab -c 50 -n 10000 127.0.0.1:8080/a.jpg Apache是默认配置,Node代码见文章末尾 50线程同时发送10000个请求,80%的请求响应时间为:

Apache	176ms
Node	145ms
Node+cache	131ms
Node+Redis	210ms

Apache的响应速度作为参考! Node+cache速度最快,是可以理解的。因为从磁盘读取要比直接从内存读取速度慢。 疑惑是:为什么Node+Redis要比纯Node速度慢呢?并且是最慢!?

详细数据

Apache

Server Software:        Apache/2.0.65
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /a.jpg
Document Length:        191970 bytes

Concurrency Level:      50
Time taken for tests:   32.269846 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1922230000 bytes
HTML transferred:       1919700000 bytes
Requests per second:    309.89 [#/sec] (mean)
Time per request:       161.349 [ms] (mean)
Time per request:       3.227 [ms] (mean, across all concurrent requests)
Transfer rate:          58171.24 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.8      1      10
Processing:   122  159  27.6    153     290
Waiting:       30   58  15.8     57     137
Total:        123  160  27.7    153     290
WARNING: The median and mean for the initial connection time are not within a no
rmal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%    153
  66%    163
  75%    171
  80%    176
  90%    196
  95%    216
  98%    246
  99%    267
 100%    290 (longest request)

Node

Server Software:        Node_v0.10.25
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /a.jpg
Document Length:        191970 bytes

Concurrency Level:      50
Time taken for tests:   25.898481 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1920940000 bytes
HTML transferred:       1919700000 bytes
Requests per second:    386.12 [#/sec] (mean)
Time per request:       129.492 [ms] (mean)
Time per request:       2.590 [ms] (mean, across all concurrent requests)
Transfer rate:          72433.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.0      1      15
Processing:    26  127  32.2    117     355
Waiting:        2   15  14.1     11     150
Total:         27  128  32.4    118     356

Percentage of the requests served within a certain time (ms)
  50%    118
  66%    128
  75%    137
  80%    145
  90%    172
  95%    192
  98%    222
  99%    253
 100%    356 (longest request)

Node+cache

Server Software:        Node_v0.10.25
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /a.jpg
Document Length:        191970 bytes

Concurrency Level:      50
Time taken for tests:   24.616408 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1920940000 bytes
HTML transferred:       1919700000 bytes
Requests per second:    406.23 [#/sec] (mean)
Time per request:       123.082 [ms] (mean)
Time per request:       2.462 [ms] (mean, across all concurrent requests)
Transfer rate:          76205.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.0      1      12
Processing:    32  121  27.4    115     244
Waiting:        1    8   6.0      7      95
Total:         33  122  27.6    116     252

Percentage of the requests served within a certain time (ms)
  50%    116
  66%    122
  75%    127
  80%    131
  90%    153
  95%    190
  98%    215
  99%    226
 100%    252 (longest request)

Node+Redis

Server Software:        Node_v0.10.25
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /a.jpg
Document Length:        191970 bytes

Concurrency Level:      50
Time taken for tests:   34.663983 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1920940000 bytes
HTML transferred:       1919700000 bytes
Requests per second:    288.48 [#/sec] (mean)
Time per request:       173.320 [ms] (mean)
Time per request:       3.466 [ms] (mean, across all concurrent requests)
Transfer rate:          54117.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.5      1      27
Processing:    34  170  46.8    163     379
Waiting:        4   62  60.6     34     271
Total:         35  171  46.8    164     380

Percentage of the requests served within a certain time (ms)
  50%    164
  66%    187
  75%    202
  80%    210
  90%    233
  95%    249
  98%    276
  99%    317
 100%    380 (longest request)

Node.js代码

var http = require('http'),
	fs = require('fs');

var redis = require("redis"),
	client = redis.createClient(6379, '127.0.0.1', {/*parser: 'javascript', */return_buffers: true});

server = http.createServer(function(req, res) {
	res.setHeader("Server", "Node_v0.10.25");
	var realPath = './assets/a.jpg',
		contentType = 'image/jpeg';
		res.writeHead(200, {'Content-Type' : contentType});

			/* Node start */
//			var raw = fs.createReadStream(realPath);
//			raw.pipe(res);
			/* Node end */

		/* Node+cache start */
//		if(undefined === server.cache[realPath]) {
//			fs.readFile(realPath, function(err, data) {
//				res.end(data);
//				server.cache[realPath] = data;
//			});
//		} else {
//			res.end(server.cache[realPath]);
//		}
		/* Node+cache end */

	/* Node+Redis start */
	client.get(realPath, function(err, data) {
		if(null != data) {
			res.end(data);
		} else {
			fs.readFile(realPath, function(err, data) {
				res.end(data);
				client.exists(realPath, function(err, is) {
					if(0 == is) {
						client.set(realPath, data, redis.print);
					}
				});
			});
		}
	});
	/* Node+Redis end */
});
server.cache = {};
server.listen(8080);
8 回复

你认为无cache的apache和纯node都用到了系统本身的IO buffer cache. 用redis的多走了一道socket输出+接收.

个人认为这个测试,除了redis,别的都是在缓存中处理,不会每次读取物理设备 所以加了redis一个环节,应该就比其他的慢了。 哪个和缓存联系的最直接,哪个最快,应该是这个路子吧?

系统本身的IO buffer cache 是指操作系统吗?

“缓存” 是哪里的缓存?

@wangjiakai 应该是指Apache或Nodejs系统内部的缓存

@wangjiakai 对, 操作系统会把暂时用不到的内存拿去缓存频繁存取的文件, 你这种benchmark基本都会命中。

@whatsmynick 这样的话,就得随机访问大量的文件才能看出性能优劣了。 有没有更好一点的测试评估工具?自己写的话,没那么全面、准确。

@j4cnodejs 看来得请教细致研究过Apache和Node.js的人了

回到顶部