Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mina-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
<parent>
<groupId>org.apache.mina</groupId>
<artifactId>mina-parent</artifactId>
<version>2.0.8-SNAPSHOT</version>
<version>2.2.11-SNAPSHOT</version>
</parent>

<artifactId>mina-benchmarks</artifactId>
<groupId>org.apache.mina</groupId>
<version>2.0.12-SNAPSHOT</version>
<version>2.2.11-SNAPSHOT</version>
<name>Apache MINA Benchmarks tests</name>
<properties>
<!-- defined in order to run against a different MINA version -->
<mina.version>${project.version}</mina.version>
<netty.version>3.5.9.Final</netty.version>
<netty.version>4.2.21.Final</netty.version>
</properties>

<dependencies>
Expand All @@ -47,7 +47,7 @@
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<artifactId>netty-transport</artifactId>
<version>${netty.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,86 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFactory;
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.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class NettyBenchmarkClient implements BenchmarkClient {

private ChannelFactory factory;
private EventLoopGroup group;

/**
*
*/
public NettyBenchmarkClient() {
}

/**
* {@inheritedDoc}
* {@inheritDoc}
*/
@Override
public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
factory = new NioClientSocketChannelFactory();
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setOption("sendBufferSize", 64 * 1024);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new SimpleChannelUpstreamHandler() {
private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
ctx.getChannel().write(buffer);
}

group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
ChannelFuture connected = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_SNDBUF, 64 * 1024)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (e.getMessage() instanceof ChannelBuffer) {
ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
for (int i = 0; i < buffer.readableBytes(); ++i) {
counter.countDown();
if (counter.getCount() > 0) {
sendMessage(ctx, data);
} else {
ctx.getChannel().close();
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
sendMessage(ctx);
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) {
for (int i = 0; i < buffer.readableBytes(); ++i) {
counter.countDown();
if (counter.getCount() == 0) {
ctx.close();
return;
}
sendMessage(ctx);
}
}
} else {
throw new IllegalArgumentException(e.getMessage().getClass().getName());
}
}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
sendMessage(ctx, data);
private void sendMessage(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.wrappedBuffer(data));
}
});
}

});
})
.connect(new InetSocketAddress(port)).awaitUninterruptibly();
if (!connected.isSuccess()) {
throw new IOException("Could not connect Netty benchmark client to port " + port, connected.cause());
}
});
bootstrap.connect(new InetSocketAddress(port));
} catch (IOException | RuntimeException | Error e) {
stop();
throw e;
}
}

/**
* {@inheritedDoc}
* {@inheritDoc}
*/
public void stop() throws IOException {
factory.releaseExternalResources();
@Override
public void stop() {
if (group != null) {
group.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).awaitUninterruptibly();
group = null;
}
}
}
Loading
Loading