diff --git a/src/main/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReader.java b/src/main/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReader.java index 47293b419..d757117aa 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReader.java @@ -19,8 +19,7 @@ public class AutoTuneXmlReader { /** * Read and return a Profiling from an xml file. */ - public Autotune read(File file) { - + public static Autotune read(File file) { try { return readFile(file); } catch (IOException e) { @@ -28,7 +27,7 @@ public class AutoTuneXmlReader { } } - protected Autotune readFile(File file) throws IOException { + protected static Autotune readFile(File file) throws IOException { if (!file.exists()) { return new Autotune(); } @@ -43,8 +42,7 @@ public class AutoTuneXmlReader { /** * Read and return a Profiling from an xml document. */ - public Autotune read(InputStream is) { - + public static Autotune read(InputStream is) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Autotune.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/autotune/service/DefaultAutoTuneService.java b/src/main/java/com/avaje/ebeaninternal/server/autotune/service/DefaultAutoTuneService.java index bdf99ac22..5a7942259 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autotune/service/DefaultAutoTuneService.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autotune/service/DefaultAutoTuneService.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.InputStream; import java.util.concurrent.TimeUnit; /** @@ -101,19 +102,26 @@ public class DefaultAutoTuneService implements AutoTuneService { */ private void loadTuningFile() { File file = new File(tuningFile); - if (!file.exists()) { - logger.warn("AutoTune file {} not found - no initial automatic query tuning", file.getAbsolutePath()); - + if (file.exists()) { + loadAutoTuneProfiling(AutoTuneXmlReader.read(file)); } else { - AutoTuneXmlReader reader = new AutoTuneXmlReader(); - Autotune profiling = reader.read(file); - logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size()); - for (Origin origin : profiling.getOrigin()) { - queryTuner.put(origin); + // look for autotune as a resource + InputStream stream = getClass().getResourceAsStream("/" + tuningFile); + if (stream != null) { + loadAutoTuneProfiling(AutoTuneXmlReader.read(stream)); + } else { + logger.warn("AutoTune file {} not found - no initial automatic query tuning", tuningFile); } } } + private void loadAutoTuneProfiling(Autotune profiling) { + logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size()); + for (Origin origin : profiling.getOrigin()) { + queryTuner.put(origin); + } + } + /** * Collect profiling, check for new/diff to existing tuning and apply changes. */ diff --git a/src/test/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReaderTest.java b/src/test/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReaderTest.java new file mode 100644 index 000000000..a8afbec00 --- /dev/null +++ b/src/test/java/com/avaje/ebeaninternal/server/autotune/service/AutoTuneXmlReaderTest.java @@ -0,0 +1,31 @@ +package com.avaje.ebeaninternal.server.autotune.service; + +import com.avaje.ebeaninternal.server.autotune.model.Autotune; +import org.junit.Test; + +import java.io.File; +import java.io.InputStream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AutoTuneXmlReaderTest { + + @Test + public void read_file() throws Exception { + + File testFile = new File("src/test/resources/autotune/test-autotune.xml"); + + Autotune tuneInfo = AutoTuneXmlReader.read(testFile); + assertThat(tuneInfo.getOrigin()).isNotEmpty(); + } + + @Test + public void read_inputStream() throws Exception { + + InputStream is = getClass().getResourceAsStream("/autotune/test-autotune.xml"); + + Autotune tuneInfo = AutoTuneXmlReader.read(is); + assertThat(tuneInfo.getOrigin()).isNotEmpty(); + } + +} \ No newline at end of file diff --git a/src/test/resources/autotune/test-autotune.xml b/src/test/resources/autotune/test-autotune.xml new file mode 100644 index 000000000..aabed5e4b --- /dev/null +++ b/src/test/resources/autotune/test-autotune.xml @@ -0,0 +1,35 @@ + + + + com.avaje.tests.query.autotune.TestAutoTuneProfiling.findById(TestAutoTuneProfiling.java:62) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.useOrderDate(TestAutoTuneProfiling.java:66) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:52) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25) +sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + + + + com.avaje.tests.query.autotune.TestAutoTuneProfiling.useLotUntuned(TestAutoTuneProfiling.java:95) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:55) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25) +sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) +sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) + + + + com.avaje.tests.model.basic.Order._ebean_get_customer(Order.java:6) +com.avaje.tests.model.basic.Order.getCustomer(Order.java:228) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.useOrderDateCustomerName(TestAutoTuneProfiling.java:74) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:53) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25) + + + + com.avaje.tests.model.basic.OrderDetail._ebean_get_product(OrderDetail.java:6) +com.avaje.tests.model.basic.OrderDetail.getProduct(OrderDetail.java:150) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.useLotUntuned(TestAutoTuneProfiling.java:96) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:55) +com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:32) + + + diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index c6a513108..ecacef46d 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -73,6 +73,8 @@ + +