|
@@ -15,43 +15,66 @@
|
|
|
*/
|
|
|
package hello;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
import com.firenio.baseio.Options;
|
|
|
-import com.firenio.baseio.buffer.ByteBuf;
|
|
|
import com.firenio.baseio.codec.http11.HttpCodec;
|
|
|
import com.firenio.baseio.codec.http11.HttpCodecLite;
|
|
|
import com.firenio.baseio.codec.http11.HttpFrameLite;
|
|
|
import com.firenio.baseio.codec.http11.HttpStatic;
|
|
|
import com.firenio.baseio.codec.http11.HttpStatus;
|
|
|
-import com.firenio.baseio.common.Encoding;
|
|
|
import com.firenio.baseio.common.Util;
|
|
|
+import com.firenio.baseio.component.Channel;
|
|
|
import com.firenio.baseio.component.ChannelAcceptor;
|
|
|
+import com.firenio.baseio.component.ChannelEventListenerAdapter;
|
|
|
+import com.firenio.baseio.component.Frame;
|
|
|
import com.firenio.baseio.component.IoEventHandle;
|
|
|
import com.firenio.baseio.component.NioEventLoopGroup;
|
|
|
-import com.firenio.baseio.component.NioSocketChannel;
|
|
|
+import com.firenio.baseio.component.ProtocolCodec;
|
|
|
+import com.firenio.baseio.component.SocketOptions;
|
|
|
import com.firenio.baseio.log.DebugUtil;
|
|
|
import com.firenio.baseio.log.LoggerFactory;
|
|
|
-import com.firenio.baseio.protocol.Frame;
|
|
|
-import com.firenio.baseio.protocol.ProtocolCodec;
|
|
|
+import com.jsoniter.output.JsonStream;
|
|
|
+import com.jsoniter.output.JsonStreamPool;
|
|
|
+import com.jsoniter.spi.JsonException;
|
|
|
|
|
|
public class TestHttpLoadServer {
|
|
|
|
|
|
- static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(Encoding.UTF8);
|
|
|
- static final byte[] STATIC_SERVER = "baseio".getBytes();
|
|
|
+ static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes();
|
|
|
+
|
|
|
+ static class Message {
|
|
|
+
|
|
|
+ private final String message;
|
|
|
+
|
|
|
+ public Message(String message) {
|
|
|
+ this.message = message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMessage() {
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
- boolean lite = Util.isSystemTrue("lite");
|
|
|
- boolean read = Util.isSystemTrue("read");
|
|
|
- boolean pool = Util.isSystemTrue("pool");
|
|
|
- boolean direct = Util.isSystemTrue("direct");
|
|
|
- int core = Util.getProperty("core", 1);
|
|
|
- int frame = Util.getProperty("frame", 0);
|
|
|
- int level = Util.getProperty("level");
|
|
|
- int readBuf = Util.getProperty("readBuf", 1);
|
|
|
+ boolean lite = Util.getBooleanProperty("lite");
|
|
|
+ boolean read = Util.getBooleanProperty("read");
|
|
|
+ boolean pool = Util.getBooleanProperty("pool");
|
|
|
+ boolean direct = Util.getBooleanProperty("direct");
|
|
|
+ boolean epoll = Util.getBooleanProperty("epoll");
|
|
|
+ boolean unsafeBuf = Util.getBooleanProperty("unsafeBuf");
|
|
|
+ int core = Util.getIntProperty("core", 1);
|
|
|
+ int frame = Util.getIntProperty("frame", 16);
|
|
|
+ int level = Util.getIntProperty("level", 1);
|
|
|
+ int readBuf = Util.getIntProperty("readBuf", 16);
|
|
|
LoggerFactory.setEnableSLF4JLogger(false);
|
|
|
LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
|
|
|
+ Options.setBufAutoExpansion(false);
|
|
|
Options.setDebugErrorLevel(level);
|
|
|
Options.setChannelReadFirst(read);
|
|
|
+ Options.setEnableEpoll(epoll);
|
|
|
+ Options.setEnableUnsafeBuf(unsafeBuf);
|
|
|
DebugUtil.info("lite: {}", lite);
|
|
|
DebugUtil.info("read: {}", read);
|
|
|
DebugUtil.info("pool: {}", pool);
|
|
@@ -64,56 +87,64 @@ public class TestHttpLoadServer {
|
|
|
IoEventHandle eventHandle = new IoEventHandle() {
|
|
|
|
|
|
@Override
|
|
|
- public void accept(NioSocketChannel ch, Frame frame) throws Exception {
|
|
|
+ public void accept(Channel ch, Frame frame) throws Exception {
|
|
|
HttpFrameLite f = (HttpFrameLite) frame;
|
|
|
String action = f.getRequestURL();
|
|
|
-
|
|
|
if ("/plaintext".equals(action)) {
|
|
|
- f.write(STATIC_PLAINTEXT);
|
|
|
+ f.setContent(STATIC_PLAINTEXT);
|
|
|
f.setContentType(HttpStatic.text_plain_bytes);
|
|
|
} else if ("/json".equals(action)) {
|
|
|
- f.write(JSON.toJSONString(new Message("Hello, World!")), ch);
|
|
|
+ f.setContent(serializeMsg(new Message("Hello, World!")));
|
|
|
f.setContentType(HttpStatic.application_json_bytes);
|
|
|
} else {
|
|
|
- f.write("404,page not found!", ch);
|
|
|
+ f.setContent("404,page not found!".getBytes());
|
|
|
f.setStatus(HttpStatus.C404);
|
|
|
}
|
|
|
- ByteBuf buf = ch.encode(f);
|
|
|
- ch.flush(buf);
|
|
|
+ ch.writeAndFlush(f);
|
|
|
ch.release(f);
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
- ProtocolCodec codec = lite ? new HttpCodecLite("baseio", 1024 * frame)
|
|
|
- : new HttpCodec("baseio", 1024 * frame);
|
|
|
+ ProtocolCodec codec;
|
|
|
+ if (lite) {
|
|
|
+ codec = new HttpCodecLite("baseio", 1024 * 16);
|
|
|
+ } else {
|
|
|
+ codec = new HttpCodec("baseio", 1024 * 16);
|
|
|
+ }
|
|
|
NioEventLoopGroup group = new NioEventLoopGroup();
|
|
|
- group.setEnableMemoryPool(pool);
|
|
|
- group.setEnableMemoryPoolDirect(direct);
|
|
|
+ ChannelAcceptor context = new ChannelAcceptor(group, 8080);
|
|
|
group.setMemoryPoolCapacity(1024 * 128);
|
|
|
- group.setChannelReadBuffer(1024 * readBuf);
|
|
|
group.setMemoryPoolUnit(256);
|
|
|
group.setWriteBuffers(32);
|
|
|
+ group.setChannelReadBuffer(1024 * readBuf);
|
|
|
group.setEventLoopSize(Util.availableProcessors() * core);
|
|
|
group.setConcurrentFrameStack(false);
|
|
|
- ChannelAcceptor context = new ChannelAcceptor(group, 8080);
|
|
|
- context.setProtocolCodec(codec);
|
|
|
+ context.addProtocolCodec(codec);
|
|
|
+ context.addChannelEventListener(new ChannelEventListenerAdapter() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelOpened(Channel ch) throws Exception {
|
|
|
+ ch.setOption(SocketOptions.TCP_NODELAY, 1);
|
|
|
+ ch.setOption(SocketOptions.TCP_QUICKACK, 1);
|
|
|
+ ch.setOption(SocketOptions.SO_KEEPALIVE, 0);
|
|
|
+ }
|
|
|
+ });
|
|
|
context.setIoEventHandle(eventHandle);
|
|
|
context.bind();
|
|
|
}
|
|
|
|
|
|
- static class Message {
|
|
|
-
|
|
|
- private final String message;
|
|
|
-
|
|
|
- public Message(String message) {
|
|
|
- this.message = message;
|
|
|
- }
|
|
|
-
|
|
|
- public String getMessage() {
|
|
|
- return message;
|
|
|
+ private static byte[] serializeMsg(Message obj) {
|
|
|
+ JsonStream stream = JsonStreamPool.borrowJsonStream();
|
|
|
+ try {
|
|
|
+ stream.reset(null);
|
|
|
+ stream.writeVal(Message.class, obj);
|
|
|
+ return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new JsonException(e);
|
|
|
+ } finally {
|
|
|
+ JsonStreamPool.returnJsonStream(stream);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|