Search in sources :

Example 1 with NioEventLoopGroup

use of com.firenio.component.NioEventLoopGroup in project baseio by generallycloud.

the class TestHttpLoadServerTFB method main.

public static void main(String[] args) throws Exception {
    System.setProperty("core", "1");
    System.setProperty("frame", "16");
    System.setProperty("readBuf", "512");
    System.setProperty("pool", "true");
    System.setProperty("inline", "true");
    System.setProperty("level", "1");
    System.setProperty("read", "false");
    System.setProperty("epoll", "true");
    System.setProperty("nodelay", "true");
    System.setProperty("cachedurl", "true");
    System.setProperty("unsafeBuf", "false");
    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 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.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("readBuf: {}", readBuf);
    DebugUtil.info("nodelay: {}", nodelay);
    DebugUtil.info("cachedurl: {}", cachedurl);
    DebugUtil.info("unsafeBuf: {}", unsafeBuf);
    int processors = Util.availableProcessors() * core;
    int fcache = 1024 * 16;
    int pool_unit = 256 * 16;
    int pool_cap = 1024 * 8 * pool_unit * processors;
    String server = "firenio";
    ByteTree cachedUrls = null;
    if (cachedurl) {
        cachedUrls = new ByteTree();
        cachedUrls.add("/plaintext");
        cachedUrls.add("/json");
    }
    HttpCodec codec = new HttpCodec(server, fcache, lite, cachedUrls) {

        @Override
        protected Object newAttachment() {
            return new MyHttpAttachment();
        }
    };
    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)) {
                MyHttpAttachment att = (MyHttpAttachment) ch.getAttachment();
                ByteBuf buf = att.write_buf;
                if (buf == null) {
                    buf = ch.allocate();
                    ByteBuf temp = buf;
                    att.write_buf = buf;
                    ch.getEventLoop().submit(() -> {
                        ch.writeAndFlush(temp);
                        att.write_buf = null;
                    });
                }
                f.setContent(STATIC_PLAINTEXT);
                f.setContentType(HttpContentType.text_plain);
                f.setConnection(HttpConnection.NONE);
                f.setDate(HttpDateUtil.getDateLine());
                codec.encode(ch, buf, f);
                codec.release(ch.getEventLoop(), f);
            } else if ("/json".equals(action)) {
                ByteBuf temp = FastThreadLocal.get().getAttribute(JSON_BUF);
                if (temp == null) {
                    temp = ByteBuf.heap(0);
                    FastThreadLocal.get().setAttribute(JSON_BUF, temp);
                }
                JsonStream stream = JsonStreamPool.borrowJsonStream();
                try {
                    stream.reset(null);
                    stream.writeVal(Message.class, new Message("Hello, World!"));
                    Slice slice = stream.buffer();
                    temp.reset(slice.data(), slice.head(), slice.tail());
                    f.setContent(temp);
                    f.setContentType(HttpContentType.application_json);
                    f.setConnection(HttpConnection.NONE);
                    f.setDate(HttpDateUtil.getDateLine());
                    ch.writeAndFlush(f);
                    ch.release(f);
                } finally {
                    JsonStreamPool.returnJsonStream(stream);
                }
            } else {
                System.err.println("404");
                f.setString("404,page not found!", ch);
                f.setContentType(HttpContentType.text_plain);
                f.setStatus(HttpStatus.C404);
                f.setDate(HttpDateUtil.getDateLine());
                ch.writeAndFlush(f);
                ch.release(f);
            }
        }
    };
    HttpDateUtil.start();
    NioEventLoopGroup group = new NioEventLoopGroup();
    ChannelAcceptor context = new ChannelAcceptor(group, 8081);
    group.setMemoryCapacity(pool_cap);
    group.setEnableMemoryPool(pool);
    group.setMemoryUnit(pool_unit);
    group.setWriteBuffers(8);
    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.SO_KEEPALIVE, 0);
            }
        });
    }
    context.addProtocolCodec(codec);
    context.setIoEventHandle(eventHandle);
    context.bind(1024 * 8);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) HttpFrame(com.firenio.codec.http11.HttpFrame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) JsonStream(com.jsoniter.output.JsonStream) ByteBuf(com.firenio.buffer.ByteBuf) HttpFrame(com.firenio.codec.http11.HttpFrame) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) ByteTree(com.firenio.collection.ByteTree) HttpCodec(com.firenio.codec.http11.HttpCodec) Slice(com.jsoniter.spi.Slice) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 2 with NioEventLoopGroup

use of com.firenio.component.NioEventLoopGroup in project baseio by generallycloud.

the class TestLoadClient method test.

public static void test(TestLoadRound round) throws Exception {
    final String host = round.host;
    final ByteBuf buf = round.request_buf;
    final int port = round.port;
    final int pipes = round.pipes;
    final int threads = round.threads;
    final int requests = round.requests;
    final int connections = round.connections;
    final int batch = requests / pipes;
    DebugUtil.info("requests:" + requests);
    DebugUtil.info("threads:" + threads);
    DebugUtil.info("connections:" + connections);
    DebugUtil.info("pipes:" + pipes);
    DebugUtil.info("batch:" + batch);
    NioEventLoopGroup g = new NioEventLoopGroup(true, threads, Integer.MAX_VALUE);
    g.start();
    Channel[] chs = new Channel[connections];
    DebugUtil.info("build connections...");
    CountDownLatch c_latch = new CountDownLatch(connections);
    CountDownLatch b_latch = new CountDownLatch(batch);
    AtomicInteger c_complete = new AtomicInteger();
    long last = Util.now();
    for (int i = 0; i < connections; i++) {
        ChannelConnector context = new ChannelConnector(g.getNext(), host, port);
        context.setPrintConfig(false);
        context.addProtocolCodec(new ClientHttpCodec());
        context.setIoEventHandle(new IoEventHandle() {

            int c = 0;

            int b = batch / connections;

            @Override
            public void accept(Channel ch, Frame frame) {
                if (++c == pipes) {
                    c = 0;
                    b_latch.countDown();
                    if (--b > 0) {
                        ch.writeAndFlush(buf.duplicate());
                    } else {
                        DebugUtil.info("c complate......" + c_complete.incrementAndGet());
                    }
                }
            }
        });
        final int i_copy = i;
        context.connect((ch, ex) -> {
            if (ex != null) {
                ex.printStackTrace();
            }
            chs[i_copy] = ch;
            c_latch.countDown();
        }, 9000);
    }
    c_latch.await();
    DebugUtil.info("build connections cost:" + (Util.now() - last));
    DebugUtil.info("start request...");
    last = Util.now();
    for (int i = 0; i < chs.length; i++) {
        chs[i].writeAndFlush(buf.duplicate());
    }
    b_latch.await();
    long cost = (Util.now() - last);
    DebugUtil.info("request cost:" + cost);
    DebugUtil.info("request rps:" + (requests * 1000d / cost));
    for (int i = 0; i < chs.length; i++) {
        chs[i].close();
    }
    g.stop();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ByteBuf(com.firenio.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 3 with NioEventLoopGroup

use of com.firenio.component.NioEventLoopGroup in project baseio by generallycloud.

the class TestLoadClient1 method prepare.

@Override
public void prepare() throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            addCount(80000);
            if (debug) {
                count.decrementAndGet();
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(5120000 * 256 * CLIENT_CORE_SIZE);
    group.setMemoryUnit(256);
    group.setWriteBuffers(TestLoadServer.WRITE_BUFFERS);
    group.setEnableMemoryPool(TestLoadServer.ENABLE_POOL);
    context = new ChannelConnector(group, "127.0.0.1", 8300);
    context.setIoEventHandle(eventHandleAdaptor);
    if (TestLoadServer.ENABLE_SSL) {
        context.setSslContext(SslContextBuilder.forClient(true).build());
    }
    context.setPrintConfig(false);
    context.addProtocolCodec(new LengthValueCodec());
    if (TestLoadServer.ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256));
    }
    context.connect(6000);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 4 with NioEventLoopGroup

use of com.firenio.component.NioEventLoopGroup in project baseio by generallycloud.

the class TestLoadServer method main.

public static void main(String[] args) throws Exception {
    Options.setBufAutoExpansion(AUTO_EXPANSION);
    Options.setEnableEpoll(ENABLE_EPOLL);
    Options.setEnableUnsafeBuf(ENABLE_UNSAFE_BUF);
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            String text = f.getStringContent();
            if (BUFFERED_WRITE) {
                ByteBuf buf = ch.getAttribute(WRITE_BUF);
                if (buf == null) {
                    buf = ch.allocate();
                    ByteBuf temp = buf;
                    ch.setAttribute(WRITE_BUF, buf);
                    ch.getEventLoop().submit(() -> {
                        ch.writeAndFlush(temp);
                        ch.setAttribute(WRITE_BUF, null);
                    });
                }
                byte[] data = text.getBytes(ch.getCharset());
                buf.writeInt(data.length);
                buf.writeBytes(data);
            } else {
                f.setString(text, ch);
                ch.writeAndFlush(f);
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup(SERVER_CORE_SIZE);
    group.setMemoryCapacity(1024 * 512 * MEM_UNIT * SERVER_CORE_SIZE);
    group.setWriteBuffers(WRITE_BUFFERS);
    group.setMemoryUnit(MEM_UNIT);
    group.setEnableMemoryPool(ENABLE_POOL);
    ChannelAcceptor context = new ChannelAcceptor(group, 8300);
    context.addProtocolCodec(new LengthValueCodec());
    context.setIoEventHandle(eventHandle);
    if (ENABLE_SSL) {
    // context.setSslPem("localhost.key;localhost.crt");
    }
    context.addChannelEventListener(new LoggerChannelOpenListener());
    if (ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256 * CLIENT_CORE_SIZE));
    }
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ByteBuf(com.firenio.buffer.ByteBuf) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 5 with NioEventLoopGroup

use of com.firenio.component.NioEventLoopGroup in project baseio by generallycloud.

the class TestHttp2Server method main.

public static void main(String[] args) throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            frame.write("Hello World", ch);
            ch.writeAndFlush(frame);
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(1024 * 1024 * 4);
    group.setMemoryUnit(512);
    group.setEnableMemoryPool(true);
    ChannelAcceptor context = new ChannelAcceptor(group, 443);
    context.addProtocolCodec(new Http2Codec());
    context.setIoEventHandle(eventHandleAdaptor);
    // context.setApplicationProtocols(new String[]{"h2", "http/1.1"});
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)11 Channel (com.firenio.component.Channel)10 Frame (com.firenio.component.Frame)9 IoEventHandle (com.firenio.component.IoEventHandle)9 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)7 ChannelAcceptor (com.firenio.component.ChannelAcceptor)5 ChannelConnector (com.firenio.component.ChannelConnector)5 ByteBuf (com.firenio.buffer.ByteBuf)3 HttpCodec (com.firenio.codec.http11.HttpCodec)3 HttpFrame (com.firenio.codec.http11.HttpFrame)3 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)3 ThreadEventLoopGroup (com.firenio.concurrent.ThreadEventLoopGroup)3 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)2 Http2Codec (com.firenio.codec.http2.Http2Codec)2 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)2 ChannelActiveListener (com.firenio.component.ChannelActiveListener)2 LifeCycle (com.firenio.LifeCycle)1 LifeCycleListener (com.firenio.LifeCycleListener)1 ByteBufAllocatorGroup (com.firenio.buffer.ByteBufAllocatorGroup)1