高性能,跨平台,轻量级Nodejs静态文件ifile模块
高性能,跨平台,轻量级Nodejs静态文件ifile模块
之前写了篇博客作为ifile模块的前瞻,终于经过几个晚上的奋斗,ifile模块发布了,ifile模块是nodejs的http/https静态文件模块,主要解决了nodejs对静态文件疲软的支持,模块主要采用libuv的多线程异步编写而成. 整个项目跨平台支持,win8,linux, mac 10.8,均测试通过,mac下挺奇怪的,在线程里加锁会报错,有知道的朋友能指点一二吗?
项目地址:https://github.com/DoubleSpout/ifile 安装方法:
npm install ifile
代码示例:
var ifile = require('ifile')
var http = require('http')
ifile.add([
["/static",__dirname,['js','css','jpg']],
],function(req,res,is_static){
res.statusCode = 404;
res.end('404')
})
var http = require('http');
http.createServer(function (req, res) {
ifile.route(req,res);
}).listen(8124);
下面一张图片用来说明ifile模块和传统nodejs的静态模块的确别和优势:
ifile模块解决了nodejs单一主线程所造成的频繁线程切换的性能损耗,ifile模块所有计算,添加响应头,读取文件,gzip压缩等都是在libuv的工作线程中执行的,所以主线程能够尽可能多的接受用户的请求,增加系统的吞吐量。
下面是一组ifile模块和expressjs框架的静态文件压力测试数据报告,测试机器和网络都相同,test.js为同一个文件,大小是6KB,我们看下测试结果: 首先是expressjs
express
ab -c 100 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 915.21 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 858.89 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 668.99 [#/sec] (mean)
ab -c 500 -n 20000 -H “Accept-Encoding: gzip” http://192.168.28.5:8126/js/test.js
Requests per second: 677.11 [#/sec] (mean)
然后是ifile
ifile
ab -c 100 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 2077.29 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 1880.00 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 1791.16 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8125/js/test.js
Requests per second: 1858.01 [#/sec] (mean)
在性能上ifile模块比express要高出1倍还不止,有的甚至将近3倍,当然express框架还有其他的一些损耗,比如添加req和res的原形链等等,于是我将express的静态文件功能交由ifile模块来执行,看下测试结果:
express+ifile
ab -c 100 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1684.85 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1717.32 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1399.09 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8127/js/test.js
Requests per second: 1468.06 [#/sec] (mean)
性能虽较单独使用ifile模块由所下降,但是比expressjs自带的静态文件输出性能提升1倍。
最后我们再看下老牌web服务器nginx的性能把:
nginx
ab -c 100 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2634.31 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2086.92 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2033.45 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8124/js/test.js
Requests per second: 2029.59 [#/sec] (mean)
性能相当强悍,比ifile模块还要高一个数量级,看来我写ifile想超越nginx的静态文件性能的愿望是破灭了。
附上ifile整合入expressjs框架的代码:
var express = require('express');
var app = express();
var ifile = require("ifile");
app.use(ifile.connect());
//default is [['/static',__dirname]];
app.listen(3000);
最后欢迎广大TX下载试用,有任何问题欢迎留言.
ps.社区右下角的markdown简明教程很好,短短几行,让我这个外行人瞬间会编辑markdown了。
高性能,跨平台,轻量级Nodejs静态文件ifile模块
之前写了篇博客作为ifile模块的前瞻,终于经过几个晚上的奋斗,ifile模块发布了。ifile模块是Node.js的HTTP/HTTPS静态文件模块,主要解决了Node.js对静态文件支持不足的问题。该模块主要采用libuv的多线程异步编写而成。
整个项目跨平台支持,包括Windows 8、Linux、Mac OS X 10.8等操作系统,均测试通过。在Mac下遇到一个奇怪的现象:在线程里加锁会报错,如果有知道原因的朋友可以指点一下。
项目地址
https://github.com/DoubleSpout/ifile
安装方法
npm install ifile
代码示例
var ifile = require('ifile');
var http = require('http');
// 添加静态文件路径和类型
ifile.add([
["/static", __dirname, ['js', 'css', 'jpg']],
], function(req, res, is_static) {
res.statusCode = 404;
res.end('404');
});
// 创建HTTP服务器
var server = http.createServer(function(req, res) {
ifile.route(req, res);
});
server.listen(8124);
性能对比
下面是一组ifile模块和Express.js框架的静态文件压力测试数据报告。测试机器和网络条件相同,test.js
文件大小为6KB,以下是测试结果:
Express.js
ab -c 100 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 915.21 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 858.89 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8126/js/test.js
Requests per second: 668.99 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8126/js/test.js
Requests per second: 677.11 [#/sec] (mean)
ifile模块
ab -c 100 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 2077.29 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 1880.00 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8125/js/test.js
Requests per second: 1791.16 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8125/js/test.js
Requests per second: 1858.01 [#/sec] (mean)
从上面的数据可以看出,ifile模块在性能上比Express.js高出一倍以上,有的情况下甚至接近三倍。
Express + ifile模块
ab -c 100 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1684.85 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1717.32 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8127/js/test.js
Requests per second: 1399.09 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8127/js/test.js
Requests per second: 1468.06 [#/sec] (mean)
虽然性能相比单独使用ifile模块有所下降,但仍然比Express.js自带的静态文件处理性能提升了大约一倍。
老牌Web服务器Nginx
ab -c 100 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2634.31 [#/sec] (mean)
ab -c 500 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2086.92 [#/sec] (mean)
ab -c 800 -n 20000 http://192.168.28.5:8124/js/test.js
Requests per second: 2033.45 [#/sec] (mean)
ab -c 500 -n 20000 -H "Accept-Encoding: gzip" http://192.168.28.5:8124/js/test.js
Requests per second: 2029.59 [#/sec] (mean)
Nginx的表现非常强悍,比ifile模块还要高一个数量级。
整合到Express框架
var express = require('express');
var app = express();
var ifile = require('ifile');
app.use(ifile.connect());
app.listen(3000);
结论
ifile模块提供了一种高性能、跨平台的解决方案,适用于需要高效处理静态文件的Node.js应用。欢迎广大开发者下载试用,并在遇到任何问题时留言交流。
赞
非常牛
谢谢分享!
有个地方想请教一下。在图里,你的ifile主线程在实际执行中也是在node.js主线程里吗?
这个用在什么场景下?
npm install ifle 安装不了这个模块了,求给一个 编译好的可以直接使用的文件模块包,我的邮箱rajan.zhan@foxmail.com
针对“高性能,跨平台,轻量级Nodejs静态文件ifile模块”这一帖子的内容,我总结并补充了一些细节信息。ifile模块专为提高Node.js应用程序处理静态文件时的性能而设计。它利用libuv的多线程异步特性,减少主线程的负担,从而显著提升了系统处理用户请求的能力。
安装
你可以通过npm安装ifile模块:
npm install ifile
基本使用
首先需要引入ifile
模块,并设置静态文件路径和扩展名,然后使用它来处理HTTP请求:
var ifile = require('ifile');
var http = require('http');
// 添加静态文件目录及类型
ifile.add([
["/static", __dirname, ['js', 'css', 'jpg']],
], function(req, res, is_static) {
res.statusCode = 404;
res.end('404');
});
// 创建HTTP服务器并路由请求
http.createServer(function(req, res) {
ifile.route(req, res);
}).listen(8124);
性能对比
与Express和Nginx相比,ifile模块在处理大量并发请求时表现出色。例如,在相同配置下,ifile模块的请求处理速度远高于Express框架,但略逊于Nginx。
集成到Express
如果你想将ifile集成到Express应用中,可以这样做:
var express = require('express');
var app = express();
var ifile = require("ifile");
app.use(ifile.connect());
app.listen(3000);
这段代码将ifile模块集成到了Express应用中,用于处理静态文件请求。
平台支持
ifile模块已经在Windows、Linux和MacOS上进行了测试,并且具有良好的跨平台兼容性。
如果你在macOS上遇到在线程中加锁报错的问题,可能是因为macOS对线程同步有不同的限制或行为。建议查阅macOS线程编程的具体文档,以获得更详细的解决方案。