Search in sources :

Example 1 with ChannelTrafficShapingHandler

use of io.netty.handler.traffic.ChannelTrafficShapingHandler in project netty by netty.

the class TrafficShapingHandlerTest method testTrafficShapping0.

/**
     *
     * @param additionalExecutor
     *            shall the pipeline add the handler using an additionnal executor
     * @param limitRead
     *            True to set Read Limit on Server side
     * @param limitWrite
     *            True to set Write Limit on Client side
     * @param globalLimit
     *            True to change Channel to Global TrafficShapping
     * @param minimalWaitBetween
     *            time in ms that should be waited before getting the final result (note: for READ the values are
     *            right shifted once, the first value being 0)
     * @param multipleMessage
     *            how many message to send at each step (for READ: the first should be 1, as the two last steps to
     *            ensure correct testing)
     * @throws Throwable
     */
private static void testTrafficShapping0(ServerBootstrap sb, Bootstrap cb, final boolean additionalExecutor, final boolean limitRead, final boolean limitWrite, final boolean globalLimit, int[] autoRead, long[] minimalWaitBetween, int[] multipleMessage) throws Throwable {
    currentTestRun++;
    logger.info("TEST: " + currentTestName + " RUN: " + currentTestRun + " Exec: " + additionalExecutor + " Read: " + limitRead + " Write: " + limitWrite + " Global: " + globalLimit);
    final ServerHandler sh = new ServerHandler(autoRead, multipleMessage);
    Promise<Boolean> promise = group.next().newPromise();
    final ClientHandler ch = new ClientHandler(promise, minimalWaitBetween, multipleMessage, autoRead);
    final AbstractTrafficShapingHandler handler;
    if (limitRead) {
        if (globalLimit) {
            handler = new GlobalTrafficShapingHandler(groupForGlobal, 0, bandwidthFactor * messageSize, check);
        } else {
            handler = new ChannelTrafficShapingHandler(0, bandwidthFactor * messageSize, check);
        }
    } else if (limitWrite) {
        if (globalLimit) {
            handler = new GlobalTrafficShapingHandler(groupForGlobal, bandwidthFactor * messageSize, 0, check);
        } else {
            handler = new ChannelTrafficShapingHandler(bandwidthFactor * messageSize, 0, check);
        }
    } else {
        handler = null;
    }
    sb.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel c) throws Exception {
            if (limitRead) {
                c.pipeline().addLast(TRAFFIC, handler);
            }
            c.pipeline().addLast(sh);
        }
    });
    cb.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel c) throws Exception {
            if (limitWrite) {
                c.pipeline().addLast(TRAFFIC, handler);
            }
            c.pipeline().addLast(ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    int totalNb = 0;
    for (int i = 1; i < multipleMessage.length; i++) {
        totalNb += multipleMessage[i];
    }
    Long start = TrafficCounter.milliSecondFromNano();
    int nb = multipleMessage[0];
    for (int i = 0; i < nb; i++) {
        cc.write(cc.alloc().buffer().writeBytes(data));
    }
    cc.flush();
    promise.await();
    Long stop = TrafficCounter.milliSecondFromNano();
    assertTrue("Error during exceution of TrafficShapping: " + promise.cause(), promise.isSuccess());
    float average = (totalNb * messageSize) / (float) (stop - start);
    logger.info("TEST: " + currentTestName + " RUN: " + currentTestRun + " Average of traffic: " + average + " compare to " + bandwidthFactor);
    sh.channel.close().sync();
    ch.channel.close().sync();
    sc.close().sync();
    if (autoRead != null) {
        // for extra release call in AutoRead
        Thread.sleep(minimalms);
    }
    if (autoRead == null && minimalWaitBetween != null) {
        assertTrue("Overall Traffic not ok since > " + maxfactor + ": " + average, average <= maxfactor);
        if (additionalExecutor) {
            // Oio is not as good when using additionalExecutor
            assertTrue("Overall Traffic not ok since < 0.25: " + average, average >= 0.25);
        } else {
            assertTrue("Overall Traffic not ok since < " + minfactor + ": " + average, average >= minfactor);
        }
    }
    if (handler != null && globalLimit) {
        ((GlobalTrafficShapingHandler) handler).release();
    }
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) AbstractTrafficShapingHandler(io.netty.handler.traffic.AbstractTrafficShapingHandler) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException) ChannelTrafficShapingHandler(io.netty.handler.traffic.ChannelTrafficShapingHandler) GlobalTrafficShapingHandler(io.netty.handler.traffic.GlobalTrafficShapingHandler)

Aggregations

Channel (io.netty.channel.Channel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 AbstractTrafficShapingHandler (io.netty.handler.traffic.AbstractTrafficShapingHandler)1 ChannelTrafficShapingHandler (io.netty.handler.traffic.ChannelTrafficShapingHandler)1 GlobalTrafficShapingHandler (io.netty.handler.traffic.GlobalTrafficShapingHandler)1 IOException (java.io.IOException)1