TestHttpLoadServer.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package hello;
  2. import com.firenio.Options;
  3. import com.firenio.buffer.ByteBuf;
  4. import com.firenio.codec.http11.HttpAttachment;
  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.FastThreadLocal;
  17. import com.firenio.component.Frame;
  18. import com.firenio.component.IoEventHandle;
  19. import com.firenio.component.NioEventLoopGroup;
  20. import com.firenio.component.SocketOptions;
  21. import com.firenio.log.DebugUtil;
  22. import com.firenio.log.LoggerFactory;
  23. import com.jsoniter.output.JsonStream;
  24. import com.jsoniter.output.JsonStreamPool;
  25. import com.jsoniter.spi.Slice;
  26. public class TestHttpLoadServer {
  27. static final int JSON_BUF = FastThreadLocal.nextAttributeKey();
  28. static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes();
  29. static class Message {
  30. private final String message;
  31. public Message(String message) {
  32. this.message = message;
  33. }
  34. public String getMessage() {
  35. return message;
  36. }
  37. }
  38. static class MyHttpAttachment extends HttpAttachment {
  39. ByteBuf write_buf;
  40. }
  41. public static void main(String[] args) throws Exception {
  42. boolean lite = Util.getBooleanProperty("lite");
  43. boolean read = Util.getBooleanProperty("read");
  44. boolean pool = Util.getBooleanProperty("pool");
  45. boolean epoll = Util.getBooleanProperty("epoll");
  46. boolean nodelay = Util.getBooleanProperty("nodelay");
  47. boolean cachedurl = Util.getBooleanProperty("cachedurl");
  48. boolean unsafeBuf = Util.getBooleanProperty("unsafeBuf");
  49. int core = Util.getIntProperty("core", 1);
  50. int frame = Util.getIntProperty("frame", 16);
  51. int level = Util.getIntProperty("level", 1);
  52. int readBuf = Util.getIntProperty("readBuf", 16);
  53. LoggerFactory.setEnableSLF4JLogger(false);
  54. LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
  55. Options.setBufAutoExpansion(false);
  56. Options.setChannelReadFirst(read);
  57. Options.setEnableEpoll(epoll);
  58. Options.setEnableUnsafeBuf(unsafeBuf);
  59. Options.setBufFastIndexOf(true);
  60. DebugUtil.info("lite: {}", lite);
  61. DebugUtil.info("read: {}", read);
  62. DebugUtil.info("pool: {}", pool);
  63. DebugUtil.info("core: {}", core);
  64. DebugUtil.info("epoll: {}", epoll);
  65. DebugUtil.info("frame: {}", frame);
  66. DebugUtil.info("level: {}", level);
  67. DebugUtil.info("readBuf: {}", readBuf);
  68. DebugUtil.info("nodelay: {}", nodelay);
  69. DebugUtil.info("cachedurl: {}", cachedurl);
  70. DebugUtil.info("unsafeBuf: {}", unsafeBuf);
  71. int processors = Util.availableProcessors() * core;
  72. int fcache = 1024 * 16;
  73. int pool_unit = 256 * 16;
  74. int pool_cap = 1024 * 8 * pool_unit * processors;
  75. String server = "tfb";
  76. ByteTree cachedUrls = null;
  77. if (cachedurl) {
  78. cachedUrls = new ByteTree();
  79. cachedUrls.add("/plaintext");
  80. cachedUrls.add("/json");
  81. }
  82. HttpCodec codec = new HttpCodec(server, fcache, lite, cachedUrls) {
  83. @Override
  84. protected Object newAttachment() {
  85. return new MyHttpAttachment();
  86. }
  87. };
  88. IoEventHandle eventHandle = new IoEventHandle() {
  89. @Override
  90. public void accept(Channel ch, Frame frame) throws Exception {
  91. HttpFrame f = (HttpFrame) frame;
  92. String action = f.getRequestURL();
  93. if ("/plaintext".equals(action)) {
  94. MyHttpAttachment att = (MyHttpAttachment) ch.getAttachment();
  95. ByteBuf buf = att.write_buf;
  96. if (buf == null) {
  97. buf = ch.allocate();
  98. ByteBuf temp = buf;
  99. att.write_buf = buf;
  100. ch.getEventLoop().submit(() -> {
  101. ch.writeAndFlush(temp);
  102. att.write_buf = null;
  103. });
  104. }
  105. f.setContent(STATIC_PLAINTEXT);
  106. f.setContentType(HttpContentType.text_plain);
  107. f.setConnection(HttpConnection.NONE);
  108. f.setDate(HttpDateUtil.getDateLine());
  109. codec.encode(ch, buf, f);
  110. codec.release(ch.getEventLoop(), f);
  111. } else if ("/json".equals(action)) {
  112. ByteBuf temp = FastThreadLocal.get().getAttribute(JSON_BUF);
  113. if (temp == null) {
  114. temp = ByteBuf.heap(0);
  115. FastThreadLocal.get().setAttribute(JSON_BUF, temp);
  116. }
  117. JsonStream stream = JsonStreamPool.borrowJsonStream();
  118. try {
  119. stream.reset(null);
  120. stream.writeVal(Message.class, new Message("Hello, World!"));
  121. Slice slice = stream.buffer();
  122. temp.reset(slice.data(), slice.head(), slice.tail());
  123. f.setContent(temp);
  124. f.setContentType(HttpContentType.application_json);
  125. f.setConnection(HttpConnection.NONE);
  126. f.setDate(HttpDateUtil.getDateLine());
  127. ch.writeAndFlush(f);
  128. ch.release(f);
  129. } finally {
  130. JsonStreamPool.returnJsonStream(stream);
  131. }
  132. } else {
  133. System.err.println("404");
  134. f.setString("404,page not found!", ch);
  135. f.setContentType(HttpContentType.text_plain);
  136. f.setStatus(HttpStatus.C404);
  137. f.setDate(HttpDateUtil.getDateLine());
  138. ch.writeAndFlush(f);
  139. ch.release(f);
  140. }
  141. }
  142. };
  143. HttpDateUtil.start();
  144. NioEventLoopGroup group = new NioEventLoopGroup();
  145. ChannelAcceptor context = new ChannelAcceptor(group, 8080);
  146. group.setMemoryCapacity(pool_cap);
  147. group.setEnableMemoryPool(pool);
  148. group.setMemoryUnit(pool_unit);
  149. group.setWriteBuffers(32);
  150. group.setChannelReadBuffer(1024 * readBuf);
  151. group.setEventLoopSize(Util.availableProcessors() * core);
  152. group.setConcurrentFrameStack(false);
  153. if (nodelay) {
  154. context.addChannelEventListener(new ChannelEventListenerAdapter() {
  155. @Override
  156. public void channelOpened(Channel ch) throws Exception {
  157. ch.setOption(SocketOptions.TCP_NODELAY, 1);
  158. ch.setOption(SocketOptions.SO_KEEPALIVE, 0);
  159. }
  160. });
  161. }
  162. context.addProtocolCodec(codec);
  163. context.setIoEventHandle(eventHandle);
  164. context.bind(1024 * 8);
  165. }
  166. }