Skip to content

Commit 7a551f1

Browse files
rkorytkowskiwluyima
authored andcommittedSep 4, 2013
TRUNK-3843 Introduce API performance tests
1 parent 98042dc commit 7a551f1

File tree

6 files changed

+243
-4
lines changed

6 files changed

+243
-4
lines changed
 

‎api/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,15 @@
290290
</plugins>
291291
</build>
292292
</profile>
293+
<profile>
294+
<id>performance-only</id>
295+
<dependencies>
296+
<dependency>
297+
<groupId>org.openmrs.contrib</groupId>
298+
<artifactId>mvpconceptdictionary</artifactId>
299+
<scope>test</scope>
300+
</dependency>
301+
</dependencies>
302+
</profile>
293303
</profiles>
294304
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.api;
15+
16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.containsInAnyOrder;
18+
import static org.openmrs.test.OpenmrsMatchers.hasId;
19+
20+
import java.util.List;
21+
22+
import javax.annotation.Resource;
23+
24+
import org.junit.Before;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.TestRule;
28+
import org.openmrs.Concept;
29+
import org.openmrs.test.BaseContextSensitiveTest;
30+
import org.openmrs.test.SkipBaseSetup;
31+
32+
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
33+
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
34+
35+
/**
36+
* Performance tests for {@link ConceptService}.
37+
*/
38+
@SkipBaseSetup
39+
@BenchmarkHistoryChart
40+
public class ConceptServicePerformanceIT extends BaseContextSensitiveTest {
41+
42+
@Rule
43+
public TestRule benchmarkRule = new BenchmarkRule();
44+
45+
@Resource(name = "conceptService")
46+
ConceptService conceptService;
47+
48+
static boolean dictionaryLoaded = false;
49+
50+
@Before
51+
public void loadDictionary() throws Exception {
52+
if (!dictionaryLoaded) {
53+
executeDataSet(INITIAL_XML_DATASET_PACKAGE_PATH);
54+
executeLargeDataSet("org/openmrs/contrib/mvpconceptdictionary/dbunit.xml");
55+
56+
authenticate();
57+
58+
getConnection().commit();
59+
60+
dictionaryLoaded = true;
61+
}
62+
}
63+
64+
@Test
65+
public void shouldTestGetConceptsByName() {
66+
List<Concept> concepts = conceptService.getConceptsByName("hiv positive");
67+
68+
assertThat(concepts, containsInAnyOrder(hasId(138571), hasId(159804)));
69+
}
70+
71+
@Test
72+
public void shouldTestGetConceptByName() {
73+
Concept concept = conceptService.getConceptByName("hiv positive");
74+
75+
assertThat(concept, hasId(138571));
76+
}
77+
}

‎api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
import org.dbunit.dataset.DefaultTable;
5656
import org.dbunit.dataset.IDataSet;
5757
import org.dbunit.dataset.ReplacementDataSet;
58+
import org.dbunit.dataset.stream.StreamingDataSet;
5859
import org.dbunit.dataset.xml.FlatXmlDataSet;
60+
import org.dbunit.dataset.xml.FlatXmlProducer;
5961
import org.dbunit.dataset.xml.XmlDataSet;
6062
import org.dbunit.ext.h2.H2DataTypeFactory;
6163
import org.dbunit.operation.DatabaseOperation;
@@ -81,6 +83,7 @@
8183
import org.springframework.test.context.transaction.TransactionConfiguration;
8284
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
8385
import org.springframework.transaction.annotation.Transactional;
86+
import org.xml.sax.InputSource;
8487

8588
/**
8689
* This is the base for spring/context tests. Tests that NEED to use calls to the Context class and
@@ -574,6 +577,41 @@ public void executeDataSet(String datasetFilename) throws Exception {
574577
executeDataSet(xmlDataSetToRun);
575578
}
576579

580+
/**
581+
* Runs the large flat xml dataset. It does not cache the file as opposed to
582+
* {@link #executeDataSet(String)}.
583+
*
584+
* @param datasetFilename
585+
* @throws Exception
586+
* @since 1.10
587+
*/
588+
public void executeLargeDataSet(String datasetFilename) throws Exception {
589+
InputStream inputStream = null;
590+
try {
591+
final File file = new File(datasetFilename);
592+
if (file.exists()) {
593+
inputStream = new FileInputStream(datasetFilename);
594+
} else {
595+
inputStream = getClass().getClassLoader().getResourceAsStream(datasetFilename);
596+
if (inputStream == null)
597+
throw new FileNotFoundException("Unable to find '" + datasetFilename + "' in the classpath");
598+
}
599+
600+
final FlatXmlProducer flatXmlProducer = new FlatXmlProducer(new InputSource(inputStream));
601+
final StreamingDataSet streamingDataSet = new StreamingDataSet(flatXmlProducer);
602+
603+
final ReplacementDataSet replacementDataSet = new ReplacementDataSet(streamingDataSet);
604+
replacementDataSet.addReplacementObject("[NULL]", null);
605+
606+
executeDataSet(replacementDataSet);
607+
608+
inputStream.close();
609+
}
610+
finally {
611+
IOUtils.closeQuietly(inputStream);
612+
}
613+
}
614+
577615
/**
578616
* Runs the xml data file at the classpath location specified by <code>datasetFilename</code>
579617
* using XmlDataSet. It simply creates an {@link IDataSet} and calls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.test;
15+
16+
import java.io.FileOutputStream;
17+
18+
import org.dbunit.database.DatabaseConfig;
19+
import org.dbunit.database.DatabaseConnection;
20+
import org.dbunit.database.ForwardOnlyResultSetTableFactory;
21+
import org.dbunit.database.IDatabaseConnection;
22+
import org.dbunit.dataset.IDataSet;
23+
import org.dbunit.dataset.xml.FlatXmlDataSet;
24+
import org.junit.Ignore;
25+
import org.junit.Test;
26+
27+
/**
28+
* Creates conceptDictionaryDataSet.xml. You must set connection details in
29+
* hibernate.default.properties.
30+
*/
31+
@Ignore
32+
public class CreateConceptDictionaryDataSet extends BaseContextSensitiveTest {
33+
34+
@Override
35+
public Boolean useInMemoryDatabase() {
36+
return false;
37+
}
38+
39+
@Test
40+
@SkipBaseSetup
41+
public void createConceptDictionaryDataSet() throws Exception {
42+
IDatabaseConnection connection = new DatabaseConnection(getConnection());
43+
DatabaseConfig config = connection.getConfig();
44+
45+
config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory());
46+
47+
String[] tableNames = new String[] { "concept_class", "concept_datatype", "concept_map_type",
48+
"concept_reference_source", "concept", "concept_numeric", "concept_description", "concept_name",
49+
"concept_reference_term", "concept_reference_map", "concept_reference_term_map", "concept_set",
50+
"concept_complex", "concept_answer", "concept_word", "concept_stop_word" };
51+
IDataSet dataSet = connection.createDataSet(tableNames);
52+
53+
FlatXmlDataSet.write(dataSet, new FileOutputStream("target/conceptDictionaryDataSet.xml"));
54+
}
55+
}

‎pom.xml

+59-4
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,16 @@
642642
<artifactId>xmlunit</artifactId>
643643
<version>1.3</version>
644644
</dependency>
645+
<dependency>
646+
<groupId>com.carrotsearch</groupId>
647+
<artifactId>junit-benchmarks</artifactId>
648+
<version>0.7.0</version>
649+
</dependency>
650+
<dependency>
651+
<groupId>org.openmrs.contrib</groupId>
652+
<artifactId>mvpconceptdictionary</artifactId>
653+
<version>1.9.3-20120727</version>
654+
</dependency>
645655
<dependency>
646656
<groupId>net.sf.ehcache</groupId>
647657
<artifactId>ehcache-core</artifactId>
@@ -925,6 +935,12 @@
925935
<artifactId>maven-surefire-plugin</artifactId>
926936
<version>2.5</version>
927937
<configuration>
938+
<includes>
939+
<include>**/*Test.java</include>
940+
</includes>
941+
<excludes>
942+
<exclude>**/*PerformanceTest.java</exclude>
943+
</excludes>
928944
<argLine>-Duser.language=en -Duser.region=US -Xmx512m -XX:MaxPermSize=512m ${customArgLineForTesting}</argLine>
929945
</configuration>
930946
</plugin>
@@ -945,17 +961,56 @@
945961
<artifactId>maven-surefire-plugin</artifactId>
946962
<configuration>
947963
<includes>
948-
<include>**/IT*.java</include>
949964
<include>**/*IT.java</include>
950-
<include>**/*ITCase.java</include>
951-
<include>**/Test*.java</include>
952965
<include>**/*Test.java</include>
953-
<include>**/*TestCase.java</include>
966+
</includes>
967+
<excludes>
968+
<exclude>**/*PerformanceIT.java</exclude>
969+
<exclude>**/*PerformanceTest.java</exclude>
970+
</excludes>
971+
</configuration>
972+
</plugin>
973+
</plugins>
974+
</build>
975+
</profile>
976+
<profile>
977+
<id>integration-only</id>
978+
<build>
979+
<plugins>
980+
<plugin>
981+
<groupId>org.apache.maven.plugins</groupId>
982+
<artifactId>maven-surefire-plugin</artifactId>
983+
<configuration>
984+
<includes>
985+
<include>**/*IT.java</include>
986+
</includes>
987+
<excludes>
988+
<exclude>**/*PerformanceIT.java</exclude>
989+
</excludes>
990+
</configuration>
991+
</plugin>
992+
</plugins>
993+
</build>
994+
</profile>
995+
<profile>
996+
<id>performance-only</id>
997+
<build>
998+
<plugins>
999+
<plugin>
1000+
<groupId>org.apache.maven.plugins</groupId>
1001+
<artifactId>maven-surefire-plugin</artifactId>
1002+
<configuration>
1003+
<includes>
1004+
<include>**/*PerformanceIT.java</include>
1005+
<include>**/*PerformanceTest.java</include>
9541006
</includes>
9551007
</configuration>
9561008
</plugin>
9571009
</plugins>
9581010
</build>
1011+
<properties>
1012+
<customArgLineForTesting>-Djub.consumers=CONSOLE,H2 -Djub.db.file=target/benchmarks/results -Djub.charts.dir=target/benchmarks</customArgLineForTesting>
1013+
</properties>
9591014
</profile>
9601015
</profiles>
9611016

‎test/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@
5656
<groupId>org.hamcrest</groupId>
5757
<artifactId>hamcrest-library</artifactId>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.carrotsearch</groupId>
61+
<artifactId>junit-benchmarks</artifactId>
62+
</dependency>
5963
</dependencies>
6064
</project>

0 commit comments

Comments
 (0)
Please sign in to comment.