Repository: camel Updated Branches: refs/heads/master 1131374e2 -> 32edf80c5
CAMEL-XXX Add JMH benchmarks for TypeConverter Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d7c29069 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d7c29069 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d7c29069 Branch: refs/heads/master Commit: d7c2906937186c7113ca01838f2496eb08b2bcdc Parents: 1131374 Author: Lachowicz, Marcin <[email protected]> Authored: Thu Jul 13 17:32:31 2017 +0200 Committer: Claus Ibsen <[email protected]> Committed: Sat Jul 15 13:34:02 2017 +0200 ---------------------------------------------------------------------- .../camel/itest/jmh/TypeConverterTest.java | 105 +++++++++++++++---- .../src/test/resources/sample_soap.xml | 33 ++++++ 2 files changed, 116 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d7c29069/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/TypeConverterTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/TypeConverterTest.java b/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/TypeConverterTest.java index c51c8f8..912225e 100644 --- a/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/TypeConverterTest.java +++ b/tests/camel-jmh/src/test/java/org/apache/camel/itest/jmh/TypeConverterTest.java @@ -16,14 +16,20 @@ */ package org.apache.camel.itest.jmh; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; import java.util.concurrent.TimeUnit; +import org.w3c.dom.Document; + import org.apache.camel.CamelContext; import org.apache.camel.impl.DefaultCamelContext; import org.junit.Test; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; @@ -43,21 +49,22 @@ public class TypeConverterTest { @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(1)) - .warmupIterations(2) - .measurementTime(TimeValue.seconds(1)) - .measurementIterations(2) - .threads(2) - .forks(1) - .shouldFailOnError(true) - .shouldDoGC(true) - .build(); + // 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.SampleTime) + .timeUnit(TimeUnit.MILLISECONDS) + .warmupTime(TimeValue.seconds(1)) + .warmupIterations(2) + .measurementTime(TimeValue.seconds(5)) + .measurementIterations(3) + .threads(1) + .forks(1) + .shouldFailOnError(true) + .shouldDoGC(true) + .measurementBatchSize(100000) + .build(); new Runner(opt).run(); } @@ -65,17 +72,25 @@ public class TypeConverterTest { // 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 { + public static class BenchmarkCamelContextState { + Integer someInteger = 12345; + String someIntegerString = String.valueOf(someInteger); + String xmlAsString; + byte[] xmlAsBytes; + CamelContext camel; @Setup(Level.Trial) - public void initialize() { + public void initialize() throws IOException { camel = new DefaultCamelContext(); try { camel.start(); } catch (Exception e) { // ignore } + + xmlAsString = getResourceAsString("sample_soap.xml"); + xmlAsBytes = xmlAsString.getBytes(StandardCharsets.UTF_8); } @TearDown(Level.Trial) @@ -87,13 +102,59 @@ public class TypeConverterTest { } } + private String getResourceAsString(String resource) { + Scanner s = new Scanner(getClass().getClassLoader().getResourceAsStream(resource)) + .useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; + } } + @Benchmark - @Measurement(batchSize = 1000000) - public void typeConvertIntegerToString(BenchmarkState state, Blackhole bh) { - String id = state.camel.getTypeConverter().convertTo(String.class, 12345); - bh.consume(id); + public void typeConvertIntegerToString(BenchmarkCamelContextState state, Blackhole bh) { + String string = state.camel.getTypeConverter().convertTo(String.class, state.someInteger); + bh.consume(string); } + @Benchmark + public void typeConvertStringToInteger(BenchmarkCamelContextState state, Blackhole bh) { + Integer integer = state.camel.getTypeConverter().convertTo(Integer.class, state.someIntegerString); + bh.consume(integer); + } + + @Benchmark + public void typeConvertTheSameTypes(BenchmarkCamelContextState state, Blackhole bh) { + String string = state.camel.getTypeConverter().convertTo(String.class, state.someIntegerString); + bh.consume(string); + } + + @Benchmark + public void typeConvertInputStreamToString(BenchmarkCamelContextState state, Blackhole bh) { + String string = state.camel.getTypeConverter().convertTo(String.class, new ByteArrayInputStream(state.xmlAsBytes)); + bh.consume(string); + } + + @Benchmark + public void typeConvertStringToInputStream(BenchmarkCamelContextState state, Blackhole bh) { + InputStream inputStream = state.camel.getTypeConverter().convertTo(InputStream.class, state.xmlAsString); + bh.consume(inputStream); + } + + @Benchmark + public void typeConvertStringToDocument(BenchmarkCamelContextState state, Blackhole bh) { + Document document = state.camel.getTypeConverter().convertTo(Document.class, state.xmlAsString); + bh.consume(document); + } + + @Benchmark + public void typeConvertStringToByteArray(BenchmarkCamelContextState state, Blackhole bh) { + byte[] bytes = state.camel.getTypeConverter().convertTo(byte[].class, state.xmlAsString); + bh.consume(bytes); + } + + @Benchmark + public void typeConvertByteArrayToString(BenchmarkCamelContextState state, Blackhole bh) { + String string = state.camel.getTypeConverter().convertTo(String.class, state.xmlAsBytes); + bh.consume(string); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/d7c29069/tests/camel-jmh/src/test/resources/sample_soap.xml ---------------------------------------------------------------------- diff --git a/tests/camel-jmh/src/test/resources/sample_soap.xml b/tests/camel-jmh/src/test/resources/sample_soap.xml new file mode 100644 index 0000000..6800482 --- /dev/null +++ b/tests/camel-jmh/src/test/resources/sample_soap.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> + <soapenv:Header><routing xmlns="http://someuri">xadmin;server1;community#1.0##</routing></soapenv:Header> + <soapenv:Body> + <m:buyStocks xmlns:m="http://services.samples/xsd"> + <order><symbol>IBM</symbol><buyerID>asankha</buyerID><price>140.34</price><volume>2000</volume></order> + <order><symbol>MSFT</symbol><buyerID>ruwan</buyerID><price>23.56</price><volume>8030</volume></order> + <order><symbol>SUN</symbol><buyerID>indika</buyerID><price>14.56</price><volume>500</volume></order> + <order><symbol>GOOG</symbol><buyerID>chathura</buyerID><price>60.24</price><volume>40000</volume></order> + <order><symbol>IBM</symbol><buyerID>asankha</buyerID><price>140.34</price><volume>2000</volume></order> + <order><symbol>MSFT</symbol><buyerID>ruwan</buyerID><price>23.56</price><volume>803000</volume></order> + <order><symbol>SUN</symbol><buyerID>indika</buyerID><price>14.56</price><volume>5000</volume></order> + </m:buyStocks> + </soapenv:Body> +</soapenv:Envelope> \ No newline at end of file
