You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/15 10:45:55 UTC

[camel] branch master updated (af0ff58 -> 790f0b4)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from af0ff58  camel-resteasy: remove unnecessary extra eval (#4450)
     new e14e445  CAMEL-15176: Optimize component to do as much in init phase vs start phase.
     new 2f8c124  CAMEL-15176: Optimize component to do as much in init phase vs start phase.
     new 0e3373e  CAMEL-15690: add jmh test
     new 790f0b4  CAMEL-15176: Optimize component to do as much in init phase vs start phase.

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/component/direct/DirectEndpoint.java     | 31 ++-------
 .../camel/component/directvm/DirectVmProducer.java | 73 +++++++++++++---------
 components/components-init-work-in-progress.md     | 12 ++--
 .../camel/component/direct/DirectEndpointTest.java | 73 ----------------------
 ...ceholderTest.java => DirectConcurrentTest.java} | 38 ++++++-----
 5 files changed, 76 insertions(+), 151 deletions(-)
 delete mode 100644 core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointTest.java
 copy tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/{SimpleMockPlaceholderTest.java => DirectConcurrentTest.java} (79%)


[camel] 03/04: CAMEL-15690: add jmh test

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0e3373ee67ef82964809dc0205cb47fd89f38ab4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 15 11:10:18 2020 +0200

    CAMEL-15690: add jmh test
---
 .../camel/itest/jmh/DirectConcurrentTest.java      | 121 +++++++++++++++++++++
 1 file changed, 121 insertions(+)

diff --git a/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/DirectConcurrentTest.java b/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/DirectConcurrentTest.java
new file mode 100644
index 0000000..ad2e156
--- /dev/null
+++ b/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/DirectConcurrentTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.itest.jmh;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.infra.Blackhole;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+import org.openjdk.jmh.runner.options.TimeValue;
+
+/**
+ * Tests a simple Camel route
+ */
+public class DirectConcurrentTest {
+
+    @Test
+    public void launchBenchmark() throws Exception {
+        Options opt = new OptionsBuilder()
+                // Specify which benchmarks to run.
+                // You can be more specific if you'd like to run only one benchmark per test.
+                .include(this.getClass().getName() + ".*")
+                // Set the following options as needed
+                .mode(Mode.All)
+                .timeUnit(TimeUnit.MICROSECONDS)
+                .warmupTime(TimeValue.seconds(5))
+                .warmupIterations(0)
+                .measurementTime(TimeValue.seconds(30))
+                .measurementIterations(1)
+                .threads(4)
+                .forks(1)
+                .shouldFailOnError(true)
+                .shouldDoGC(true)
+                .build();
+
+        new Runner(opt).run();
+    }
+
+    // The JMH samples are the best documentation for how to use it
+    // http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/
+    @State(Scope.Thread)
+    public static class BenchmarkState {
+        CamelContext camel;
+        ProducerTemplate producer;
+
+        @Setup(Level.Trial)
+        public void initialize() {
+            camel = new DefaultCamelContext();
+            try {
+                camel.addRoutes(new RouteBuilder() {
+                    @Override
+                    public void configure() throws Exception {
+                        from("direct:start")
+                                .to("direct:a")
+                                .to("direct:b")
+                                .to("direct:c")
+                                .to("mock:result?retainFirst=0");
+
+                        from("direct:a")
+                                .to("log:a?level=OFF");
+
+                        from("direct:b")
+                                .to("log:b?level=OFF");
+
+                        from("direct:c")
+                                .to("log:c?level=OFF");
+                    }
+                });
+                camel.start();
+                producer = camel.createProducerTemplate();
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+
+        @TearDown(Level.Trial)
+        public void close() {
+            try {
+                producer.stop();
+                camel.stop();
+            } catch (Exception e) {
+                // ignore
+            }
+        }
+
+    }
+
+    @Benchmark
+    public void directConcurrentTest(BenchmarkState state, Blackhole bh) {
+        ProducerTemplate template = state.producer;
+        template.sendBody("direct:start", "Hello World");
+    }
+
+}


[camel] 04/04: CAMEL-15176: Optimize component to do as much in init phase vs start phase.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 790f0b4cc198493cba281d2bd1f1f0f2486fb9fd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 15 11:57:03 2020 +0200

    CAMEL-15176: Optimize component to do as much in init phase vs start phase.
---
 .../camel/component/direct/DirectEndpoint.java     | 31 ++-------
 .../camel/component/direct/DirectEndpointTest.java | 73 ----------------------
 .../camel/component/ref/RefComponentTest.java      |  2 -
 3 files changed, 6 insertions(+), 100 deletions(-)

diff --git a/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java b/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
index 4b4e1fd..7d5ac76 100644
--- a/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
+++ b/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.direct;
 
-import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.camel.Category;
@@ -42,7 +41,7 @@ import org.apache.camel.util.StringHelper;
 public class DirectEndpoint extends DefaultEndpoint {
 
     private final Map<String, DirectConsumer> consumers;
-    private String key;
+    private final String key;
 
     @UriPath(description = "Name of direct endpoint")
     @Metadata(required = true)
@@ -55,22 +54,14 @@ public class DirectEndpoint extends DefaultEndpoint {
     @UriParam(label = "producer", defaultValue = "true")
     private boolean failIfNoConsumers = true;
 
-    public DirectEndpoint() {
-        this.consumers = new HashMap<>();
-    }
-
-    public DirectEndpoint(String endpointUri, Component component) {
-        this(endpointUri, component, new HashMap<>());
-    }
-
     public DirectEndpoint(String uri, Component component, Map<String, DirectConsumer> consumers) {
         super(uri, component);
         this.consumers = consumers;
-    }
-
-    @Override
-    protected void doInit() throws Exception {
-        key = initKey();
+        if (uri.indexOf('?') != -1) {
+            this.key = StringHelper.before(uri, "?");
+        } else {
+            this.key = uri;
+        }
     }
 
     @Override
@@ -141,8 +132,6 @@ public class DirectEndpoint extends DefaultEndpoint {
 
     /**
      * The timeout value to use if block is enabled.
-     *
-     * @param timeout the timeout value
      */
     public void setTimeout(long timeout) {
         this.timeout = timeout;
@@ -160,12 +149,4 @@ public class DirectEndpoint extends DefaultEndpoint {
         this.failIfNoConsumers = failIfNoConsumers;
     }
 
-    protected String initKey() {
-        String uri = getEndpointUri();
-        if (uri.indexOf('?') != -1) {
-            return StringHelper.before(uri, "?");
-        } else {
-            return uri;
-        }
-    }
 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointTest.java b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointTest.java
deleted file mode 100644
index 6020ca0..0000000
--- a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointTest.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.component.direct;
-
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
-import org.junit.jupiter.api.Test;
-
-public class DirectEndpointTest extends ContextTestSupport {
-
-    @Override
-    public boolean isUseRouteBuilder() {
-        return false;
-    }
-
-    @Test
-    public void testDirectEndpoint() throws Exception {
-        final DirectEndpoint de = new DirectEndpoint();
-        de.setCamelContext(context);
-        de.setEndpointUriIfNotSpecified("direct://foo");
-
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from(de).to("mock:result");
-            }
-        });
-        context.start();
-
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(1);
-
-        template.sendBody(de, "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testDirectEndpointAgain() throws Exception {
-        final DirectEndpoint de = new DirectEndpoint("direct://foo", context.getComponent("direct"));
-
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from(de).to("mock:result");
-            }
-        });
-        context.start();
-
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(1);
-
-        template.sendBody(de, "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java b/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
index 45a80e6..b9df214 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
@@ -31,10 +31,8 @@ public class RefComponentTest extends ContextTestSupport {
     private void setupComponent() throws Exception {
         Component comp = new DirectComponent();
         comp.setCamelContext(context);
-        comp.start();
 
         Endpoint slow = comp.createEndpoint("direct:somename");
-        slow.start();
         Consumer consumer = slow.createConsumer(new Processor() {
             public void process(Exchange exchange) throws Exception {
                 template.send("mock:result", exchange);


[camel] 02/04: CAMEL-15176: Optimize component to do as much in init phase vs start phase.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2f8c12481eb7542fb2f1fb7be652440356541b80
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 15 10:19:52 2020 +0200

    CAMEL-15176: Optimize component to do as much in init phase vs start phase.
---
 .../src/test/java/org/apache/camel/component/ref/RefComponentTest.java  | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java b/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
index b9df214..45a80e6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/ref/RefComponentTest.java
@@ -31,8 +31,10 @@ public class RefComponentTest extends ContextTestSupport {
     private void setupComponent() throws Exception {
         Component comp = new DirectComponent();
         comp.setCamelContext(context);
+        comp.start();
 
         Endpoint slow = comp.createEndpoint("direct:somename");
+        slow.start();
         Consumer consumer = slow.createConsumer(new Processor() {
             public void process(Exchange exchange) throws Exception {
                 template.send("mock:result", exchange);


[camel] 01/04: CAMEL-15176: Optimize component to do as much in init phase vs start phase.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit e14e44580a823445d6f67b18a957ac57874a8543
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 15 09:59:21 2020 +0200

    CAMEL-15176: Optimize component to do as much in init phase vs start phase.
---
 .../camel/component/direct/DirectEndpoint.java     | 14 ++---
 .../camel/component/directvm/DirectVmProducer.java | 73 +++++++++++++---------
 components/components-init-work-in-progress.md     | 12 ++--
 3 files changed, 56 insertions(+), 43 deletions(-)

diff --git a/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java b/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
index 941ce31..4b4e1fd 100644
--- a/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
+++ b/components/camel-direct/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
@@ -42,6 +42,7 @@ import org.apache.camel.util.StringHelper;
 public class DirectEndpoint extends DefaultEndpoint {
 
     private final Map<String, DirectConsumer> consumers;
+    private String key;
 
     @UriPath(description = "Name of direct endpoint")
     @Metadata(required = true)
@@ -68,6 +69,11 @@ public class DirectEndpoint extends DefaultEndpoint {
     }
 
     @Override
+    protected void doInit() throws Exception {
+        key = initKey();
+    }
+
+    @Override
     public Producer createProducer() throws Exception {
         return new DirectProducer(this);
     }
@@ -80,7 +86,6 @@ public class DirectEndpoint extends DefaultEndpoint {
     }
 
     public void addConsumer(DirectConsumer consumer) {
-        String key = getKey();
         synchronized (consumers) {
             if (consumers.putIfAbsent(key, consumer) != null) {
                 throw new IllegalArgumentException(
@@ -91,7 +96,6 @@ public class DirectEndpoint extends DefaultEndpoint {
     }
 
     public void removeConsumer(DirectConsumer consumer) {
-        String key = getKey();
         synchronized (consumers) {
             consumers.remove(key, consumer);
             consumers.notifyAll();
@@ -99,7 +103,6 @@ public class DirectEndpoint extends DefaultEndpoint {
     }
 
     protected DirectConsumer getConsumer() throws InterruptedException {
-        String key = getKey();
         synchronized (consumers) {
             DirectConsumer answer = consumers.get(key);
             if (answer == null && block) {
@@ -116,9 +119,6 @@ public class DirectEndpoint extends DefaultEndpoint {
                     consumers.wait(rem);
                 }
             }
-            //            if (answer != null && answer.getEndpoint() != this) {
-            //                throw new IllegalStateException();
-            //            }
             return answer;
         }
     }
@@ -160,7 +160,7 @@ public class DirectEndpoint extends DefaultEndpoint {
         this.failIfNoConsumers = failIfNoConsumers;
     }
 
-    protected String getKey() {
+    protected String initKey() {
         String uri = getEndpointUri();
         if (uri.indexOf('?') != -1) {
             return StringHelper.before(uri, "?");
diff --git a/components/camel-directvm/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java b/components/camel-directvm/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java
index 1eed7e1..568eef7 100644
--- a/components/camel-directvm/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java
+++ b/components/camel-directvm/src/main/java/org/apache/camel/component/directvm/DirectVmProducer.java
@@ -54,43 +54,56 @@ public class DirectVmProducer extends DefaultAsyncProducer {
             return true;
         }
 
-        final HeaderFilterStrategy headerFilterStrategy = endpoint.getHeaderFilterStrategy();
+        try {
+            final HeaderFilterStrategy headerFilterStrategy = endpoint.getHeaderFilterStrategy();
 
-        // Only clone the Exchange if we actually need to filter out properties or headers.
-        final Exchange submitted
-                = (!endpoint.isPropagateProperties() || headerFilterStrategy != null) ? exchange.copy() : exchange;
+            // Only clone the Exchange if we actually need to filter out properties or headers.
+            final Exchange submitted
+                    = (!endpoint.isPropagateProperties() || headerFilterStrategy != null) ? exchange.copy() : exchange;
 
-        // Clear properties in the copy if we are not propagating them.
-        if (!endpoint.isPropagateProperties()) {
-            submitted.getProperties().clear();
-        }
-
-        // Filter headers by Header Filter Strategy if there is one set.
-        if (headerFilterStrategy != null) {
-            submitted.getIn().getHeaders().entrySet()
-                    .removeIf(e -> headerFilterStrategy.applyFilterToCamelHeaders(e.getKey(), e.getValue(), submitted));
-        }
-
-        return consumer.getAsyncProcessor().process(submitted, done -> {
-            Message msg = submitted.getMessage();
-
-            if (headerFilterStrategy != null) {
-                msg.getHeaders().entrySet()
-                        .removeIf(e -> headerFilterStrategy.applyFilterToExternalHeaders(e.getKey(), e.getValue(), submitted));
+            // Clear properties in the copy if we are not propagating them.
+            if (!endpoint.isPropagateProperties()) {
+                submitted.getProperties().clear();
             }
 
-            if (exchange != submitted) {
-                // only need to copy back if they are different
-                exchange.setException(submitted.getException());
-                exchange.getOut().copyFrom(msg);
+            // Filter headers by Header Filter Strategy if there is one set.
+            if (headerFilterStrategy != null) {
+                submitted.getIn().getHeaders().entrySet()
+                        .removeIf(e -> headerFilterStrategy.applyFilterToCamelHeaders(e.getKey(), e.getValue(), submitted));
             }
 
-            if (endpoint.isPropagateProperties()) {
-                exchange.getProperties().putAll(submitted.getProperties());
-            }
+            return consumer.getAsyncProcessor().process(submitted, done -> {
+                try {
+                    Message msg = submitted.getMessage();
+
+                    if (headerFilterStrategy != null) {
+                        msg.getHeaders().entrySet()
+                                .removeIf(e -> headerFilterStrategy.applyFilterToExternalHeaders(e.getKey(), e.getValue(),
+                                        submitted));
+                    }
+
+                    if (exchange != submitted) {
+                        // only need to copy back if they are different
+                        exchange.setException(submitted.getException());
+                        exchange.getOut().copyFrom(msg);
+                    }
+
+                    if (endpoint.isPropagateProperties()) {
+                        exchange.getProperties().putAll(submitted.getProperties());
+                    }
+                } catch (Throwable e) {
+                    exchange.setException(e);
+                } finally {
+                    callback.done(done);
+                }
+            });
+
+        } catch (Throwable e) {
+            exchange.setException(e);
+        }
 
-            callback.done(done);
-        });
+        callback.done(true);
+        return true;
     }
 
 }
diff --git a/components/components-init-work-in-progress.md b/components/components-init-work-in-progress.md
index 487cdab..740dac6 100644
--- a/components/components-init-work-in-progress.md
+++ b/components/components-init-work-in-progress.md
@@ -90,13 +90,13 @@
 |camel-controlbus|DONE | |
 |camel-corda|REJECT|open network connection|
 |camel-couchbase|DONE | |
-|camel-couchdb| | |
+|camel-couchdb|DONE | |
 |camel-cron|DONE | |
-|camel-crypto| | |
-|camel-crypto-cms| | |
+|camel-crypto|DONE | |
+|camel-crypto-cms|REJECT | |
 |camel-csv|DONE| |
 |camel-cxf|REJECT|start a server|
-|camel-cxf-transport| | |
+|camel-cxf-transport|REJECT | |
 |camel-dataformat|DONE| |
 |camel-dataset|DONE| |
 |camel-debezium| | |
@@ -106,8 +106,8 @@
 |camel-debezium-postgres| | |
 |camel-debezium-sqlserver| | |
 |camel-digitalocean|REJECT|create a http client|
-|camel-direct| | |
-|camel-directvm| | |
+|camel-direct|DONE | |
+|camel-directvm|DONE | |
 |camel-disruptor|REJECT|start a thread|
 |camel-djl| | |
 |camel-dns| | |