org.apache.activemq.artemis.utils.TokenBucketLimiterImpl

Here are the examples of the java api class org.apache.activemq.artemis.utils.TokenBucketLimiterImpl taken from open source projects.

1. TokenBucketLimiterImplTest#testVerifyRate()

Project: activemq-artemis
File: TokenBucketLimiterImplTest.java
public void testVerifyRate(final int rate, final int window, final int timeRunning) throws Exception {
    // Allow for 5% error
    final double error = 1.05;
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicBoolean rateError = new AtomicBoolean(false);
    final AtomicInteger iterations = new AtomicInteger(0);
    TokenBucketLimiterImpl tbl = new TokenBucketLimiterImpl(rate, false, TimeUnit.SECONDS, window);
    Thread t = new Thread() {

        @Override
        public void run() {
            int lastRun = 0;
            long lastTime = System.currentTimeMillis();
            while (running.get()) {
                int tmpValue = iterations.get();
                if (lastRun != 0) {
                    int consumed = tmpValue - lastRun;
                    double calculatedRate = consumed * window * 1000 / ((System.currentTimeMillis() - lastTime));
                    if (calculatedRate > rate * error) {
                        System.out.println("got more than " + rate + " tokens / second");
                        rateError.set(true);
                    } else if (calculatedRate > rate) {
                        System.out.println("got more than " + rate + " tokens / second but still on the error marging" + "make sure it's ok though, if you see to many of this message it's an issue");
                    }
                    System.out.println("Rate = " + calculatedRate + " consumed = " + consumed);
                }
                lastTime = System.currentTimeMillis();
                lastRun = tmpValue;
                try {
                    Thread.sleep(window * 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
    long timeout;
    timeout = System.currentTimeMillis() + 3000;
    while (timeout > System.currentTimeMillis()) {
        tbl.limit();
        iterations.incrementAndGet();
    }
    Thread.sleep(3000);
    timeout = System.currentTimeMillis() + timeRunning;
    while (timeout > System.currentTimeMillis()) {
        tbl.limit();
        iterations.incrementAndGet();
    }
    running.set(false);
    t.join();
}

2. TokenBucketLimiterImplTest#testRate()

Project: activemq-artemis
File: TokenBucketLimiterImplTest.java
private void testRate(final int rate, final boolean spin) throws Exception {
    // Allow for 5% error
    final double error = 0.05;
    TokenBucketLimiterImpl tbl = new TokenBucketLimiterImpl(rate, spin);
    long start = System.currentTimeMillis();
    long count = 0;
    final long measureTime = 5000;
    // Do some initial testing ..   to ramp up the calculations
    while (System.currentTimeMillis() - start < measureTime) {
        tbl.limit();
    }
    // wait some time
    Thread.sleep(2000);
    // start measuring again
    start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < measureTime) {
        tbl.limit();
        // when using a low rate (1 for instance), the very last could come after or very close to 5 seconds
        // what could give us a false negative
        count++;
    }
    long end = System.currentTimeMillis();
    if (rate == 1) {
        // in 5 seconds you may get exactly 6 buckets..
        // Count... 1, 2, 3, 4, 5, 6
        // Time.... 0, 1, 2, 3, 4, 5
        // any variation on 1 could make the test to fail, for that reason we make it this way
        Assert.assertTrue(count == 5 || count == 6);
    } else {
        double actualRate = (double) (1000 * count) / measureTime;
        Assert.assertTrue("actual rate = " + actualRate + " expected=" + rate, actualRate > rate * (1 - error));
        Assert.assertTrue("actual rate = " + actualRate + " expected=" + rate, actualRate < rate * (1 + error));
    }
}