Skip to content

Instantly share code, notes, and snippets.

@laurentvaills
Created June 12, 2015 14:18
Show Gist options
  • Save laurentvaills/e763218fa26b5501c1d4 to your computer and use it in GitHub Desktop.
Save laurentvaills/e763218fa26b5501c1d4 to your computer and use it in GitHub Desktop.
Ensure throttling works as expected
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();
}
}
}
@laurentvaills
Copy link
Author

The last output on the console is :

-- Meters ----------------------------------------------------------------------
injector
             count = 292984
         mean rate = 970.20 events/second
     1-minute rate = 974.38 events/second
     5-minute rate = 759.41 events/second
    15-minute rate = 555.57 events/second
output
             count = 2941
         mean rate = 9.74 events/second
     1-minute rate = 9.77 events/second
     5-minute rate = 8.31 events/second
    15-minute rate = 6.92 events/second

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