|
@@ -16,16 +16,15 @@
|
|
package hello;
|
|
package hello;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
-import com.generallycloud.baseio.Constants;
|
|
|
|
|
|
+import com.generallycloud.baseio.Options;
|
|
import com.generallycloud.baseio.buffer.ByteBuf;
|
|
import com.generallycloud.baseio.buffer.ByteBuf;
|
|
|
|
+import com.generallycloud.baseio.codec.http11.HttpCodec;
|
|
|
|
+import com.generallycloud.baseio.codec.http11.HttpFrame;
|
|
import com.generallycloud.baseio.codec.http11.HttpHeader;
|
|
import com.generallycloud.baseio.codec.http11.HttpHeader;
|
|
import com.generallycloud.baseio.codec.http11.HttpStatic;
|
|
import com.generallycloud.baseio.codec.http11.HttpStatic;
|
|
import com.generallycloud.baseio.codec.http11.HttpStatus;
|
|
import com.generallycloud.baseio.codec.http11.HttpStatus;
|
|
-import com.generallycloud.baseio.codec.http11.ServerHttpCodec;
|
|
|
|
-import com.generallycloud.baseio.codec.http11.ServerHttpFrame;
|
|
|
|
import com.generallycloud.baseio.common.Encoding;
|
|
import com.generallycloud.baseio.common.Encoding;
|
|
import com.generallycloud.baseio.component.ChannelAcceptor;
|
|
import com.generallycloud.baseio.component.ChannelAcceptor;
|
|
-import com.generallycloud.baseio.component.ChannelContext;
|
|
|
|
import com.generallycloud.baseio.component.IoEventHandle;
|
|
import com.generallycloud.baseio.component.IoEventHandle;
|
|
import com.generallycloud.baseio.component.NioEventLoopGroup;
|
|
import com.generallycloud.baseio.component.NioEventLoopGroup;
|
|
import com.generallycloud.baseio.component.NioSocketChannel;
|
|
import com.generallycloud.baseio.component.NioSocketChannel;
|
|
@@ -33,62 +32,65 @@ import com.generallycloud.baseio.log.LoggerFactory;
|
|
import com.generallycloud.baseio.protocol.Frame;
|
|
import com.generallycloud.baseio.protocol.Frame;
|
|
|
|
|
|
public class TestHttpLoadServer {
|
|
public class TestHttpLoadServer {
|
|
-
|
|
|
|
|
|
+
|
|
static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(Encoding.UTF8);
|
|
static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(Encoding.UTF8);
|
|
- static final byte[] CONTENT_TYPE_JSON = "application/json".getBytes(Encoding.UTF8);
|
|
|
|
-
|
|
|
|
|
|
+ static final byte[] STATIC_SERVER = "baseio".getBytes();
|
|
|
|
+
|
|
public static void main(String[] args) throws Exception {
|
|
public static void main(String[] args) throws Exception {
|
|
- LoggerFactory.setLogLevel(LoggerFactory.LEVEL_ERROR);
|
|
|
|
- System.setProperty(Constants.DEVELOP_DEBUG_KEY, "false");
|
|
|
|
-
|
|
|
|
|
|
+ LoggerFactory.setEnableSLF4JLogger(false);
|
|
|
|
+ LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
|
|
|
|
+ Options.setDevelopDebug(false);
|
|
|
|
+ Options.setChannelReadFirst(true);
|
|
|
|
+
|
|
IoEventHandle eventHandle = new IoEventHandle() {
|
|
IoEventHandle eventHandle = new IoEventHandle() {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void accept(NioSocketChannel ch, Frame frame) throws Exception {
|
|
public void accept(NioSocketChannel ch, Frame frame) throws Exception {
|
|
- ServerHttpFrame f = (ServerHttpFrame) frame;
|
|
|
|
- String action = f.getRequestURI();
|
|
|
|
- f.setResponseHeader(HttpHeader.Connection_Bytes, null);
|
|
|
|
-
|
|
|
|
- if("/plaintext".equals(action)){
|
|
|
|
- frame.write(STATIC_PLAINTEXT);
|
|
|
|
- f.setResponseHeader(HttpHeader.Content_Type_Bytes, HttpStatic.plain_bytes);
|
|
|
|
- }else if("/json".equals(action)){
|
|
|
|
- frame.write(JSON.toJSONString(new Message("Hello, World!")), ch);
|
|
|
|
- f.setResponseHeader(HttpHeader.Content_Type_Bytes, CONTENT_TYPE_JSON);
|
|
|
|
- }else{
|
|
|
|
- frame.write("404,page not found!",ch);
|
|
|
|
- f.setStatus(HttpStatus.C404);
|
|
|
|
- }
|
|
|
|
- ByteBuf buf = ch.encode(frame);
|
|
|
|
|
|
+ HttpFrame f = (HttpFrame) frame;
|
|
|
|
+ String action = f.getRequestURI();
|
|
|
|
+ f.getResponseHeaders().remove(HttpHeader.Connection);
|
|
|
|
+ f.setResponseHeader(HttpHeader.Server, STATIC_SERVER);
|
|
|
|
+
|
|
|
|
+ if ("/plaintext".equals(action)) {
|
|
|
|
+ f.write(STATIC_PLAINTEXT);
|
|
|
|
+ f.setResponseHeader(HttpHeader.Content_Type, HttpStatic.text_plain_bytes);
|
|
|
|
+ } else if ("/json".equals(action)) {
|
|
|
|
+ f.write(JSON.toJSONString(new Message("Hello, World!")), ch);
|
|
|
|
+ f.setResponseHeader(HttpHeader.Content_Type, HttpStatic.application_json_bytes);
|
|
|
|
+ } else {
|
|
|
|
+ f.write("404,page not found!", ch);
|
|
|
|
+ f.setStatus(HttpStatus.C404);
|
|
|
|
+ }
|
|
|
|
+ ByteBuf buf = ch.encode(f);
|
|
ch.flush(buf);
|
|
ch.flush(buf);
|
|
- f.release(ch.getEventLoop());
|
|
|
|
|
|
+ ch.release(f);
|
|
}
|
|
}
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ int core_size = Runtime.getRuntime().availableProcessors();
|
|
NioEventLoopGroup group = new NioEventLoopGroup();
|
|
NioEventLoopGroup group = new NioEventLoopGroup();
|
|
- group.setMemoryPoolCapacity(1024 * 256);
|
|
|
|
|
|
+ group.setMemoryPoolCapacity(1024 * 1024 / core_size);
|
|
group.setMemoryPoolUnit(512);
|
|
group.setMemoryPoolUnit(512);
|
|
- ChannelContext context = new ChannelContext(8080);
|
|
|
|
- ChannelAcceptor acceptor = new ChannelAcceptor(context, group);
|
|
|
|
- context.setProtocolCodec(new ServerHttpCodec(1024 * 8));
|
|
|
|
|
|
+ group.setEventLoopSize(core_size);
|
|
|
|
+ ChannelAcceptor context = new ChannelAcceptor(group, 8080);
|
|
|
|
+ context.setProtocolCodec(new HttpCodec(1024 * 16));
|
|
context.setIoEventHandle(eventHandle);
|
|
context.setIoEventHandle(eventHandle);
|
|
-
|
|
|
|
- acceptor.bind();
|
|
|
|
|
|
+ context.bind();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static class Message {
|
|
static class Message {
|
|
|
|
|
|
- private final String message;
|
|
|
|
|
|
+ private final String message;
|
|
|
|
|
|
- public Message(String message) {
|
|
|
|
- this.message = message;
|
|
|
|
- }
|
|
|
|
|
|
+ public Message(String message) {
|
|
|
|
+ this.message = message;
|
|
|
|
+ }
|
|
|
|
|
|
- public String getMessage() {
|
|
|
|
- return message;
|
|
|
|
- }
|
|
|
|
|
|
+ public String getMessage() {
|
|
|
|
+ return message;
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|