use of com.generallycloud.baseio.component.AioSocketChannelContext in project baseio by generallycloud.
the class TestLoadClient method main.
public static void main(String[] args) throws Exception {
final Logger logger = LoggerFactory.getLogger(TestLoadClient.class);
final CountDownLatch latch = new CountDownLatch(time);
final AtomicInteger res = new AtomicInteger();
final AtomicInteger req = new AtomicInteger();
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
// latch.countDown();
// long count = latch.getCount();
// if (count % 10 == 0) {
// if (count < 50) {
// logger.info("************************================" + count);
// }
// }
// logger.info("res==========={}",res.getAndIncrement());
}
};
ServerConfiguration configuration = new ServerConfiguration(8300);
// SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelContext context = new AioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
connector.getContext().setProtocolFactory(new FixedLengthProtocolFactory());
connector.getContext().getServerConfiguration().setSERVER_CORE_SIZE(1);
SocketSession session = connector.connect();
System.out.println("################## Test start ####################");
long old = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
future.write("hello server!");
session.flush(future);
}
latch.await();
long spend = (System.currentTimeMillis() - old);
System.out.println("## Execute Time:" + time);
System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
System.out.println("## Expend Time:" + spend);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.AioSocketChannelContext in project baseio by generallycloud.
the class TestHttpLoadServerAio method main.
public static void main(String[] args) throws Exception {
final AtomicInteger res = new AtomicInteger();
final AtomicInteger req = new AtomicInteger();
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
HttpFuture f = (HttpFuture) future;
String res;
if (f.hasBodyContent()) {
byte[] array = f.getBodyContent();
res = "yes server already accept your message :) </BR><PRE style='font-size: 18px;color: #FF9800;'>" + new String(array) + "</PRE>";
} else {
res = "yes server already accept your message :) " + f.getRequestParams();
}
f.write(res);
session.flush(f);
// System.out.println("req======================"+req.getAndIncrement());
}
};
ServerConfiguration c = new ServerConfiguration(80);
c.setSERVER_MEMORY_POOL_CAPACITY(2560000);
c.setSERVER_MEMORY_POOL_UNIT(256);
c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
c.setSERVER_CORE_SIZE(4);
c.setSERVER_ENABLE_MEMORY_POOL(true);
c.setSERVER_MEMORY_POOL_CAPACITY_RATE(0.5);
SocketChannelContext context = new AioSocketChannelContext(c);
context.setProtocolFactory(new ServerHTTPProtocolFactory());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
new SocketChannelAcceptor(context).bind();
ThreadUtil.sleep(99999999);
}
use of com.generallycloud.baseio.component.AioSocketChannelContext in project baseio by generallycloud.
the class AioSocketChannelAcceptor method bind.
@Override
protected void bind(InetSocketAddress socketAddress) throws IOException {
AioSocketChannelContext context = (AioSocketChannelContext) getContext();
AsynchronousChannelGroup group = context.getAsynchronousChannelGroup();
final FixedAtomicInteger channelIds = new FixedAtomicInteger(1, Integer.MAX_VALUE);
serverSocketChannel = AsynchronousServerSocketChannel.open(group);
serverSocketChannel.bind(socketAddress);
serverSocketChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(AsynchronousSocketChannel _channel, Void attachment) {
// 接受下一个连接
serverSocketChannel.accept(null, this);
int channelId = channelIds.getAndIncrement();
CachedAioThread aioThread = (CachedAioThread) Thread.currentThread();
AioSocketChannel channel = new AioSocketChannel(aioThread, _channel, channelId);
channel.fireOpend();
aioThread.getReadCompletionHandler().completed(0, channel);
}
@Override
public void failed(Throwable exc, Void attachment) {
logger.error(exc.getMessage(), exc);
}
});
}
Aggregations