Skip to content

Instantly share code, notes, and snippets.

@jsanda
Last active August 29, 2015 14:07
Show Gist options
  • Save jsanda/4cb39f5119bb85f94ce5 to your computer and use it in GitHub Desktop.
Save jsanda/4cb39f5119bb85f94ce5 to your computer and use it in GitHub Desktop.
public class RateLimitTest {
@Test
public void throttle() {
RateLimiter rateLimiter = RateLimiter.create(5, 5, TimeUnit.SECONDS);
for (int i = 0; i < 25; ++i) {
rateLimiter.acquire();
System.out.println("i = " + i);
}
rateLimiter.setRate(1);
for (int i = 25; i < 50; ++i) {
rateLimiter.acquire();
System.out.println("i = " + i);
}
rateLimiter = RateLimiter.create(5, 10, TimeUnit.SECONDS);
for (int i = 51; i < 100; ++i) {
rateLimiter.acquire();
System.out.println("i = " + i);
}
}
}
@jsanda
Copy link
Author

jsanda commented Oct 3, 2014

This test exhibits the behavior I expected. The output is noticeably faster after a few second until i == 25, at which point the output is much slower. Once i == 51 though, the output does start to speed back up. I think this will work for ramping back up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment