private void throttle(int credits) { long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); if (waitTimeMetric != null) { waitTimeMetric.update(System.nanoTime() - startNs); } }
private void throttle(int credits) { if (!rateLimited) { return; } long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); waitTimeMetric.update(System.nanoTime() - startNs); }
private void throttle(int credits) { if (!rateLimited) { return; } long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); waitTimeMetric.update(System.nanoTime() - startNs); }
private void throttle(int credits) { if (!rateLimited) { return; } long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); waitTimeMetric.update(System.nanoTime() - startNs); }
private void throttle(int credits) { if (!rateLimited) { return; } long startNs = System.nanoTime(); rateLimiter.acquire(Collections.singletonMap(tag, credits)); waitTimeMetric.update(System.nanoTime() - startNs); }
@Test(expected = IllegalArgumentException.class) public void testFailsWhenNotUsingTags() { Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", 1); tagToCredits.put("green", 1); RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(tagToCredits); initRateLimiter(rateLimiter); rateLimiter.acquire(1); }
@Test(expected = IllegalArgumentException.class) public void testFailsWhenUsingTags() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(10); initRateLimiter(rateLimiter); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", 1); tagToCredits.put("green", 1); rateLimiter.acquire(tagToCredits); }
@Test public void testAcquireWithTimeoutTagged() { RateLimiter rateLimiter = createRateLimiter(); Map<String, Integer> tagToCount = new HashMap<>(); tagToCount.put("red", 0); tagToCount.put("green", 0); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", INCREMENT); tagToCredits.put("green", INCREMENT); long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { Map<String, Integer> resultMap = rateLimiter.acquire(tagToCredits, 20, MILLISECONDS); tagToCount.put("red", tagToCount.get("red") + resultMap.get("red")); tagToCount.put("green", tagToCount.get("green") + resultMap.get("green")); } { long rate = tagToCount.get("red") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } { // Note: due to blocking, green is capped at red's QPS long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }
@Test public void testAcquireTagged() { RateLimiter rateLimiter = createRateLimiter(); Map<String, Integer> tagToCount = new HashMap<>(); tagToCount.put("red", 0); tagToCount.put("green", 0); Map<String, Integer> tagToCredits = new HashMap<>(); tagToCredits.put("red", INCREMENT); tagToCredits.put("green", INCREMENT); long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { rateLimiter.acquire(tagToCredits); tagToCount.put("red", tagToCount.get("red") + INCREMENT); tagToCount.put("green", tagToCount.get("green") + INCREMENT); } { long rate = tagToCount.get("red") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } { // Note: due to blocking, green is capped at red's QPS long rate = tagToCount.get("green") * 1000 / TEST_INTERVAL; verifyRate(rate, TARGET_RATE_PER_TASK_RED); } }
@Test public void testThrottle() { TableRateLimiter<String, String> rateLimitHelper = getThrottler(); Timer timer = mock(Timer.class); rateLimitHelper.setTimerMetric(timer); rateLimitHelper.throttle("foo"); verify(rateLimitHelper.rateLimiter, times(1)).acquire(anyMap()); verify(timer, times(1)).update(anyLong()); }
@Test @Ignore("Flaky Test.") public void testAcquireWithTimeout() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(TARGET_RATE); initRateLimiter(rateLimiter); boolean hasSeenZeros = false; int count = 0; int callCount = 0; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { ++callCount; int availableCredits = rateLimiter.acquire(INCREMENT, 20, MILLISECONDS); if (availableCredits <= 0) { hasSeenZeros = true; } else { count += INCREMENT; } } long rate = count * 1000 / TEST_INTERVAL; verifyRate(rate); junit.framework.Assert.assertTrue(Math.abs(callCount - TARGET_RATE_PER_TASK * TEST_INTERVAL / 1000 / INCREMENT) <= 2); junit.framework.Assert.assertFalse(hasSeenZeros); }
@Test @Ignore("Flaky Test: Test fails in travis.") public void testAcquire() { RateLimiter rateLimiter = new EmbeddedTaggedRateLimiter(TARGET_RATE); initRateLimiter(rateLimiter); int count = 0; long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < TEST_INTERVAL) { rateLimiter.acquire(INCREMENT); count += INCREMENT; } long rate = count * 1000 / TEST_INTERVAL; verifyRate(rate); }