Search in sources :

Example 6 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class SoakSender method run.

public void run() throws Exception {
    connect();
    boolean runInfinitely = perfParams.getDurationInMinutes() == -1;
    BytesMessage message = session.createBytesMessage();
    byte[] payload = SoakBase.randomByteArray(perfParams.getMessageSize());
    message.writeBytes(payload);
    final int modulo = 10000;
    TokenBucketLimiter tbl = perfParams.getThrottleRate() != -1 ? new TokenBucketLimiterImpl(perfParams.getThrottleRate(), false) : null;
    boolean transacted = perfParams.isSessionTransacted();
    int txBatchSize = perfParams.getBatchSize();
    boolean display = true;
    long start = System.currentTimeMillis();
    long moduleStart = start;
    AtomicLong count = new AtomicLong(0);
    while (true) {
        try {
            producer.send(message);
            count.incrementAndGet();
            if (transacted) {
                if (count.longValue() % txBatchSize == 0) {
                    session.commit();
                }
            }
            long totalDuration = System.currentTimeMillis() - start;
            if (display && count.longValue() % modulo == 0) {
                double duration = (1.0 * System.currentTimeMillis() - moduleStart) / 1000;
                moduleStart = System.currentTimeMillis();
                SoakSender.log.info(String.format("sent %s messages in %2.2fs (time: %.0fs)", modulo, duration, totalDuration / 1000.0));
            }
            if (tbl != null) {
                tbl.limit();
            }
            if (!runInfinitely && totalDuration > perfParams.getDurationInMinutes() * SoakBase.TO_MILLIS) {
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    SoakSender.log.info(String.format("Sent %s messages in %s minutes", count, perfParams.getDurationInMinutes()));
    SoakSender.log.info("END OF RUN");
    if (connection != null) {
        connection.close();
        connection = null;
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TokenBucketLimiter(org.apache.activemq.artemis.utils.TokenBucketLimiter) TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl) BytesMessage(javax.jms.BytesMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Aggregations

TokenBucketLimiterImpl (org.apache.activemq.artemis.utils.TokenBucketLimiterImpl)6 BytesMessage (javax.jms.BytesMessage)2 ClientConsumerImpl (org.apache.activemq.artemis.core.client.impl.ClientConsumerImpl)2 SessionCreateConsumerMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCreateConsumerMessage)2 SessionQueueQueryResponseMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionQueueQueryResponseMessage)2 TokenBucketLimiter (org.apache.activemq.artemis.utils.TokenBucketLimiter)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 JMSException (javax.jms.JMSException)1 MessageProducer (javax.jms.MessageProducer)1 NamingException (javax.naming.NamingException)1 ActiveMQConsumerContext (org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQConsumerContext)1