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);
}
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);
}
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);
}
}
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);
}
}
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();
}
Aggregations