123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package hello;
- import java.io.IOException;
- import java.util.Arrays;
- import com.firenio.Options;
- import com.firenio.codec.http11.HttpCodec;
- import com.firenio.codec.http11.HttpConnection;
- import com.firenio.codec.http11.HttpContentType;
- import com.firenio.codec.http11.HttpDateUtil;
- import com.firenio.codec.http11.HttpFrame;
- import com.firenio.codec.http11.HttpStatus;
- import com.firenio.collection.ByteTree;
- import com.firenio.common.Util;
- import com.firenio.component.Channel;
- import com.firenio.component.ChannelAcceptor;
- import com.firenio.component.ChannelEventListenerAdapter;
- import com.firenio.component.Frame;
- import com.firenio.component.IoEventHandle;
- import com.firenio.component.NioEventLoopGroup;
- import com.firenio.component.SocketOptions;
- import com.firenio.log.DebugUtil;
- import com.firenio.log.LoggerFactory;
- 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();
- 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.getBooleanProperty("lite");
- boolean read = Util.getBooleanProperty("read");
- boolean pool = Util.getBooleanProperty("pool");
- boolean epoll = Util.getBooleanProperty("epoll");
- boolean direct = Util.getBooleanProperty("direct");
- boolean inline = Util.getBooleanProperty("inline");
- boolean nodelay = Util.getBooleanProperty("nodelay");
- boolean cachedurl = Util.getBooleanProperty("cachedurl");
- 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);
- DebugUtil.info("core: {}", core);
- DebugUtil.info("epoll: {}", epoll);
- DebugUtil.info("frame: {}", frame);
- DebugUtil.info("level: {}", level);
- DebugUtil.info("direct: {}", direct);
- DebugUtil.info("inline: {}", inline);
- DebugUtil.info("readBuf: {}", readBuf);
- DebugUtil.info("nodelay: {}", nodelay);
- DebugUtil.info("cachedurl: {}", cachedurl);
- DebugUtil.info("unsafeBuf: {}", unsafeBuf);
- IoEventHandle eventHandle = new IoEventHandle() {
- @Override
- public void accept(Channel ch, Frame frame) throws Exception {
- HttpFrame f = (HttpFrame) frame;
- String action = f.getRequestURL();
- if ("/plaintext".equals(action)) {
- f.setContent(STATIC_PLAINTEXT);
- f.setContentType(HttpContentType.text_plain);
- f.setConnection(HttpConnection.NONE);
- } else if ("/json".equals(action)) {
- f.setContent(serializeMsg(new Message("Hello, World!")));
- f.setContentType(HttpContentType.application_json);
- f.setConnection(HttpConnection.NONE);
- } else {
- System.err.println("404");
- f.setContent("404,page not found!".getBytes());
- f.setContentType(HttpContentType.text_plain);
- f.setStatus(HttpStatus.C404);
- }
- f.setDate(HttpDateUtil.getDateLine());
- ch.writeAndFlush(f);
- ch.release(f);
- }
- };
- int fcache = 1024 * 16;
- int pool_cap = 1024 * 128;
- int pool_unit = 256;
- if (inline) {
- pool_cap = 1024 * 8;
- pool_unit = 256 * 16;
- }
- HttpDateUtil.start();
- NioEventLoopGroup group = new NioEventLoopGroup();
- ChannelAcceptor context = new ChannelAcceptor(group, 8080);
- group.setMemoryPoolCapacity(pool_cap);
- group.setEnableMemoryPoolDirect(direct);
- group.setEnableMemoryPool(pool);
- group.setMemoryPoolUnit(pool_unit);
- group.setWriteBuffers(32);
- group.setChannelReadBuffer(1024 * readBuf);
- group.setEventLoopSize(Util.availableProcessors() * core);
- group.setConcurrentFrameStack(false);
- if (nodelay) {
- 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);
- }
- });
- }
- ByteTree cachedUrls = null;
- if (cachedurl){
- cachedUrls = new ByteTree();
- cachedUrls.add("/plaintext");
- cachedUrls.add("/json");
- }
- context.addProtocolCodec(new HttpCodec("firenio", fcache, lite, inline, cachedUrls));
- context.setIoEventHandle(eventHandle);
- context.bind(1024 * 8);
- }
- 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);
- }
- }
- }
|