Search in sources :

Example 1 with RateLimiter

use of com.linkedin.r2.transport.http.client.RateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testSimple.

@Test
public void testSimple() throws Exception {
    final int total = 10;
    // NB on Solaris x86 there seems to be an extra 10ms that gets added to the period; need
    // to figure this out.  For now set the period high enough that period + 10 will be within
    // the tolerance.
    final int period = 100;
    final CountDownLatch latch = new CountDownLatch(total);
    final Task incr = new Task() {

        @Override
        public void run(SimpleCallback doneCallback) {
            latch.countDown();
            doneCallback.onDone();
        }
    };
    RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor);
    limiter.setPeriod(period);
    long start = System.currentTimeMillis();
    long lowTolerance = (total * period) * 4 / 5;
    long highTolerance = (total * period) * 5 / 4;
    for (int i = 0; i < total * period; i++) {
        limiter.submit(incr);
    }
    Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
    long t = System.currentTimeMillis() - start;
    Assert.assertTrue(t > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + t + ")");
}
Also used : Task(com.linkedin.r2.transport.http.client.RateLimiter.Task) CountDownLatch(java.util.concurrent.CountDownLatch) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) SimpleCallback(com.linkedin.common.callback.SimpleCallback) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Example 2 with RateLimiter

use of com.linkedin.r2.transport.http.client.RateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testSetPeriod.

@Test
public void testSetPeriod() throws Exception {
    final RateLimiter rl = new ExponentialBackOffRateLimiter(0, MAXPERIOD, 20, _executor);
    final ScheduledFuture<?> future = _executor.scheduleAtFixedRate(new Runnable() {

        private int _period = 300;

        @Override
        public void run() {
            rl.setPeriod(_period);
            _period += 50;
        }
    }, 0, 100, TimeUnit.MILLISECONDS);
    RateLimiterRunner runner = new RateLimiterRunner(System.currentTimeMillis());
    rl.submit(runner);
    Assert.assertTrue(runner.getElapsedTime() <= MAXPERIOD, "Elapsed Time exceed MAX Period");
    future.cancel(false);
}
Also used : ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Example 3 with RateLimiter

use of com.linkedin.r2.transport.http.client.RateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testMaxRunningTasks.

@Test
public void testMaxRunningTasks() throws Exception {
    final int total = 20;
    final int maxRunning = 5;
    final int period = 100;
    final Random rand = new Random();
    final CountDownLatch latch = new CountDownLatch(total);
    final AtomicInteger totalStarted = new AtomicInteger();
    final AtomicInteger totalFinished = new AtomicInteger();
    final Task r = new Task() {

        @Override
        public void run(final SimpleCallback callback) {
            totalStarted.incrementAndGet();
            int delay = period + rand.nextInt(period);
            _executor.schedule(new Runnable() {

                @Override
                public void run() {
                    totalFinished.incrementAndGet();
                    callback.onDone();
                }
            }, delay, TimeUnit.MILLISECONDS);
            latch.countDown();
        }
    };
    RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor, maxRunning);
    limiter.setPeriod(period);
    for (int i = 0; i < total; ++i) {
        limiter.submit(r);
    }
    // check the current number of concurrent tasks every 100ms.
    for (int i = 0; i < total * 2; ++i) {
        int currentRunning = totalStarted.get() - totalFinished.get();
        Assert.assertTrue(currentRunning <= maxRunning, "Should have less than " + maxRunning + " concurrent tasks");
        Thread.sleep(period);
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(total, totalStarted.get());
}
Also used : Task(com.linkedin.r2.transport.http.client.RateLimiter.Task) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) SimpleCallback(com.linkedin.common.callback.SimpleCallback) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Aggregations

ExponentialBackOffRateLimiter (com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter)3 RateLimiter (com.linkedin.r2.transport.http.client.RateLimiter)3 Test (org.testng.annotations.Test)3 SimpleCallback (com.linkedin.common.callback.SimpleCallback)2 Task (com.linkedin.r2.transport.http.client.RateLimiter.Task)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1