use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.
the class ActiveMQSessionContext method createConsumer.
@Override
public ClientConsumerInternal createConsumer(SimpleString queueName, SimpleString filterString, int windowSize, int maxRate, int ackBatchSize, boolean browseOnly, Executor executor, Executor flowControlExecutor) throws ActiveMQException {
long consumerID = idGenerator.generateID();
ActiveMQConsumerContext consumerContext = new ActiveMQConsumerContext(consumerID);
SessionCreateConsumerMessage request = new SessionCreateConsumerMessage(consumerID, queueName, filterString, browseOnly, true);
SessionQueueQueryResponseMessage queueInfo;
if (sessionChannel.getConnection().isVersionBeforeAddressChange()) {
queueInfo = (SessionQueueQueryResponseMessage) sessionChannel.sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP_V2);
} else {
queueInfo = (SessionQueueQueryResponseMessage) sessionChannel.sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP_V3);
}
return new ClientConsumerImpl(session, consumerContext, queueName, filterString, browseOnly, calcWindowSize(windowSize), ackBatchSize, maxRate > 0 ? new TokenBucketLimiterImpl(maxRate, false) : null, executor, flowControlExecutor, this, queueInfo.toQueueQuery(), lookupTCCL());
}
use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.
the class TokenBucketLimiterImplTest method testRate.
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));
}
}
use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.
the class TokenBucketLimiterImplTest method testVerifyRate.
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();
}
use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.
the class PerfBase method sendMessages.
private void sendMessages(final int numberOfMessages, final int txBatchSize, final boolean durable, final boolean transacted, final boolean display, final int throttleRate, final int messageSize) throws Exception {
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(perfParams.isDurable() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
producer.setDisableMessageID(perfParams.isDisableMessageID());
producer.setDisableMessageTimestamp(perfParams.isDisableTimestamp());
BytesMessage message = session.createBytesMessage();
byte[] payload = PerfBase.randomByteArray(messageSize);
message.writeBytes(payload);
final int modulo = 2000;
TokenBucketLimiter tbl = throttleRate != -1 ? new TokenBucketLimiterImpl(throttleRate, false) : null;
boolean committed = false;
for (int i = 1; i <= numberOfMessages; i++) {
producer.send(message);
if (transacted) {
if (i % txBatchSize == 0) {
session.commit();
committed = true;
} else {
committed = false;
}
}
if (display && i % modulo == 0) {
double duration = (1.0 * System.currentTimeMillis() - start) / 1000;
PerfBase.log.info(String.format("sent %6d messages in %2.2fs", i, duration));
}
if (tbl != null) {
tbl.limit();
}
}
if (transacted && !committed) {
session.commit();
}
}
use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.
the class HornetQClientSessionContext method createConsumer.
@Override
public ClientConsumerInternal createConsumer(SimpleString queueName, SimpleString filterString, int windowSize, int maxRate, int ackBatchSize, boolean browseOnly, Executor executor, Executor flowControlExecutor) throws ActiveMQException {
long consumerID = idGenerator.generateID();
ActiveMQConsumerContext consumerContext = new ActiveMQConsumerContext(consumerID);
SessionCreateConsumerMessage request = new SessionCreateConsumerMessage(consumerID, queueName, filterString, browseOnly, true);
SessionQueueQueryResponseMessage queueInfo = (SessionQueueQueryResponseMessage) getSessionChannel().sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP);
return new ClientConsumerImpl(session, consumerContext, queueName, filterString, browseOnly, calcWindowSize(windowSize), ackBatchSize, maxRate > 0 ? new TokenBucketLimiterImpl(maxRate, false) : null, executor, flowControlExecutor, this, queueInfo.toQueueQuery(), lookupTCCL());
}
Aggregations