Search in sources :

Example 6 with Channel

use of com.firenio.component.Channel 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 7 with Channel

use of com.firenio.component.Channel 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 8 with Channel

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

the class TestLoadClient method main.

public static void main(String[] args) throws Exception {
    final Logger logger = LoggerFactory.getLogger(TestLoadClient.class);
    final CountDownLatch latch = new CountDownLatch(time);
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
        // latch.countDown();
        // long count = latch.getCount();
        // if (count % 10 == 0) {
        // if (count < 50) {
        // logger.info("************************================" + count);
        // }
        // }
        // logger.info("res==========={}",res.getAndIncrement());
        }
    };
    ChannelConnector context = new ChannelConnector(8300);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect();
    System.out.println("################## Test start ####################");
    long old = Util.now();
    for (int i = 0; i < time; i++) {
        LengthValueFrame frame = new LengthValueFrame();
        frame.write("hello server!", ch);
        ch.writeAndFlush(frame);
    }
    latch.await();
    long spend = (Util.past(old));
    System.out.println("## Execute Time:" + time);
    System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
    System.out.println("## Expend Time:" + spend);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Logger(com.firenio.log.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) BigDecimal(java.math.BigDecimal) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelConnector(com.firenio.component.ChannelConnector)

Example 9 with Channel

use of com.firenio.component.Channel 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 10 with Channel

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

the class TestSimpleHttpClient method main.

@Test
public void main() throws Exception {
    Options.setEnableEpoll(true);
    Waiter<String> w = new Waiter<>();
    ChannelConnector context = new ChannelConnector("firenio.com", 443);
    SslContext sslContext = SslContextBuilder.forClient(true).build();
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            ClientHttpFrame res = (ClientHttpFrame) frame;
            System.out.println();
            System.out.println(new String(res.getBytesContent()));
            System.out.println();
            Util.close(context);
            w.call(new String(res.getBytesContent()), null);
        }
    });
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setSslContext(sslContext);
    long start = Util.now();
    Channel ch = context.connect(3000);
    ch.writeAndFlush(new ClientHttpFrame("/test?p=2222"));
    w.await(3000);
    System.out.println(Util.past(start));
    Assert.assertEquals("yes server already accept your message :) {p=2222}", w.getResponse());
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) Waiter(com.firenio.concurrent.Waiter) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) SslContext(com.firenio.component.SslContext) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) Test(org.junit.Test)

Aggregations

Channel (com.firenio.component.Channel)33 Frame (com.firenio.component.Frame)28 IoEventHandle (com.firenio.component.IoEventHandle)27 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)26 ChannelConnector (com.firenio.component.ChannelConnector)19 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)15 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)11 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)10 ChannelAcceptor (com.firenio.component.ChannelAcceptor)9 ByteBuf (com.firenio.buffer.ByteBuf)5 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)5 HttpFrame (com.firenio.codec.http11.HttpFrame)5 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)5 Waiter (com.firenio.concurrent.Waiter)5 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)4 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)4 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)4 ChannelEventListenerAdapter (com.firenio.component.ChannelEventListenerAdapter)4 WebSocketFrame (com.firenio.codec.http11.WebSocketFrame)3 HttpCodec (com.firenio.codec.http11.HttpCodec)2