Last active
August 9, 2017 01:05
-
-
Save rdhabalia/324519648b8a1008ef49d30c8f4a8bf2 to your computer and use it in GitHub Desktop.
RateLimiting Comparision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why Custom Rate-limiter? | |
A. Usecase: Per second rate-limiting not satisfied by Guava-RateLimiter | |
(1) Guava RateLimiter: For X permits: it releases X/1000 permits every msec. therefore, for permits=2/sec => | |
it release 1st permit on first 500msec and 2nd permit on next 500ms. therfore, if 2 request comes with in 500msec duration then | |
2nd request fails to acquire permit though we have configured 2 permits/second. | |
https://github.com/google/guava/blob/v15.0/guava/src/com/google/common/util/concurrent/RateLimiter.java#L102 | |
(2) Custon RateLimiter: it releases X permits every second. so, in above usecase: if 2 requests comes at the same time then both will | |
acquire the permit. | |
B. Custom-RateLimiter is light-weight and faster than Guava-RateLimiter | |
@Test | |
public void compareRateLimiter() { | |
final int permitsPerSecond = 1000; | |
com.google.common.util.concurrent.RateLimiter guavaRateLimiter = com.google.common.util.concurrent.RateLimiter | |
.create(permitsPerSecond); | |
org.apache.pulsar.common.util.RateLimiter rateLimiter = new org.apache.pulsar.common.util.RateLimiter( | |
permitsPerSecond, 1, TimeUnit.SECONDS); | |
Assert.assertTrue(rateLimiter.tryAcquire()); | |
Assert.assertTrue(guavaRateLimiter.tryAcquire()); | |
final int totalRound = 50; | |
long totalGavaRateLimiterTime = 0; | |
long totalCustomRateLimiterTime = 0; | |
for (int i = 0; i < totalRound; i++) { | |
long time = runGuavaRateLimiter(guavaRateLimiter, permitsPerSecond); | |
System.out.println("Guava rate-limiter = " + time); | |
totalGavaRateLimiterTime+=time; | |
} | |
System.out.println("*****************************"); | |
for (int i = 0; i < totalRound; i++) { | |
long time = runCustomRateLimiter(rateLimiter, permitsPerSecond); | |
System.out.println("Custom rate-limiter= " + time); | |
totalCustomRateLimiterTime+=time; | |
} | |
System.out.println("*****************************"); | |
System.out.println("Avg-Guava ="+(totalGavaRateLimiterTime/totalRound)); | |
System.out.println("Avg-Custom="+(totalCustomRateLimiterTime/totalRound)); | |
} | |
private long runCustomRateLimiter(org.apache.pulsar.common.util.RateLimiter rateLimiter, | |
int permitsPerSecond) { | |
long start = System.nanoTime(); | |
for (int i = 0; i < permitsPerSecond; i++) { | |
rateLimiter.tryAcquire(); | |
} | |
return System.nanoTime() - start; | |
} | |
private long runGuavaRateLimiter(com.google.common.util.concurrent.RateLimiter rateLimiter, | |
int permitsPerSecond) { | |
long start = System.nanoTime(); | |
for (int i = 0; i < permitsPerSecond; i++) { | |
rateLimiter.tryAcquire(); | |
} | |
return System.nanoTime() - start; | |
} | |
Output: | |
Guava rate-limiter = 816142 | |
Guava rate-limiter = 390226 | |
Guava rate-limiter = 181257 | |
Guava rate-limiter = 203298 | |
Guava rate-limiter = 135213 | |
Guava rate-limiter = 117949 | |
Guava rate-limiter = 151119 | |
Guava rate-limiter = 155849 | |
Guava rate-limiter = 136992 | |
Guava rate-limiter = 255227 | |
Guava rate-limiter = 159248 | |
Guava rate-limiter = 148158 | |
Guava rate-limiter = 180691 | |
Guava rate-limiter = 135304 | |
Guava rate-limiter = 147782 | |
Guava rate-limiter = 138417 | |
Guava rate-limiter = 121818 | |
Guava rate-limiter = 115644 | |
Guava rate-limiter = 104803 | |
Guava rate-limiter = 155415 | |
Guava rate-limiter = 156300 | |
Guava rate-limiter = 138936 | |
Guava rate-limiter = 116146 | |
Guava rate-limiter = 103286 | |
Guava rate-limiter = 110809 | |
Guava rate-limiter = 74366 | |
Guava rate-limiter = 70510 | |
Guava rate-limiter = 79539 | |
Guava rate-limiter = 77491 | |
Guava rate-limiter = 76157 | |
Guava rate-limiter = 111347 | |
Guava rate-limiter = 69286 | |
Guava rate-limiter = 67669 | |
Guava rate-limiter = 71077 | |
Guava rate-limiter = 74837 | |
Guava rate-limiter = 67403 | |
Guava rate-limiter = 67240 | |
Guava rate-limiter = 67441 | |
Guava rate-limiter = 74310 | |
Guava rate-limiter = 125629 | |
Guava rate-limiter = 89536 | |
Guava rate-limiter = 99363 | |
Guava rate-limiter = 93774 | |
Guava rate-limiter = 126160 | |
Guava rate-limiter = 101278 | |
Guava rate-limiter = 93005 | |
Guava rate-limiter = 71114 | |
Guava rate-limiter = 92420 | |
Guava rate-limiter = 71865 | |
Guava rate-limiter = 68615 | |
***************************** | |
Custom rate-limiter= 432327 | |
Custom rate-limiter= 213219 | |
Custom rate-limiter= 246625 | |
Custom rate-limiter= 74359 | |
Custom rate-limiter= 66935 | |
Custom rate-limiter= 79609 | |
Custom rate-limiter= 76600 | |
Custom rate-limiter= 84380 | |
Custom rate-limiter= 98325 | |
Custom rate-limiter= 91005 | |
Custom rate-limiter= 106330 | |
Custom rate-limiter= 89921 | |
Custom rate-limiter= 83557 | |
Custom rate-limiter= 146340 | |
Custom rate-limiter= 90749 | |
Custom rate-limiter= 97285 | |
Custom rate-limiter= 89704 | |
Custom rate-limiter= 92439 | |
Custom rate-limiter= 90426 | |
Custom rate-limiter= 90035 | |
Custom rate-limiter= 91065 | |
Custom rate-limiter= 92197 | |
Custom rate-limiter= 76730 | |
Custom rate-limiter= 49499 | |
Custom rate-limiter= 46078 | |
Custom rate-limiter= 44608 | |
Custom rate-limiter= 43051 | |
Custom rate-limiter= 43037 | |
Custom rate-limiter= 52295 | |
Custom rate-limiter= 33088 | |
Custom rate-limiter= 144337 | |
Custom rate-limiter= 50498 | |
Custom rate-limiter= 57400 | |
Custom rate-limiter= 47586 | |
Custom rate-limiter= 47072 | |
Custom rate-limiter= 47211 | |
Custom rate-limiter= 46658 | |
Custom rate-limiter= 45878 | |
Custom rate-limiter= 46090 | |
Custom rate-limiter= 56782 | |
Custom rate-limiter= 48696 | |
Custom rate-limiter= 45281 | |
Custom rate-limiter= 44289 | |
Custom rate-limiter= 39502 | |
Custom rate-limiter= 45452 | |
Custom rate-limiter= 43763 | |
Custom rate-limiter= 88435 | |
Custom rate-limiter= 45413 | |
Custom rate-limiter= 47739 | |
Custom rate-limiter= 47027 | |
***************************** | |
Avg-Guava =133149 | |
Avg-Custom=81938 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment