From c8e3c6d11ab386142c9f6e0d6126bed4c7c3853a Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Tue, 3 May 2016 11:54:34 +0200 Subject: [PATCH] Random delay when sending direct message in network stress test The delay is always longer than the one that triggers throttling. It makes not much sense for a single sending, but it will within a loop. Also made some ``Connection`` throttle constants package-private so they can be accessed by tests. --- .../io/bitsquare/p2p/network/Connection.java | 7 ++++--- .../bitsquare/p2p/network/NetworkStressTest.java | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/network/src/main/java/io/bitsquare/p2p/network/Connection.java b/network/src/main/java/io/bitsquare/p2p/network/Connection.java index f09e712895..2059b86cd8 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/Connection.java +++ b/network/src/main/java/io/bitsquare/p2p/network/Connection.java @@ -62,10 +62,11 @@ public class Connection implements MessageListener { // Static /////////////////////////////////////////////////////////////////////////////////////////// - private static final int MAX_MSG_SIZE = 500 * 1024; // 500 kb + // Leaving some constants package-private for tests to know limits. + static final int MAX_MSG_SIZE = 500 * 1024; // 500 kb //TODO decrease limits again after testing - private static final int MSG_THROTTLE_PER_SEC = 70; // With MAX_MSG_SIZE of 500kb results in bandwidth of 35 mbit/sec - private static final int MSG_THROTTLE_PER_10_SEC = 500; // With MAX_MSG_SIZE of 100kb results in bandwidth of 50 mbit/sec for 10 sec + static final int MSG_THROTTLE_PER_SEC = 70; // With MAX_MSG_SIZE of 500kb results in bandwidth of 35 mbit/sec + static final int MSG_THROTTLE_PER_10_SEC = 500; // With MAX_MSG_SIZE of 100kb results in bandwidth of 50 mbit/sec for 10 sec private static final int SOCKET_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(60); public static int getMaxMsgSize() { diff --git a/network/src/test/java/io/bitsquare/p2p/network/NetworkStressTest.java b/network/src/test/java/io/bitsquare/p2p/network/NetworkStressTest.java index 3baf279454..380834a0e0 100644 --- a/network/src/test/java/io/bitsquare/p2p/network/NetworkStressTest.java +++ b/network/src/test/java/io/bitsquare/p2p/network/NetworkStressTest.java @@ -2,6 +2,7 @@ package io.bitsquare.p2p.network; import io.bitsquare.app.Version; import io.bitsquare.common.Clock; +import io.bitsquare.common.UserThread; import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.common.crypto.KeyStorage; import io.bitsquare.common.crypto.PubKeyRing; @@ -43,6 +44,7 @@ public class NetworkStressTest { /** Numeric identifier of the regtest Bitcoin network. */ private static final int REGTEST_NETWORK_ID = 2; + /** Default number of peers in the test. */ private static final int NPEERS_DEFAULT = 4; /** Minimum number of peers for the test to work. */ @@ -52,6 +54,11 @@ public class NetworkStressTest { /** Environment variable to specify a persistent test data directory. */ private static final String TEST_DIR_ENVVAR = "STRESS_TEST_DIR"; + /** Minimum delay between direct messages in milliseconds, 25% larger than throttle limit. */ + private static long MIN_DIRECT_DELAY_MILLIS = Math.round(1.25 * (1.0 / Connection.MSG_THROTTLE_PER_SEC) * 1000); + /** Maximum delay between direct messages in milliseconds, 10 times larger than minimum. */ + private static long MAX_DIRECT_DELAY_MILLIS = 10 * MIN_DIRECT_DELAY_MILLIS; + // Instance fields /** A directory to (temporarily) hold seed and normal nodes' configuration and state files. */ @@ -195,12 +202,14 @@ public class NetworkStressTest { receivedDirectLatch.countDown(); }); - // Select a random peer and send a direct message to it. + // Select a random peer and send a direct message to it after a random delay + // not shorter than throttle limits. final int dstPeerIdx = (int) (Math.random() * nPeers); final P2PService dstPeer = peerNodes.get(dstPeerIdx); final NodeAddress dstPeerAddress = dstPeer.getAddress(); //print("sending direct message from peer %s to %s", srcPeer.getAddress(), dstPeer.getAddress()); - srcPeer.sendEncryptedDirectMessage(dstPeerAddress, peerPKRings.get(dstPeerIdx), + UserThread.runAfterRandomDelay(() -> srcPeer.sendEncryptedDirectMessage( + dstPeerAddress, peerPKRings.get(dstPeerIdx), new StressTestDirectMessage("test/" + dstPeerAddress), new SendDirectMessageListener() { @Override public void onArrived() { @@ -212,7 +221,8 @@ public class NetworkStressTest { sentDirectFailed.set(true); sentDirectLatch.countDown(); } - }); + }), MIN_DIRECT_DELAY_MILLIS, MAX_DIRECT_DELAY_MILLIS, TimeUnit.MILLISECONDS + ); } // Since receiving is completed before sending is reported to be complete, // all receiving checks should end before all sending checks to avoid deadlocking.