Created
June 12, 2015 14:18
-
-
Save laurentvaills/e763218fa26b5501c1d4 to your computer and use it in GitHub Desktop.
Ensure throttling works as expected
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
package org.forgerock.openig.filter; | |
import static org.forgerock.util.time.Duration.duration; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import org.forgerock.util.time.Duration; | |
import org.forgerock.util.time.TimeService; | |
import com.codahale.metrics.ConsoleReporter; | |
import com.codahale.metrics.Meter; | |
import com.codahale.metrics.MetricRegistry; | |
public class BucketMain { | |
private static class BucketRunnable implements Runnable { | |
private TokenBucket bucket; | |
private boolean stop; | |
private Meter injector; | |
private Meter output; | |
private CountDownLatch latch; | |
public BucketRunnable(TokenBucket bucket, MetricRegistry metrics, CountDownLatch latch) { | |
this.latch = latch; | |
this.bucket = bucket; | |
this.stop = false; | |
this.injector = metrics.meter("injector"); | |
this.output = metrics.meter("output"); | |
} | |
@Override | |
public void run() { | |
try { | |
latch.await(); | |
} catch (InterruptedException e1) { | |
throw new RuntimeException(e1); | |
} | |
while (!stop) { | |
injector.mark(); | |
long delay = bucket.consume(); | |
if (delay <= 0) { | |
output.mark(); | |
} | |
try { | |
Thread.sleep(5); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public void stop() { | |
this.stop = true; | |
} | |
} | |
static final MetricRegistry metrics = new MetricRegistry(); | |
public static void main(String args[]) throws InterruptedException { | |
TimeService time = TimeService.SYSTEM; | |
startReport(); | |
TokenBucket bucket = new TokenBucket(time, 10, duration("1 seconds")); | |
final int count = 5; | |
List<BucketRunnable> runnables = new ArrayList<>(count); | |
ExecutorService threadPool = Executors.newFixedThreadPool(count); | |
CountDownLatch latch = new CountDownLatch(1); | |
for (int i = 0; i < count; i++) { | |
BucketRunnable runnable = new BucketRunnable(bucket, metrics, latch); | |
runnables.add(runnable); | |
threadPool.submit(runnable); | |
} | |
wait(duration("3 seconds")); | |
latch.countDown(); | |
wait(duration("5 minutes")); | |
for(BucketRunnable runnable : runnables) { | |
runnable.stop(); | |
} | |
threadPool.shutdown(); | |
threadPool.awaitTermination(20, TimeUnit.SECONDS); | |
} | |
static void startReport() { | |
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics) | |
.convertRatesTo(TimeUnit.SECONDS) | |
.convertDurationsTo(TimeUnit.MILLISECONDS) | |
.build(); | |
reporter.start(2, TimeUnit.SECONDS); | |
} | |
static void wait(Duration duration) { | |
try { | |
Thread.sleep(duration.to(TimeUnit.MILLISECONDS)); | |
} catch(InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last output on the console is :