IGNITE-5497 aligning with absolute time ticks was added
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/64ded765 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/64ded765 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/64ded765 Branch: refs/heads/ignite-5267-1 Commit: 64ded765fcedada93cbe3836c109fba189efbb85 Parents: 5c988cb Author: Sergey Chugunov <[email protected]> Authored: Thu Jun 15 17:46:11 2017 +0300 Committer: Sergey Chugunov <[email protected]> Committed: Thu Jun 15 19:21:56 2017 +0300 ---------------------------------------------------------------------- .../database/MemoryMetricsSelfTest.java | 42 ++++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/64ded765/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MemoryMetricsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MemoryMetricsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MemoryMetricsSelfTest.java index f9a9824..7fc1035 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MemoryMetricsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MemoryMetricsSelfTest.java @@ -20,6 +20,7 @@ import java.util.concurrent.CountDownLatch; import org.apache.ignite.MemoryMetrics; import org.apache.ignite.configuration.MemoryPolicyConfiguration; import org.apache.ignite.internal.processors.cache.persistence.MemoryMetricsImpl; +import org.apache.ignite.internal.processors.cache.ratemetrics.HitRateMetrics; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import static java.lang.Thread.sleep; @@ -40,6 +41,12 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { /** */ private Thread watcherThread; + /** */ + private static final int RATE_TIME_INTERVAL_1 = 5_000; + + /** */ + private static final int RATE_TIME_INTERVAL_2 = 10_000; + /** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { MemoryPolicyConfiguration plcCfg = new MemoryPolicyConfiguration(); @@ -55,13 +62,15 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { */ public void testAllocationRateSingleThreaded() throws Exception { threadsCnt = 1; - memMetrics.rateTimeInterval(10_000); + memMetrics.rateTimeInterval(RATE_TIME_INTERVAL_2); CountDownLatch startLatch = new CountDownLatch(1); startAllocationThreads(startLatch, 340, 50); AllocationRateWatcher watcher = startWatcherThread(startLatch, 20); + alignWithTimeInterval(RATE_TIME_INTERVAL_2, 5); + startLatch.countDown(); joinAllThreads(); @@ -77,7 +86,7 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { */ public void testAllocationRateMultiThreaded() throws Exception { threadsCnt = 4; - memMetrics.rateTimeInterval(5_000); + memMetrics.rateTimeInterval(RATE_TIME_INTERVAL_1); CountDownLatch startLatch = new CountDownLatch(1); @@ -85,13 +94,13 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { AllocationRateWatcher watcher = startWatcherThread(startLatch, 20); + alignWithTimeInterval(RATE_TIME_INTERVAL_1, 5); + startLatch.countDown(); joinAllocationThreads(); - assertTrue(watcher.rateDropsCntr > 3); - - assertTrue(watcher.rateDropsCntr < 6); + assertTrue("4 or 5 rate drops must be observed: " + watcher.rateDropsCntr, watcher.rateDropsCntr == 4 || watcher.rateDropsCntr == 5); sleep(3); @@ -114,7 +123,7 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { */ public void testAllocationRateTimeIntervalConcurrentChange() throws Exception { threadsCnt = 5; - memMetrics.rateTimeInterval(5_000); + memMetrics.rateTimeInterval(RATE_TIME_INTERVAL_1); CountDownLatch startLatch = new CountDownLatch(1); @@ -122,6 +131,8 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { AllocationRateWatcher watcher = startWatcherThread(startLatch, 20); + alignWithTimeInterval(RATE_TIME_INTERVAL_1, 5); + startLatch.countDown(); for (int i = 0; i < 10; i++) { @@ -141,7 +152,7 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { */ public void testAllocationRateSubintervalsConcurrentChange() throws Exception { threadsCnt = 5; - memMetrics.rateTimeInterval(5_000); + memMetrics.rateTimeInterval(RATE_TIME_INTERVAL_1); CountDownLatch startLatch = new CountDownLatch(1); @@ -149,6 +160,8 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { AllocationRateWatcher watcher = startWatcherThread(startLatch, 20); + alignWithTimeInterval(RATE_TIME_INTERVAL_1, 5); + startLatch.countDown(); for (int i = 0; i < 10; i++) { @@ -163,6 +176,21 @@ public class MemoryMetricsSelfTest extends GridCommonAbstractTest { } /** + * As rate metrics {@link HitRateMetrics implementation} is tied to absolute time ticks + * (not related to the first hit) all tests need to align start time with this sequence of ticks. + * + * @param rateTimeInterval Rate time interval. + * @param size Size. + */ + private void alignWithTimeInterval(int rateTimeInterval, int size) throws InterruptedException { + int subIntervalLength = rateTimeInterval / size; + + long subIntCurTime = System.currentTimeMillis() % subIntervalLength; + + Thread.sleep(subIntervalLength - subIntCurTime); + } + + /** * @param startLatch Start latch. * @param watchingDelay Watching delay. */
