INT-3752: Add AggregatingMetricsFactory

JIRA: https://jira.spring.io/browse/INT-3752
JIRA: https://jira.spring.io/browse/INT-3754

The statistics are based on this count so, for example the
`meanSendDuration` reflects the mean of total duration from the first send to
the last in each batch.

Also change the linked lists to `Deque`s.

Also remove `isTraceEnabled()` calls - too expensive in high volume environments.

Polishing

Assign the count to a local variable so the mod (%) operation is short
circuited rather than short circuiting the call to isFullStatsEnabled().

INT-3752: Fix Aggregating Metrics

Previously, the duration was for the first message in each sample.

We need to capture the total elapsed time for the sample and use it
for the duration calculation.

Add 'newCount' to the context so the after... method can calculate
the duration at the end of the sample.

The start field does not need to be volatile because its updates
are synchronized.

INT-3752: Fix Javadocs
This commit is contained in:
Gary Russell
2015-06-26 15:13:34 -04:00
committed by Artem Bilan
parent 5298368168
commit a480354210
14 changed files with 472 additions and 61 deletions

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<context:mbean-server />
<int-jmx:mbean-export metrics-factory="aggregatingMetricsFactory" />
<int:channel id="input" />
<int:bridge input-channel="input" output-channel="queue" />
<int:channel id="queue">
<int:queue />
</int:channel>
<bean id="aggregatingMetricsFactory" class="org.springframework.integration.monitor.AggregatingMetricsFactory">
<constructor-arg value="1000" />
</bean>
<int:channel id="delay" />
<int:service-activator input-channel="delay" expression="T(Thread).sleep(50)" />
</beans>

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.monitor;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.management.AggregatingMessageChannelMetrics;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.handler.management.AggregatingMessageHandlerMetrics;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @since 4.2
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class AggregatingMetricsTests {
@Autowired
private MessageChannel input;
@Autowired
private AbstractMessageChannel delay;
@Autowired
private QueueChannel output;
@Autowired
private BridgeHandler handler;
@Autowired
private ServiceActivatingHandler delayer;
@Test
public void testCounts() {
Message<?> message = new GenericMessage<String>("foo");
int count = 2000;
for (int i = 0; i < count; i++) {
input.send(message);
}
assertEquals(count, this.output.getQueueSize());
assertEquals(count, this.output.getSendCount());
assertEquals(Long.valueOf(count / 1000).longValue(), this.output.getSendDuration().getCountLong());
assertEquals(count, this.handler.getHandleCount());
assertEquals(Long.valueOf(count / 1000).longValue(), this.handler.getDuration().getCountLong());
}
@Test
public void testElapsed() {
int sampleSize = 2;
this.delay.configureMetrics(new AggregatingMessageChannelMetrics("foo", sampleSize));
this.delay.enableStats(true);
this.delayer.configureMetrics(new AggregatingMessageHandlerMetrics("bar", sampleSize));
this.delayer.enableStats(true);
GenericMessage<String> message = new GenericMessage<String>("foo");
int count = 4;
for (int i = 0; i < count; i++) {
this.delay.send(message);
}
assertEquals(count, this.delay.getSendCount());
assertEquals(count / sampleSize, this.delay.getSendDuration().getCount());
assertThat((int) this.delay.getMeanSendDuration() / sampleSize, greaterThanOrEqualTo(50));
assertEquals(count, this.delayer.getHandleCount());
assertEquals(count / sampleSize, this.delayer.getDuration().getCount());
assertThat((int) this.delayer.getMeanDuration() / sampleSize, greaterThanOrEqualTo(50));
}
@Test @Ignore
public void perf() {
AggregatingMessageHandlerMetrics metrics = new AggregatingMessageHandlerMetrics();
for (int i = 0; i < 100000000; i++) {
metrics.afterHandle(metrics.beforeHandle(), true);
}
}
}