This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.7.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.7.x by this push: new 4315693 CAMEL-16199: camel-http - Optimize headers added as request headers in producer 4315693 is described below commit 4315693d9449337655edd518895e381c055f05c8 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Feb 13 18:17:34 2021 +0100 CAMEL-16199: camel-http - Optimize headers added as request headers in producer --- .../apache/camel/component/http/HttpProducer.java | 27 +++-- .../camel/component/http/HttpProducerLoadTest.java | 125 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 7 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java index 1fc7d74..85d3d98 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java @@ -33,12 +33,12 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.stream.Collectors; import org.apache.camel.CamelExchangeException; import org.apache.camel.Exchange; import org.apache.camel.ExtendedExchange; import org.apache.camel.Message; +import org.apache.camel.TypeConverter; import org.apache.camel.component.file.GenericFile; import org.apache.camel.component.http.helper.HttpMethodHelper; import org.apache.camel.converter.stream.CachedOutputStream; @@ -130,6 +130,7 @@ public class HttpProducer extends DefaultProducer { } // propagate headers as HTTP headers + final TypeConverter tc = exchange.getContext().getTypeConverter(); for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) { String key = entry.getKey(); Object headerValue = in.getHeader(key); @@ -139,12 +140,13 @@ public class HttpProducer extends DefaultProducer { final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true); // the value to add as request header - final List<String> values = new ArrayList<>(); + List<String> multiValues = null; + String prev = null; // if its a multi value then check each value if we can add it and for multi values they // should be combined into a single value while (it.hasNext()) { - String value = exchange.getContext().getTypeConverter().convertTo(String.class, it.next()); + String value = tc.convertTo(String.class, it.next()); // we should not add headers for the parameters in the uri if we bridge the endpoint // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well @@ -152,16 +154,27 @@ public class HttpProducer extends DefaultProducer { continue; } if (value != null && strategy != null && !strategy.applyFilterToCamelHeaders(key, value, exchange)) { - values.add(value); + if (prev == null) { + prev = value; + } else { + // only create array for multi values when really needed + if (multiValues == null) { + multiValues = new ArrayList<>(); + multiValues.add(prev); + } + multiValues.add(value); + } } } // add the value(s) as a http request header - if (!values.isEmpty()) { + if (multiValues != null) { // use the default toString of a ArrayList to create in the form [xxx, yyy] // if multi valued, for a single value, then just output the value as is - String s = values.size() > 1 ? values.toString() : values.get(0); + String s = multiValues.size() > 1 ? multiValues.toString() : multiValues.get(0); httpRequest.addHeader(key, s); + } else if (prev != null) { + httpRequest.addHeader(key, prev); } } } @@ -173,7 +186,7 @@ public class HttpProducer extends DefaultProducer { String key = entry.getKey(); if (!entry.getValue().isEmpty()) { // join multi-values separated by semi-colon - httpRequest.addHeader(key, entry.getValue().stream().collect(Collectors.joining(";"))); + httpRequest.addHeader(key, String.join(";", entry.getValue())); } } } diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerLoadTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerLoadTest.java new file mode 100644 index 0000000..cd25a64 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpProducerLoadTest.java @@ -0,0 +1,125 @@ +/* + * 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.http; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.handler.DrinkValidationHandler; +import org.apache.camel.util.StopWatch; +import org.apache.http.impl.bootstrap.HttpServer; +import org.apache.http.impl.bootstrap.ServerBootstrap; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.component.http.HttpMethods.GET; + +@Disabled("Manual test") +public class HttpProducerLoadTest extends BaseHttpTest { + + private static final Logger LOG = LoggerFactory.getLogger(HttpProducerLoadTest.class); + + private HttpServer localServer; + + @BeforeEach + @Override + public void setUp() throws Exception { + localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()) + .setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()) + .setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()) + .registerHandler("/echo", new DrinkValidationHandler(GET.name(), null, null, "myHeader")).create(); + localServer.start(); + + super.setUp(); + } + + @AfterEach + @Override + public void tearDown() throws Exception { + super.tearDown(); + + if (localServer != null) { + localServer.stop(); + } + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:echo") + .to("http://localhost:" + localServer.getLocalPort() + + "/echo?throwExceptionOnFailure=false"); + } + }; + } + + @Test + public void testProducerLoad() throws Exception { + StopWatch watch = new StopWatch(); + for (int i = 0; i < 10000000; i++) { + fluentTemplate.to("direct:echo") + .withHeader("a", "aaa") + .withHeader("b", "bbb") + .withHeader("c", "ccc") + .withHeader("d", "ddd") + .withHeader("e", "eee") + .withHeader("f", "fff") + .withHeader("g", "ggg") + .withHeader("h", "hhh") + .withHeader("i", "iii") + .withHeader("j", "jjj") + .withHeader("a2", "aaa") + .withHeader("b2", "bbb") + .withHeader("c2", "ccc") + .withHeader("d2", "ddd") + .withHeader("e2", "eee") + .withHeader("f2", "fff") + .withHeader("g2", "ggg") + .withHeader("h2", "hhh") + .withHeader("i2", "iii") + .withHeader("j2", "jjj") + .withHeader("a3", "aaa") + .withHeader("b3", "bbb") + .withHeader("c3", "ccc") + .withHeader("d3", "ddd") + .withHeader("e3", "eee") + .withHeader("f3", "fff") + .withHeader("g3", "ggg") + .withHeader("h3", "hhh") + .withHeader("i3", "iii") + .withHeader("j3", "jjj") + .withHeader("a4", "aaa") + .withHeader("b4", "bbb") + .withHeader("c4", "ccc") + .withHeader("d4", "ddd") + .withHeader("e4", "eee") + .withHeader("f4", "fff") + .withHeader("g4", "ggg") + .withHeader("h4", "hhh") + .withHeader("i4", "iii") + .withHeader("j4", "jjj") + .withHeader("myHeader", "msg" + i).send(); + } + LOG.info("Took {} ms", watch.taken()); + } + +}