NodeJS vs Netty

16 篇文章 0 订阅
1 篇文章 0 订阅

周末跟朋友聊天, 一个朋友说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+代码!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值