TestHttpLoadServer.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package hello;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import com.firenio.Options;
  5. import com.firenio.codec.http11.HttpCodec;
  6. import com.firenio.codec.http11.HttpConnection;
  7. import com.firenio.codec.http11.HttpContentType;
  8. import com.firenio.codec.http11.HttpDateUtil;
  9. import com.firenio.codec.http11.HttpFrame;
  10. import com.firenio.codec.http11.HttpStatus;
  11. import com.firenio.collection.ByteTree;
  12. import com.firenio.common.Util;
  13. import com.firenio.component.Channel;
  14. import com.firenio.component.ChannelAcceptor;
  15. import com.firenio.component.ChannelEventListenerAdapter;
  16. import com.firenio.component.Frame;
  17. import com.firenio.component.IoEventHandle;
  18. import com.firenio.component.NioEventLoopGroup;
  19. import com.firenio.component.SocketOptions;
  20. import com.firenio.log.DebugUtil;
  21. import com.firenio.log.LoggerFactory;
  22. import com.jsoniter.output.JsonStream;
  23. import com.jsoniter.output.JsonStreamPool;
  24. import com.jsoniter.spi.JsonException;
  25. public class TestHttpLoadServer {
  26. static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes();
  27. static class Message {
  28. private final String message;
  29. public Message(String message) {
  30. this.message = message;
  31. }
  32. public String getMessage() {
  33. return message;
  34. }
  35. }
  36. public static void main(String[] args) throws Exception {
  37. boolean lite = Util.getBooleanProperty("lite");
  38. boolean read = Util.getBooleanProperty("read");
  39. boolean pool = Util.getBooleanProperty("pool");
  40. boolean epoll = Util.getBooleanProperty("epoll");
  41. boolean direct = Util.getBooleanProperty("direct");
  42. boolean inline = Util.getBooleanProperty("inline");
  43. boolean nodelay = Util.getBooleanProperty("nodelay");
  44. boolean cachedurl = Util.getBooleanProperty("cachedurl");
  45. boolean unsafeBuf = Util.getBooleanProperty("unsafeBuf");
  46. int core = Util.getIntProperty("core", 1);
  47. int frame = Util.getIntProperty("frame", 16);
  48. int level = Util.getIntProperty("level", 1);
  49. int readBuf = Util.getIntProperty("readBuf", 16);
  50. LoggerFactory.setEnableSLF4JLogger(false);
  51. LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
  52. Options.setBufAutoExpansion(false);
  53. Options.setDebugErrorLevel(level);
  54. Options.setChannelReadFirst(read);
  55. Options.setEnableEpoll(epoll);
  56. Options.setEnableUnsafeBuf(unsafeBuf);
  57. DebugUtil.info("lite: {}", lite);
  58. DebugUtil.info("read: {}", read);
  59. DebugUtil.info("pool: {}", pool);
  60. DebugUtil.info("core: {}", core);
  61. DebugUtil.info("epoll: {}", epoll);
  62. DebugUtil.info("frame: {}", frame);
  63. DebugUtil.info("level: {}", level);
  64. DebugUtil.info("direct: {}", direct);
  65. DebugUtil.info("inline: {}", inline);
  66. DebugUtil.info("readBuf: {}", readBuf);
  67. DebugUtil.info("nodelay: {}", nodelay);
  68. DebugUtil.info("cachedurl: {}", cachedurl);
  69. DebugUtil.info("unsafeBuf: {}", unsafeBuf);
  70. IoEventHandle eventHandle = new IoEventHandle() {
  71. @Override
  72. public void accept(Channel ch, Frame frame) throws Exception {
  73. HttpFrame f = (HttpFrame) frame;
  74. String action = f.getRequestURL();
  75. if ("/plaintext".equals(action)) {
  76. f.setContent(STATIC_PLAINTEXT);
  77. f.setContentType(HttpContentType.text_plain);
  78. f.setConnection(HttpConnection.NONE);
  79. } else if ("/json".equals(action)) {
  80. f.setContent(serializeMsg(new Message("Hello, World!")));
  81. f.setContentType(HttpContentType.application_json);
  82. f.setConnection(HttpConnection.NONE);
  83. } else {
  84. System.err.println("404");
  85. f.setContent("404,page not found!".getBytes());
  86. f.setContentType(HttpContentType.text_plain);
  87. f.setStatus(HttpStatus.C404);
  88. }
  89. f.setDate(HttpDateUtil.getDateLine());
  90. ch.writeAndFlush(f);
  91. ch.release(f);
  92. }
  93. };
  94. int fcache = 1024 * 16;
  95. int pool_cap = 1024 * 128;
  96. int pool_unit = 256;
  97. if (inline) {
  98. pool_cap = 1024 * 8;
  99. pool_unit = 256 * 16;
  100. }
  101. HttpDateUtil.start();
  102. NioEventLoopGroup group = new NioEventLoopGroup();
  103. ChannelAcceptor context = new ChannelAcceptor(group, 8080);
  104. group.setMemoryPoolCapacity(pool_cap);
  105. group.setEnableMemoryPoolDirect(direct);
  106. group.setEnableMemoryPool(pool);
  107. group.setMemoryPoolUnit(pool_unit);
  108. group.setWriteBuffers(32);
  109. group.setChannelReadBuffer(1024 * readBuf);
  110. group.setEventLoopSize(Util.availableProcessors() * core);
  111. group.setConcurrentFrameStack(false);
  112. if (nodelay) {
  113. context.addChannelEventListener(new ChannelEventListenerAdapter() {
  114. @Override
  115. public void channelOpened(Channel ch) throws Exception {
  116. ch.setOption(SocketOptions.TCP_NODELAY, 1);
  117. ch.setOption(SocketOptions.TCP_QUICKACK, 1);
  118. ch.setOption(SocketOptions.SO_KEEPALIVE, 0);
  119. }
  120. });
  121. }
  122. ByteTree cachedUrls = null;
  123. if (cachedurl){
  124. cachedUrls = new ByteTree();
  125. cachedUrls.add("/plaintext");
  126. cachedUrls.add("/json");
  127. }
  128. context.addProtocolCodec(new HttpCodec("firenio", fcache, lite, inline, cachedUrls));
  129. context.setIoEventHandle(eventHandle);
  130. context.bind(1024 * 8);
  131. }
  132. private static byte[] serializeMsg(Message obj) {
  133. JsonStream stream = JsonStreamPool.borrowJsonStream();
  134. try {
  135. stream.reset(null);
  136. stream.writeVal(Message.class, obj);
  137. return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
  138. } catch (IOException e) {
  139. throw new JsonException(e);
  140. } finally {
  141. JsonStreamPool.returnJsonStream(stream);
  142. }
  143. }
  144. }