This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - prepare test for optimize GetTransUnitListHandler to resolve hi…
…bernate n+1 query problem
- Loading branch information
Patrick Huang
committed
Oct 19, 2012
1 parent
c11c40c
commit 3cc51d2
Showing
7 changed files
with
399 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
zanata-war/src/test/java/org/zanata/DBUnitDataExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.zanata; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.util.AbstractMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.dbunit.database.DatabaseConnection; | ||
import org.dbunit.database.IDatabaseConnection; | ||
import org.dbunit.database.QueryDataSet; | ||
import org.dbunit.dataset.xml.FlatDtdDataSet; | ||
import org.dbunit.dataset.xml.FlatXmlDataSet; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* This class is used to extract data out of a real database and put it in DBUnit's xml format. | ||
* | ||
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
@Test(groups = "manual-tests") | ||
@Slf4j | ||
public class DBUnitDataExtractor | ||
{ | ||
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); | ||
|
||
private void generateTestData(String dataSetName, List<Map.Entry<String, String>> tableNameToQueryMap) throws Exception | ||
{ | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zanata", "root", ""); | ||
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); | ||
|
||
QueryDataSet dataSet = new QueryDataSet(connection); | ||
|
||
for (Map.Entry<String, String> entry : tableNameToQueryMap) | ||
{ | ||
dataSet.addTable(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
log.info("+++++ writing DBUnit data set to : {}", TEMP_DIR); | ||
FlatDtdDataSet.write(dataSet, new FileOutputStream(new File(TEMP_DIR, dataSetName + ".dbunit.dtd"))); | ||
FlatXmlDataSet.write(dataSet, new FileOutputStream(new File(TEMP_DIR, dataSetName + ".dbunit.xml"))); | ||
|
||
jdbcConnection.close(); | ||
} | ||
|
||
@Test | ||
public void run() throws Exception | ||
{ | ||
List<Map.Entry<String, String>> tableNameAndQuery = Lists.newArrayList(); | ||
|
||
addEntry(tableNameAndQuery, "HAccount", "SELECT * FROM HAccount where id = 1"); | ||
addEntry(tableNameAndQuery, "HPerson", "SELECT * FROM HPerson where id = 1"); | ||
addEntry(tableNameAndQuery, "HLocale", "SELECT * FROM HLocale where id = 3 or id = 5"); | ||
addEntry(tableNameAndQuery, "HPoHeader", "SELECT * FROM HPoHeader where id = 1"); | ||
addEntry(tableNameAndQuery, "HProject", "SELECT * FROM HProject where id = 1"); | ||
addEntry(tableNameAndQuery, "HProjectIteration", "SELECT * FROM HProjectIteration where id = 1"); | ||
addEntry(tableNameAndQuery, "HDocument", "SELECT * FROM HDocument where id = 1"); | ||
addEntry(tableNameAndQuery, "HSimpleComment", "SELECT * FROM HSimpleComment where (id >=2 AND id <=11) or (id >=3947 AND id <= 3949)"); | ||
addEntry(tableNameAndQuery, "HPotEntryData", "SELECT * FROM HPotEntryData where id >= 1 AND id <=10"); | ||
addEntry(tableNameAndQuery, "HTextFlow", "SELECT * FROM HTextFlow where document_id = 1 AND pos < 10"); | ||
addEntry(tableNameAndQuery, "HTextFlowTarget", "SELECT * FROM HTextFlowTarget where tf_id >= 1 AND tf_id <= 10 AND locale = 3"); | ||
|
||
generateTestData("GetTransUnitListHandlerPerformanceTest", tableNameAndQuery); | ||
} | ||
|
||
private static void addEntry(List<Map.Entry<String, String>> tableNameAndQuery, String table, String query) | ||
{ | ||
tableNameAndQuery.add(new AbstractMap.SimpleEntry<String, String>(table, query)); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
zanata-war/src/test/java/org/zanata/webtrans/server/rpc/GetTransUnitListHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.zanata.webtrans.server.rpc; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
|
||
import org.dbunit.database.DatabaseConnection; | ||
import org.dbunit.database.IDatabaseConnection; | ||
import org.dbunit.database.QueryDataSet; | ||
import org.dbunit.dataset.xml.FlatDtdDataSet; | ||
import org.dbunit.dataset.xml.FlatXmlDataSet; | ||
import org.dbunit.operation.DatabaseOperation; | ||
import org.hibernate.Session; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
import org.zanata.ZanataDbunitJpaTest; | ||
import org.zanata.common.LocaleId; | ||
import org.zanata.dao.TextFlowDAO; | ||
import org.zanata.model.HLocale; | ||
import org.zanata.model.TestFixture; | ||
import org.zanata.rest.service.ResourceUtils; | ||
import org.zanata.seam.SeamAutowire; | ||
import org.zanata.security.ZanataIdentity; | ||
import org.zanata.service.LocaleService; | ||
import org.zanata.service.TextFlowSearchService; | ||
import org.zanata.webtrans.client.service.GetTransUnitActionContext; | ||
import org.zanata.webtrans.shared.auth.EditorClientId; | ||
import org.zanata.webtrans.shared.model.DocumentId; | ||
import org.zanata.webtrans.shared.model.ProjectIterationId; | ||
import org.zanata.webtrans.shared.rpc.GetTransUnitList; | ||
import org.zanata.webtrans.shared.rpc.GetTransUnitListResult; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import static org.mockito.Mockito.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
@Test(groups = { "jpa-tests" }) | ||
@Slf4j | ||
public class GetTransUnitListHandlerTest extends ZanataDbunitJpaTest | ||
{ | ||
private GetTransUnitListHandler handler; | ||
@Mock | ||
private ZanataIdentity identity; | ||
@Mock | ||
private LocaleService localeService; | ||
@Mock | ||
private TextFlowSearchService textFlowSearchServiceImpl; | ||
|
||
@Override | ||
protected void prepareDBUnitOperations() | ||
{ | ||
beforeTestOperations.add(new DataSetOperation("performance/GetTransUnitListHandlerPerformanceTest.dbunit.xml", DatabaseOperation.CLEAN_INSERT)); | ||
} | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception | ||
{ | ||
MockitoAnnotations.initMocks(this); | ||
TextFlowDAO dao = new TextFlowDAO((Session) getEm().getDelegate()); | ||
TransUnitTransformer transUnitTransformer = SeamAutowire.instance().use("resourceUtils", new ResourceUtils()).autowire(TransUnitTransformer.class); | ||
// @formatter:off | ||
handler = SeamAutowire.instance() | ||
.use("identity", identity) | ||
.use("localeServiceImpl", localeService) | ||
.use("textFlowDAO", dao) | ||
.use("transUnitTransformer", transUnitTransformer) | ||
.use("textFlowSearchServiceImpl", textFlowSearchServiceImpl) | ||
.autowire(GetTransUnitListHandler.class); | ||
// @formatter:on | ||
} | ||
|
||
@Test | ||
public void testExecute() throws Exception | ||
{ | ||
GetTransUnitList action = GetTransUnitList.newAction(new GetTransUnitActionContext(new DocumentId(1))); | ||
action.setEditorClientId(new EditorClientId("sessionId", 1)); | ||
action.setWorkspaceId(TestFixture.workspaceId(new LocaleId("ja"))); | ||
HLocale jaLocale = getEm().find(HLocale.class, 3L); | ||
ProjectIterationId projectIterationId = action.getWorkspaceId().getProjectIterationId(); | ||
when(localeService.validateLocaleByProjectIteration(action.getWorkspaceId().getLocaleId(), projectIterationId.getProjectSlug(), projectIterationId.getIterationSlug())).thenReturn(jaLocale); | ||
|
||
GetTransUnitListResult result = handler.execute(action, null); | ||
|
||
log.info("result: {}", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
zanata-war/src/test/resources/performance/GetTransUnitListHandlerPerformanceTest.dbunit.dtd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<!ELEMENT dataset ( | ||
HAccount*, | ||
HPerson*, | ||
HLocale*, | ||
HPoHeader*, | ||
HProject*, | ||
HProjectIteration*, | ||
HDocument*, | ||
HSimpleComment*, | ||
HPotEntryData*, | ||
HTextFlow*, | ||
HTextFlowTarget*)> | ||
|
||
<!ELEMENT HAccount EMPTY> | ||
<!ATTLIST HAccount | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
apiKey CDATA #IMPLIED | ||
enabled CDATA #REQUIRED | ||
passwordHash CDATA #IMPLIED | ||
username CDATA #IMPLIED | ||
mergedInto CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HPerson EMPTY> | ||
<!ATTLIST HPerson | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
email CDATA #REQUIRED | ||
name CDATA #REQUIRED | ||
accountId CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HLocale EMPTY> | ||
<!ATTLIST HLocale | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
active CDATA #REQUIRED | ||
localeId CDATA #REQUIRED | ||
enabledByDefault CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HPoHeader EMPTY> | ||
<!ATTLIST HPoHeader | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
entries CDATA #IMPLIED | ||
comment_id CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HProject EMPTY> | ||
<!ATTLIST HProject | ||
projecttype CDATA #REQUIRED | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
slug CDATA #REQUIRED | ||
description CDATA #IMPLIED | ||
homeContent CDATA #IMPLIED | ||
name CDATA #REQUIRED | ||
overrideLocales CDATA #REQUIRED | ||
status CDATA #IMPLIED | ||
restrictedByRoles CDATA #IMPLIED | ||
default_copy_trans_opts_id CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HProjectIteration EMPTY> | ||
<!ATTLIST HProjectIteration | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
slug CDATA #REQUIRED | ||
parentId CDATA #IMPLIED | ||
project_id CDATA #REQUIRED | ||
overrideLocales CDATA #REQUIRED | ||
status CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HDocument EMPTY> | ||
<!ATTLIST HDocument | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
contentType CDATA #REQUIRED | ||
docId CDATA #REQUIRED | ||
name CDATA #REQUIRED | ||
obsolete CDATA #REQUIRED | ||
path CDATA #REQUIRED | ||
revision CDATA #REQUIRED | ||
last_modified_by_id CDATA #IMPLIED | ||
locale CDATA #REQUIRED | ||
poHeader_id CDATA #IMPLIED | ||
project_iteration_id CDATA #REQUIRED | ||
> | ||
|
||
<!ELEMENT HSimpleComment EMPTY> | ||
<!ATTLIST HSimpleComment | ||
id CDATA #REQUIRED | ||
comment CDATA #REQUIRED | ||
> | ||
|
||
<!ELEMENT HPotEntryData EMPTY> | ||
<!ATTLIST HPotEntryData | ||
id CDATA #REQUIRED | ||
context CDATA #IMPLIED | ||
flags CDATA #IMPLIED | ||
refs CDATA #IMPLIED | ||
comment_id CDATA #IMPLIED | ||
tf_id CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HTextFlow EMPTY> | ||
<!ATTLIST HTextFlow | ||
id CDATA #REQUIRED | ||
obsolete CDATA #REQUIRED | ||
pos CDATA #REQUIRED | ||
resId CDATA #REQUIRED | ||
revision CDATA #REQUIRED | ||
comment_id CDATA #IMPLIED | ||
document_id CDATA #REQUIRED | ||
potEntryData_id CDATA #IMPLIED | ||
wordCount CDATA #REQUIRED | ||
contentHash CDATA #REQUIRED | ||
plural CDATA #IMPLIED | ||
content0 CDATA #IMPLIED | ||
content1 CDATA #IMPLIED | ||
content2 CDATA #IMPLIED | ||
content3 CDATA #IMPLIED | ||
content4 CDATA #IMPLIED | ||
content5 CDATA #IMPLIED | ||
> | ||
|
||
<!ELEMENT HTextFlowTarget EMPTY> | ||
<!ATTLIST HTextFlowTarget | ||
id CDATA #REQUIRED | ||
creationDate CDATA #REQUIRED | ||
lastChanged CDATA #REQUIRED | ||
versionNum CDATA #REQUIRED | ||
state CDATA #REQUIRED | ||
tf_revision CDATA #REQUIRED | ||
comment_id CDATA #IMPLIED | ||
last_modified_by_id CDATA #IMPLIED | ||
locale CDATA #REQUIRED | ||
tf_id CDATA #IMPLIED | ||
content0 CDATA #IMPLIED | ||
content1 CDATA #IMPLIED | ||
content2 CDATA #IMPLIED | ||
content3 CDATA #IMPLIED | ||
content4 CDATA #IMPLIED | ||
content5 CDATA #IMPLIED | ||
> | ||
|
Oops, something went wrong.