周末跟朋友聊天, 一个朋友说NodeJS是个不错的东西, 它的非阻塞异步处理模式比用apache的fork的方式高效.
我在想如果是依靠非阻塞异步处理的方式提升性能的话, 其实现在主流的平台上都有相应的东西, 例如Java的NIO. 我想肯定有人做过比较, 于是我上网搜了一下Node.js vs Netty.
用apache benchmark来做的基于HTTP协议上的Helloworld的测试
ab -r30000 -c100 http://localhost:<port>/
这个benchmark测试下来的结果是Netty的wormup时间比Node.js长, 但整体来说性能要高40%. 这个测试结果本身并不能说明什么问题. 因为Helloworld和实际应用相差太远.
Node.js 实现代码
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
Netty 实现代码
NettyModule.java
public class NettyModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
SocketAddress provideSocketAddress() {
return new InetSocketAddress(8080);
}
@Provides @Singleton
ChannelGroup provideChannelGroup() {
return new DefaultChannelGroup("http-server");
}
@Provides
ChannelFactory provideChannelFactory() {
return new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
}
public static void main(String[] args) {
Injector injector = Guice.createInjector(new NettyModule());
final NettyServer server = injector.getInstance(NettyServer.class);
server.startAndWait();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
server.stopAndWait();
}
});
}
}
NettyServer.java
package netty;
import java.net.SocketAddress;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpServerCodec;
import org.jboss.netty.handler.codec.http.HttpVersion;
import com.google.common.base.Charsets;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Inject;
import com.google.inject.Provider;
class NettyServer extends AbstractIdleService {
private final ChannelGroup allChannels;
private final SocketAddress address;
private final ChannelFactory factory;
private final ServerBootstrap bootstrap;
private final Provider<Handler> handler;
@Inject
NettyServer(ChannelFactory factory, ChannelGroup allChannels, SocketAddress address, Provider<Handler> handler) {
this.factory = factory;
this.bootstrap = new ServerBootstrap(factory);
this.allChannels = allChannels;
this.address = address;
this.handler = handler;
}
@Override
protected void startUp() throws Exception {
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new HttpServerCodec(), handler.get());
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
Channel channel = bootstrap.bind(address);
allChannels.add(channel);
}
@Override
protected void shutDown() throws Exception {
allChannels.close().awaitUninterruptibly();
factory.releaseExternalResources();
}
}
class Handler extends SimpleChannelHandler {
private final ChannelGroup allChannels;
@Inject
Handler(ChannelGroup allChannels) {
this.allChannels = allChannels;
}
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
allChannels.add(e.getChannel());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setContent(ChannelBuffers.copiedBuffer("Hello, world!", Charsets.UTF_8));
e.getChannel().write(response).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
future.getChannel().close();
}});
}
}
原文地址:http://devnest.blogspot.hk/2012/01/nodejs-vs-netty.html
我觉得原文的回复最有意思
Typical php-codemonkey would simply stop reading after the first snippet, silently committing to 40% worse performance - 6 loc vs 100+ loc... :-)
一般来说PHP的程序员在读完第一个代码片段后就不会再往下读了, 宁愿忍受40%的性能差距. 6行代码VS 100+代码!
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com