Repository: camel Updated Branches: refs/heads/master 1220328ab -> cbf469e2f
CAMEL-11341: Optimise DefaultEndpointUtilizationStatistics to avoid synchronized block when updating hit counter. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cbf469e2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cbf469e2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cbf469e2 Branch: refs/heads/master Commit: cbf469e2f2037ac99b1037d729c701d60dd7a866 Parents: 1220328 Author: Claus Ibsen <davscl...@apache.org> Authored: Fri May 26 13:42:43 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri May 26 13:51:36 2017 +0200 ---------------------------------------------------------------------- .../DefaultEndpointUtilizationStatistics.java | 17 +++--- .../spi/EndpointUtilizationStatistics.java | 1 + ...anagedEndpointUtilizationStatisticsTest.java | 61 ++++++++++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cbf469e2/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointUtilizationStatistics.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointUtilizationStatistics.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointUtilizationStatistics.java index 9c8b1dc..14efe2b 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointUtilizationStatistics.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultEndpointUtilizationStatistics.java @@ -41,15 +41,14 @@ public class DefaultEndpointUtilizationStatistics implements EndpointUtilization } @Override - public synchronized void onHit(String uri) { - Long counter = map.get(uri); - if (counter == null) { - counter = 1L; - map.put(uri, counter); - } else { - counter++; - map.put(uri, counter); - } + public void onHit(String uri) { + map.compute(uri, (key, current) -> { + if (current == null) { + return 1L; + } else { + return ++current; + } + }); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/cbf469e2/camel-core/src/main/java/org/apache/camel/spi/EndpointUtilizationStatistics.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/EndpointUtilizationStatistics.java b/camel-core/src/main/java/org/apache/camel/spi/EndpointUtilizationStatistics.java index dabcd06..ca6cfba 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/EndpointUtilizationStatistics.java +++ b/camel-core/src/main/java/org/apache/camel/spi/EndpointUtilizationStatistics.java @@ -17,6 +17,7 @@ package org.apache.camel.spi; import java.util.Map; +import java.util.concurrent.atomic.LongAdder; /** * Various statistics about endpoint utilization, such as from EIP patterns that uses dynamic endpoints. http://git-wip-us.apache.org/repos/asf/camel/blob/cbf469e2/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointUtilizationStatisticsTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointUtilizationStatisticsTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointUtilizationStatisticsTest.java new file mode 100644 index 0000000..49188ee --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/management/ManagedEndpointUtilizationStatisticsTest.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.management; + +import java.util.List; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.RuntimeEndpointRegistry; + +/** + * @version + */ +public class ManagedEndpointUtilizationStatisticsTest extends ManagementTestSupport { + + public void testManageEndpointUtilizationStatistics() throws Exception { + // JMX tests dont work well on AIX CI servers (hangs them) + if (isPlatform("aix")) { + return; + } + + getMockEndpoint("mock:result").expectedMessageCount(4); + + template.sendBody("seda:start", "Hello World"); + template.sendBody("seda:start", "Bye World"); + template.sendBody("seda:start", "Hi World"); + template.sendBody("seda:start", "Camel World"); + + assertMockEndpointsSatisfied(); + + List<RuntimeEndpointRegistry.Statistic> stats = context.getRuntimeEndpointRegistry().getEndpointStatistics(); + assertNotNull(stats); + assertEquals(2, stats.size()); + assertEquals(4, stats.get(0).getHits()); + assertEquals(4, stats.get(1).getHits()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:start").to("mock:result"); + } + }; + } + +} \ No newline at end of file