Search in sources :

Example 11 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project scalecube by scalecube.

the class TransportSendOrderTest method testSendOrderSingleThread.

@Test
public void testSendOrderSingleThread() throws Exception {
    server = createTransport();
    // +1 warm up iteration
    int iterationNum = 11;
    int sentPerIteration = 1000;
    long[] iterationTimeSeries = new long[iterationNum - 1];
    List<Long> totalSentTimeSeries = new ArrayList<>(sentPerIteration * (iterationNum - 1));
    for (int i = 0; i < iterationNum; i++) {
        LOGGER.info("####### {} : iteration = {}", testName.getMethodName(), i);
        List<Long> iterSentTimeSeries = new ArrayList<>(sentPerIteration);
        client = createTransport();
        final List<Message> received = new ArrayList<>();
        final CountDownLatch latch = new CountDownLatch(sentPerIteration);
        Subscriber<Message> serverSubscriber = Subscribers.create(message -> {
            received.add(message);
            latch.countDown();
        });
        server.listen().subscribe(serverSubscriber);
        long startAt = System.currentTimeMillis();
        for (int j = 0; j < sentPerIteration; j++) {
            CompletableFuture<Void> sendPromise = new CompletableFuture<>();
            long sentAt = System.currentTimeMillis();
            client.send(server.address(), Message.fromQualifier("q" + j), sendPromise);
            sendPromise.whenComplete((aVoid, exception) -> {
                if (exception == null) {
                    long sentTime = System.currentTimeMillis() - sentAt;
                    iterSentTimeSeries.add(sentTime);
                } else {
                    LOGGER.error("Failed to send message in {} ms", System.currentTimeMillis() - sentAt, exception);
                }
            });
        }
        latch.await(20, TimeUnit.SECONDS);
        long iterationTime = System.currentTimeMillis() - startAt;
        if (i > 0) {
            // exclude warm up iteration
            iterationTimeSeries[i - 1] = iterationTime;
        }
        assertSendOrder(sentPerIteration, received);
        // await a bit for last msg confirmation
        Thread.sleep(10);
        LongSummaryStatistics iterSentTimeStats = iterSentTimeSeries.stream().mapToLong(v -> v).summaryStatistics();
        if (i == 0) {
            // warm up iteration
            LOGGER.info("Warm up iteration time: {} ms", iterationTime);
            LOGGER.info("Sent time stats warm up iter (ms): {}", iterSentTimeStats);
        } else {
            totalSentTimeSeries.addAll(iterSentTimeSeries);
            LongSummaryStatistics totalSentTimeStats = totalSentTimeSeries.stream().mapToLong(v -> v).summaryStatistics();
            LOGGER.info("Iteration time: {} ms", iterationTime);
            LOGGER.info("Sent time stats iter  (ms): {}", iterSentTimeStats);
            LOGGER.info("Sent time stats total (ms): {}", totalSentTimeStats);
        }
        serverSubscriber.unsubscribe();
        destroyTransport(client);
    }
    LongSummaryStatistics iterationTimeStats = LongStream.of(iterationTimeSeries).summaryStatistics();
    LOGGER.info("Iteration time stats (ms): {}", iterationTimeStats);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Logger(org.slf4j.Logger) LongStream(java.util.stream.LongStream) Subscriber(rx.Subscriber) LoggerFactory(org.slf4j.LoggerFactory) TransportTestUtils.destroyTransport(io.scalecube.transport.TransportTestUtils.destroyTransport) Test(org.junit.Test) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) TransportTestUtils.createTransport(io.scalecube.transport.TransportTestUtils.createTransport) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Future(java.util.concurrent.Future) After(org.junit.After) BaseTest(io.scalecube.testlib.BaseTest) Assert.assertEquals(org.junit.Assert.assertEquals) Subscribers(rx.observers.Subscribers) LongSummaryStatistics(java.util.LongSummaryStatistics) ExecutorService(java.util.concurrent.ExecutorService) Throwables.propagate(com.google.common.base.Throwables.propagate) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LongSummaryStatistics(java.util.LongSummaryStatistics) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 12 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project scalecube by scalecube.

the class TransportSendOrderTest method testSendOrderSingleThreadWithoutPromises.

@Test
public void testSendOrderSingleThreadWithoutPromises() throws Exception {
    server = createTransport();
    // +1 warm up iteration
    int iterationNum = 11;
    int sentPerIteration = 1000;
    long[] iterationTimeSeries = new long[iterationNum - 1];
    for (int i = 0; i < iterationNum; i++) {
        LOGGER.info("####### {} : iteration = {}", testName.getMethodName(), i);
        client = createTransport();
        final List<Message> received = new ArrayList<>();
        final CountDownLatch latch = new CountDownLatch(sentPerIteration);
        Subscriber<Message> serverSubscriber = Subscribers.create(message -> {
            received.add(message);
            latch.countDown();
        });
        server.listen().subscribe(serverSubscriber);
        long startAt = System.currentTimeMillis();
        for (int j = 0; j < sentPerIteration; j++) {
            client.send(server.address(), Message.fromQualifier("q" + j));
        }
        latch.await(20, TimeUnit.SECONDS);
        long iterationTime = System.currentTimeMillis() - startAt;
        if (i > 0) {
            // exclude warm up iteration
            iterationTimeSeries[i - 1] = iterationTime;
        }
        assertSendOrder(sentPerIteration, received);
        LOGGER.info("Iteration time: {} ms", iterationTime);
        serverSubscriber.unsubscribe();
        destroyTransport(client);
    }
    LongSummaryStatistics iterationTimeStats = LongStream.of(iterationTimeSeries).summaryStatistics();
    LOGGER.info("Iteration time stats (ms): {}", iterationTimeStats);
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 13 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project scalecube by scalecube.

the class TransportStressTest method transportStressTest.

@Test
public void transportStressTest() throws Exception {
    // Init transports
    Transport echoServer = Transport.bindAwait();
    Transport client1 = null;
    // Init measured params
    long sentTime = 0;
    long receivedTime = 0;
    LongSummaryStatistics rttStats = null;
    // Run experiment
    try {
        // Subscribe echo server handler
        echoServer.listen().subscribe(msg -> echoServer.send(msg.sender(), msg));
        // Init client
        CountDownLatch measureLatch = new CountDownLatch(msgCount);
        ArrayList<Long> rttRecords = new ArrayList<>(msgCount);
        client1 = Transport.bindAwait();
        client1.listen().subscribe(msg -> {
            long sentAt = Long.valueOf(msg.data());
            long rttTime = System.currentTimeMillis() - sentAt;
            rttRecords.add(rttTime);
            measureLatch.countDown();
        });
        // Measure
        long startAt = System.currentTimeMillis();
        for (int i = 0; i < msgCount; i++) {
            client1.send(echoServer.address(), Message.fromData(Long.toString(System.currentTimeMillis())));
        }
        sentTime = System.currentTimeMillis() - startAt;
        measureLatch.await(timeoutSeconds, TimeUnit.SECONDS);
        receivedTime = System.currentTimeMillis() - startAt;
        rttStats = rttRecords.stream().mapToLong(v -> v).summaryStatistics();
        assertTrue(measureLatch.getCount() == 0);
    } finally {
        // Print results
        LOGGER.info("Finished sending {} messages in {} ms", msgCount, sentTime);
        LOGGER.info("Finished receiving {} messages in {} ms", msgCount, receivedTime);
        LOGGER.info("Round trip stats (ms): {}", rttStats);
        // Destroy transport
        destroyTransport(echoServer);
        destroyTransport(client1);
    }
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) ArrayList(java.util.ArrayList) TransportTestUtils.destroyTransport(io.scalecube.transport.TransportTestUtils.destroyTransport) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 14 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project scalecube by scalecube.

the class GossipProtocolTest method testGossipProtocol.

@Test
public void testGossipProtocol() throws Exception {
    // Init gossip protocol instances
    List<GossipProtocolImpl> gossipProtocols = initGossipProtocols(membersNum, lossPercent, meanDelay);
    // Subscribe on gossips
    long disseminationTime = 0;
    LongSummaryStatistics messageSentStatsDissemination = null;
    LongSummaryStatistics messageLostStatsDissemination = null;
    LongSummaryStatistics messageSentStatsOverall = null;
    LongSummaryStatistics messageLostStatsOverall = null;
    long gossipTimeout = ClusterMath.gossipTimeoutToSweep(gossipRepeatMultiplier, membersNum, gossipInterval);
    try {
        final String gossipData = "test gossip - " + ThreadLocalRandom.current().nextLong();
        final CountDownLatch latch = new CountDownLatch(membersNum - 1);
        final Map<Member, Member> receivers = new ConcurrentHashMap<>();
        final AtomicBoolean doubleDelivery = new AtomicBoolean(false);
        for (final GossipProtocolImpl protocol : gossipProtocols) {
            protocol.listen().subscribe(gossip -> {
                if (gossipData.equals(gossip.data())) {
                    boolean firstTimeAdded = receivers.put(protocol.getMember(), protocol.getMember()) == null;
                    if (firstTimeAdded) {
                        latch.countDown();
                    } else {
                        LOGGER.error("Delivered gossip twice to: {}", protocol.getTransport().address());
                        doubleDelivery.set(true);
                    }
                }
            });
        }
        // Spread gossip, measure and verify delivery metrics
        long start = System.currentTimeMillis();
        gossipProtocols.get(0).spread(Message.fromData(gossipData));
        // Await for double gossip timeout
        latch.await(2 * gossipTimeout, TimeUnit.MILLISECONDS);
        disseminationTime = System.currentTimeMillis() - start;
        messageSentStatsDissemination = computeMessageSentStats(gossipProtocols);
        if (lossPercent > 0) {
            messageLostStatsDissemination = computeMessageLostStats(gossipProtocols);
        }
        Assert.assertEquals("Not all members received gossip", membersNum - 1, receivers.size());
        Assert.assertTrue("Too long dissemination time " + disseminationTime + "ms (timeout " + gossipTimeout + "ms)", disseminationTime < gossipTimeout);
        // Await gossip lifetime plus few gossip intervals too ensure gossip is fully spread
        if (awaitFullCompletion) {
            long awaitCompletionTime = gossipTimeout - disseminationTime + 3 * gossipInterval;
            Thread.sleep(awaitCompletionTime);
            messageSentStatsOverall = computeMessageSentStats(gossipProtocols);
            if (lossPercent > 0) {
                messageLostStatsOverall = computeMessageLostStats(gossipProtocols);
            }
        }
        Assert.assertFalse("Delivered gossip twice to same member", doubleDelivery.get());
    } finally {
        // Print theoretical results
        LOGGER.info("Experiment params: N={}, Gfanout={}, Grepeat_mult={}, Tgossip={}ms Ploss={}%, Tmean={}ms", membersNum, gossipFanout, gossipRepeatMultiplier, gossipInterval, lossPercent, meanDelay);
        double convergProb = gossipConvergencePercent(gossipFanout, gossipRepeatMultiplier, membersNum, lossPercent);
        long expDissemTime = gossipDisseminationTime(gossipRepeatMultiplier, membersNum, gossipInterval);
        int maxMsgPerNode = maxMessagesPerGossipPerNode(gossipFanout, gossipRepeatMultiplier, membersNum);
        int maxMsgTotal = maxMessagesPerGossipTotal(gossipFanout, gossipRepeatMultiplier, membersNum);
        LOGGER.info("Expected dissemination time is {}ms with probability {}%", expDissemTime, convergProb);
        LOGGER.info("Max messages sent per node {} and total {}", maxMsgPerNode, maxMsgTotal);
        // Print actual results
        LOGGER.info("Actual dissemination time: {}ms (timeout {}ms)", disseminationTime, gossipTimeout);
        LOGGER.info("Messages sent stats (diss.): {}", messageSentStatsDissemination);
        if (lossPercent > 0) {
            LOGGER.info("Messages lost stats (diss.): {}", messageLostStatsDissemination);
        }
        if (awaitFullCompletion) {
            LOGGER.info("Messages sent stats (total): {}", messageSentStatsOverall);
            if (lossPercent > 0) {
                LOGGER.info("Messages lost stats (total): {}", messageLostStatsOverall);
            }
        }
        // Destroy gossip protocol instances
        destroyGossipProtocols(gossipProtocols);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LongSummaryStatistics(java.util.LongSummaryStatistics) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Member(io.scalecube.cluster.Member) BaseTest(io.scalecube.testlib.BaseTest) Test(org.junit.Test)

Example 15 with LongSummaryStatistics

use of java.util.LongSummaryStatistics in project scalecube by scalecube.

the class GossipProtocolTest method computeMessageLostStats.

private LongSummaryStatistics computeMessageLostStats(List<GossipProtocolImpl> gossipProtocols) {
    List<Long> messageLostPerNode = new ArrayList<>(gossipProtocols.size());
    for (GossipProtocolImpl gossipProtocol : gossipProtocols) {
        Transport transport = gossipProtocol.getTransport();
        messageLostPerNode.add(transport.networkEmulator().totalMessageLostCount());
    }
    return messageLostPerNode.stream().mapToLong(v -> v).summaryStatistics();
}
Also used : Arrays(java.util.Arrays) Address(io.scalecube.transport.Address) MembershipProtocol(io.scalecube.cluster.membership.MembershipProtocol) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ClusterConfig(io.scalecube.cluster.ClusterConfig) ClusterMath.maxMessagesPerGossipTotal(io.scalecube.cluster.ClusterMath.maxMessagesPerGossipTotal) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Transport(io.scalecube.transport.Transport) Message(io.scalecube.transport.Message) DummyMembershipProtocol(io.scalecube.cluster.membership.DummyMembershipProtocol) BaseTest(io.scalecube.testlib.BaseTest) ClusterMath.gossipConvergencePercent(io.scalecube.cluster.ClusterMath.gossipConvergencePercent) Parameterized(org.junit.runners.Parameterized) LongSummaryStatistics(java.util.LongSummaryStatistics) ClusterMath(io.scalecube.cluster.ClusterMath) Logger(org.slf4j.Logger) ClusterMath.maxMessagesPerGossipPerNode(io.scalecube.cluster.ClusterMath.maxMessagesPerGossipPerNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Member(io.scalecube.cluster.Member) TransportConfig(io.scalecube.transport.TransportConfig) Assert(org.junit.Assert) ClusterMath.gossipDisseminationTime(io.scalecube.cluster.ClusterMath.gossipDisseminationTime) ArrayList(java.util.ArrayList) Transport(io.scalecube.transport.Transport)

Aggregations

LongSummaryStatistics (java.util.LongSummaryStatistics)47 Test (org.junit.Test)18 List (java.util.List)15 Map (java.util.Map)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ArrayList (java.util.ArrayList)10 Collectors (java.util.stream.Collectors)10 Set (java.util.Set)9 TimeUnit (java.util.concurrent.TimeUnit)9 LoggerFactory (org.slf4j.LoggerFactory)9 HashMap (java.util.HashMap)8 Logger (org.slf4j.Logger)8 BaseTest (io.scalecube.testlib.BaseTest)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Arrays (java.util.Arrays)5 Stream (java.util.stream.Stream)5 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 Entry (java.util.Map.Entry)4