Commit 584d471cf114240d9e1e5130e14152ac79c0d14e

Authored by Paulo Graça
1 parent 0727c48e0a
Exists in tests

PG: performance tests

ptcrisync-test/resources/log4j2.xml
... ... @@ -9,8 +9,11 @@
9 9 <Logger name="log4j.test.Log4jTest" level="trace">
10 10 <AppenderRef ref="Console"/>
11 11 </Logger>
12   - <Root level="trace">
  12 + <Root level="info">
13 13 <AppenderRef ref="Console"/>
14   - </Root>
  14 + </Root>
  15 + <!-- Root level="trace">
  16 + <AppenderRef ref="Console"/>
  17 + </Root-->
15 18 </Loggers>
16 19 </Configuration>
... ...
ptcrisync-test/src/pt/ptcris/test/ExportTest.java
... ... @@ -43,7 +43,7 @@ public class ExportTest {
43 43 boolean testResult = false;
44 44  
45 45 String orcidId = properties.getProperty("orcid_id");
46   - boolean ignoreLimits = Boolean.getBoolean(properties.getProperty("test_ignore_limits"));
  46 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
47 47  
48 48 try {
49 49 _log.info("Prepare data for: " + orcidId);
... ... @@ -108,7 +108,7 @@ public class ExportTest {
108 108 boolean testResult = false;
109 109  
110 110 String orcidId = properties.getProperty("orcid_id");
111   - boolean ignoreLimits = Boolean.getBoolean(properties.getProperty("test_ignore_limits"));
  111 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
112 112  
113 113 try {
114 114 _log.info("Prepare data for: " + orcidId);
... ...
ptcrisync-test/src/pt/ptcris/test/ImportTest.java
... ... @@ -62,7 +62,8 @@ public class ImportTest {
62 62 List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
63 63 numberOfImportedWorks = remoteWorks.size();
64 64  
65   - _log.info("Imported records number: " + numberOfImportedWorks);
  65 + _log.info("Import works: " + numberOfWorks);
  66 + _log.info("Number of imported works: " + numberOfImportedWorks);
66 67 testResult = true;
67 68  
68 69 assertTrue("Number of exported that should match: " + numberOfWorks,
... ... @@ -87,7 +88,7 @@ public class ImportTest {
87 88 public void importfromOrcid() {
88 89 boolean testResult = false;
89 90 String orcidId = properties.getProperty("orcid_id");
90   - boolean ignoreLimits = Boolean.getBoolean(properties.getProperty("test_ignore_limits"));
  91 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
91 92  
92 93 _log.info("Prepare data for: " + orcidId);
93 94 ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
... ... @@ -120,11 +121,11 @@ public class ImportTest {
120 121  
121 122 numberOfWorks = helper.getAllWorkSummaries().size();
122 123  
123   - List<Work> remoteWorks = PTCRISync.importUpdates(orcidClient, localWorks, progressHandler);
  124 + List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
124 125 numberOfImportedWorks = remoteWorks.size();
125 126  
126 127 _log.info("Import works: " + numberOfWorks);
127   - _log.info("Imported records number: " + numberOfImportedWorks);
  128 + _log.info("Number of imported works: " + numberOfImportedWorks);
128 129 testResult = true;
129 130  
130 131 assertTrue("Number of exported that should match: " + numberOfWorks,
... ...
ptcrisync-test/src/pt/ptcris/test/PerformanceTest.java
... ... @@ -0,0 +1,183 @@
  1 +package pt.ptcris.test;
  2 +
  3 +import org.junit.Test;
  4 +import org.junit.runner.JUnitCore;
  5 +import org.junit.runner.Result;
  6 +import org.junit.runner.notification.Failure;
  7 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  8 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  9 +import org.um.dsi.gavea.orcid.model.work.Work;
  10 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
  11 +
  12 +import pt.ptcris.ORCIDClient;
  13 +import pt.ptcris.ORCIDClientImpl;
  14 +import pt.ptcris.ORCIDHelper;
  15 +import pt.ptcris.PTCRISync;
  16 +import pt.ptcris.test.dummy.ORCIDClientDummyImpl;
  17 +import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
  18 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  19 +import pt.ptcris.test.preparation.DefaultTestOrcidClient;
  20 +import pt.ptcris.test.preparation.WorkTestPrepare;
  21 +import pt.ptcris.test.properties.TestPropertiesLoader;
  22 +
  23 +import static org.junit.Assert.assertTrue;
  24 +
  25 +import java.math.BigInteger;
  26 +import java.text.DateFormat;
  27 +import java.text.SimpleDateFormat;
  28 +import java.util.Date;
  29 +import java.util.LinkedList;
  30 +import java.util.List;
  31 +import java.util.Properties;
  32 +
  33 +import javax.xml.bind.JAXBException;
  34 +
  35 +import org.apache.logging.log4j.LogManager;
  36 +import org.apache.logging.log4j.Logger;
  37 +
  38 +
  39 +public class PerformanceTest {
  40 + private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
  41 + private static final Logger _log = LogManager.getLogger(PerformanceTest.class);
  42 + private static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  43 + private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
  44 +
  45 + /**
  46 + * Empty ORCID profile and we want to measure the time of the export N works process from local app to ORCID
  47 + */
  48 + //@Test
  49 + public void exportTimetoOrcid() {
  50 + boolean testResult = false;
  51 + String orcidId = properties.getProperty("orcid_id");
  52 + boolean isTestClient = Boolean.parseBoolean(properties.getProperty("test_dummy_client"));
  53 +
  54 + _log.info("Prepare data for: " + orcidId);
  55 +
  56 + ORCIDClient orcidClient;
  57 + //To test the With and W/O the API
  58 + if (isTestClient) {
  59 + orcidClient = ORCIDClientDummyImpl.getDefaultClientInstance();
  60 + } else {
  61 + orcidClient = DefaultTestOrcidClient.getDefaultClient();
  62 + }
  63 +
  64 + Date date = new Date();
  65 + _log.info("Date: " + dateFormat.format(date));
  66 + _log.info("Version: " + PTCRISync.class.getPackage().getImplementationVersion());
  67 + try {
  68 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  69 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  70 +
  71 + //int nRecords = Integer.parseInt(properties.getProperty("test_export_limit"));
  72 + int nRecords = 5;
  73 + //Limit the numbers to export
  74 + int putCodesListSize = putCodesList.size();
  75 + if ( putCodesListSize > nRecords ) {
  76 + putCodesList.subList(nRecords, putCodesListSize).clear();
  77 + }
  78 +
  79 + List<Work> localWorks = WorkTestPrepare.loadLocalWorksFromResources(putCodesList);
  80 +
  81 + int numberOfLocalWorks = localWorks.size();
  82 + int numberOfExportedWorks = 0;
  83 +
  84 + try {
  85 + // Check the number of exported works
  86 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  87 +
  88 + _log.info("Deleting all existing works");
  89 + helper.deleteAllSourcedWorks();
  90 +
  91 + long start = System.nanoTime();
  92 + _log.info("Start testing: " + orcidId);
  93 + PTCRISync.export(orcidClient, localWorks, progressHandler);
  94 + _log.info("End testing: " + (((double)(System.nanoTime() - start))/1E9) + " sec");
  95 +
  96 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  97 + numberOfExportedWorks = exportedWorkSummary.size();
  98 +
  99 + _log.info("Localworks to export: " + numberOfLocalWorks);
  100 + _log.info("Exported content: " + numberOfExportedWorks);
  101 + testResult = true;
  102 +
  103 + assertTrue("Number of exported that should match: " + numberOfLocalWorks,
  104 + (numberOfExportedWorks == numberOfLocalWorks));
  105 + _log.info("[exportToNewProfile] " + "PASSED");
  106 + } catch (OrcidClientException orcidException) {
  107 + _log.fatal(orcidException.getDeveloperMessage());
  108 + _log.error("[exportToNewProfile] " + "FAILED");
  109 + } catch (Exception e) {
  110 + e.printStackTrace();
  111 + } finally {
  112 + System.out.println(progressHandler.getReport());
  113 + assertTrue("Export to new profile", testResult);
  114 + }
  115 +
  116 + } catch (JAXBException e) {
  117 + // TODO Auto-generated catch block
  118 + e.printStackTrace();
  119 + }
  120 +
  121 + }
  122 +
  123 + /**
  124 + * Empty Local APP and we want to measure the time of the import M works process from ORCID to an empty local app
  125 + */
  126 + @Test
  127 + public void importTimetoOrcid() {
  128 + boolean testResult = false;
  129 + String orcidId = properties.getProperty("orcid_id");
  130 + boolean isTestClient = Boolean.parseBoolean(properties.getProperty("test_dummy_client"));
  131 +
  132 + _log.info("Prepare data for: " + orcidId);
  133 + ORCIDClient orcidClient;
  134 + //To test the With and W/O the API
  135 + if (isTestClient) {
  136 + orcidClient = ORCIDClientDummyImpl.getDefaultClientInstance();
  137 + } else {
  138 + orcidClient = DefaultTestOrcidClient.getDefaultClient();
  139 + }
  140 +
  141 + // Empty list of local works
  142 + List<Work> localWorks = new LinkedList<Work>();
  143 +
  144 + int numberOfWorks = 0;
  145 + int numberOfImportedWorks = 0;
  146 +
  147 + try {
  148 + // Check the number of exported works
  149 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  150 +
  151 + numberOfWorks = helper.getAllWorkSummaries().size();
  152 +
  153 + long start = System.nanoTime();
  154 + _log.info("Start testing: " + orcidId);
  155 + List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
  156 + _log.info("End testing: " + (((double)(System.nanoTime() - start))/1E9) + " sec");
  157 + numberOfImportedWorks = remoteWorks.size();
  158 +
  159 + _log.info("Import works: " + numberOfWorks);
  160 + _log.info("Number of imported works: " + numberOfImportedWorks);
  161 + testResult = true;
  162 +
  163 + assertTrue("Number of exported that should match: " + numberOfWorks,
  164 + (numberOfImportedWorks == numberOfWorks));
  165 + _log.info("[exportToNewProfile] " + "PASSED");
  166 + } catch (OrcidClientException orcidException) {
  167 + _log.fatal(orcidException.getDeveloperMessage());
  168 + _log.error("[exportToNewProfile] " + "FAILED");
  169 + } catch (Exception e) {
  170 + e.printStackTrace();
  171 + } finally {
  172 + System.out.println(progressHandler.getReport());
  173 + assertTrue("Import new works", testResult);
  174 + }
  175 + }
  176 +
  177 + public static void main(String[] args) {
  178 + Result result = JUnitCore.runClasses(PerformanceTest.class);
  179 + for (Failure failure : result.getFailures()) {
  180 + System.out.println(failure.toString());
  181 + }
  182 + }
  183 +}
... ...
ptcrisync-test/src/pt/ptcris/test/dummy/ORCIDClientDummyImpl.java
... ... @@ -3,21 +3,34 @@ package pt.ptcris.test.dummy;
3 3 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
4 4  
5 5 import java.math.BigInteger;
  6 +import java.util.HashMap;
  7 +import java.util.LinkedList;
  8 +import java.util.List;
6 9 import java.util.Properties;
  10 +import java.util.Random;
  11 +
  12 +import javax.xml.bind.JAXBException;
7 13  
8 14 import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
9 15 import org.um.dsi.gavea.orcid.model.work.Work;
10 16  
11 17 import pt.ptcris.ORCIDClient;
  18 +import pt.ptcris.ORCIDHelper;
  19 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  20 +import pt.ptcris.test.preparation.WorkTestPrepare;
12 21 import pt.ptcris.test.properties.TestPropertiesLoader;
13 22  
14 23 import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  24 +import org.um.dsi.gavea.orcid.model.common.ClientId;
  25 +import org.um.dsi.gavea.orcid.model.common.OrcidId;
  26 +import org.um.dsi.gavea.orcid.model.common.SourceName;
  27 +import org.um.dsi.gavea.orcid.model.common.SourceType;
15 28  
16 29 /**
17   - * // TODO remove temporary client
18   - // ORCIDClient tempClient =
19   - // ORCIDClientDummyImpl.getDefaultClientInstance();
20   - // PTCRISync.export(tempClient, localWorks, progressHandler);
  30 + * // TODO remove temporary client // ORCIDClient tempClient = //
  31 + * ORCIDClientDummyImpl.getDefaultClientInstance(); //
  32 + * PTCRISync.export(tempClient, localWorks, progressHandler);
  33 + *
21 34 * @author pgraca
22 35 *
23 36 */
... ... @@ -26,55 +39,83 @@ public class ORCIDClientDummyImpl implements ORCIDClient {
26 39 private OrcidAccessToken orcidToken;
27 40 private String clientId;
28 41 private static ORCIDClient orcidClient = null;
  42 + private HashMap<BigInteger, Work> dummyWorks = new HashMap<BigInteger, Work>();
29 43  
30   - public ORCIDClientDummyImpl(String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri, OrcidAccessToken orcidToken) {
  44 + public ORCIDClientDummyImpl(String loginUri, String apiUri, String clientId, String clientSecret,
  45 + String redirectUri, OrcidAccessToken orcidToken) {
31 46 this.orcidToken = orcidToken;
32   - this.clientId = clientId;
  47 + this.clientId = clientId;
33 48 }
34   -
35   - public static ORCIDClient getDefaultClientInstance () {
36   - if (orcidClient != null) {
  49 +
  50 + public static ORCIDClient getDefaultClientInstance() {
  51 + if (orcidClient == null) {
37 52  
38 53 Properties properties = TestPropertiesLoader.getInstance().getProperties();
39   -
  54 +
40 55 OrcidAccessToken orcidToken = new OrcidAccessToken();
41   -
  56 +
42 57 orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
43   - orcidToken.setOrcid(properties.getProperty("orcid_id"));
44   -
45   - orcidClient = new ORCIDClientDummyImpl(properties.getProperty("orcid_login_uri"),
46   - properties.getProperty("orcid_api_uri"),
47   - properties.getProperty("orcid_client_id"),
48   - properties.getProperty("orcid_client_secret"),
49   - properties.getProperty("orcid_redirect_uri"), orcidToken);
  58 + orcidToken.setOrcid(properties.getProperty("orcid_id"));
  59 +
  60 + orcidClient = new ORCIDClientDummyImpl(properties.getProperty("orcid_login_uri"),
  61 + properties.getProperty("orcid_api_uri"), properties.getProperty("orcid_client_id"),
  62 + properties.getProperty("orcid_client_secret"), properties.getProperty("orcid_redirect_uri"),
  63 + orcidToken);
50 64 }
51   -
  65 +
52 66 return orcidClient;
53 67 }
54   -
55 68  
56 69 public Work getWork(BigInteger putCode) throws OrcidClientException {
57   - // TODO Auto-generated method stub
58   - return null;
  70 + if (dummyWorks.containsKey(putCode)) {
  71 + return dummyWorks.get(putCode);
  72 + } else {
  73 + List<BigInteger> putCodesList = new LinkedList<>();
  74 + putCodesList.add(putCode);
  75 + try {
  76 + List<Work> workList = WorkTestPrepare.loadLocalWorksFromResources(putCodesList);
  77 + dummyWorks.put(putCode,workList.get(0));
  78 + return workList.get(0);
  79 + } catch (JAXBException e) {
  80 + // TODO Auto-generated catch block
  81 + e.printStackTrace();
  82 + }
  83 + }
  84 + return null;
59 85 }
60 86  
61 87 public String addWork(Work work) throws OrcidClientException {
62   - // TODO Auto-generated method stub
63   - return null;
  88 + Random random = new Random();
  89 + BigInteger key = BigInteger.valueOf(random.nextLong());
  90 + work.setPutCode(key);
  91 + OrcidId orcidId = new OrcidId(this.clientId, this.clientId, this.clientId, this.clientId);
  92 + ClientId clientId = new ClientId(this.clientId, this.clientId, this.clientId, this.clientId);
  93 + SourceName sourceName = new SourceName(this.clientId);
  94 + SourceType sourceType = new SourceType(orcidId, clientId, sourceName);
  95 + work.setSource(sourceType);
  96 + dummyWorks.put(key, work);
  97 +
  98 + return key.toString();
64 99 }
65 100  
66 101 public void deleteWork(BigInteger putCode) throws OrcidClientException {
67 102 // TODO Auto-generated method stub
68   -
  103 + dummyWorks.remove(putCode);
69 104 }
70 105  
71 106 public void updateWork(BigInteger putCode, Work work) throws OrcidClientException {
72 107 // TODO Auto-generated method stub
73   -
  108 + dummyWorks.replace(putCode, work);
74 109 }
75 110  
76 111 public ActivitiesSummary getActivitiesSummary() throws OrcidClientException {
77   - // TODO Auto-generated method stub
  112 + try {
  113 + return ActivitiesSummaryTestPrepare.loadFromResources();
  114 +
  115 + } catch (JAXBException e) {
  116 + // TODO Auto-generated catch block
  117 + e.printStackTrace();
  118 + }
78 119 return null;
79 120 }
80 121  
... ... @@ -82,8 +123,4 @@ public class ORCIDClientDummyImpl implements ORCIDClient {
82 123 return this.clientId;
83 124 }
84 125  
85   -
86   -
87   -
88 126 }
89   -
... ...
ptcrisync-test/src/pt/ptcris/test/dummy/ProgressHandlerDummyImpl.java
1 1 package pt.ptcris.test.dummy;
2 2  
  3 +import java.util.List;
  4 +import java.util.Vector;
  5 +
  6 +import org.apache.logging.log4j.LogManager;
  7 +import org.apache.logging.log4j.Logger;
  8 +
3 9 import pt.ptcris.handlers.ProgressHandler;
4 10  
5 11 public class ProgressHandlerDummyImpl implements ProgressHandler {
  12 + private static final Logger _log = LogManager.getLogger(ProgressHandlerDummyImpl.class);
  13 + private long start_init = 0L;
  14 + private long start_process = 0L;
  15 + private long start_overall = 0L;
  16 + private Long time_init;
  17 + private Long time_overall;
  18 + private Long time_process;
  19 + private int n_executions = 0;
  20 + private List<Long> timeEachExecution;
  21 + private List<String> report = new Vector<String>();
6 22  
7 23 public void setProgress(int progress) {
  24 + if (progress == 0) {
  25 + timeEachExecution = new Vector<Long>();
  26 + }
  27 + if ((progress == 0) && (start_overall == 0L)) {
  28 + start_overall = System.nanoTime();
  29 + }
8 30 // TODO Auto-generated method stub
  31 + timeEachExecution.add(System.nanoTime());
  32 + n_executions++;
  33 +
9 34  
10 35 }
11 36  
12 37 public void setCurrentStatus(String message) {
13 38 // TODO Auto-generated method stub
  39 + _log.debug(message);
  40 + switch (message) {
  41 + case "ORCID_SYNC_EXPORT_STARTED":
  42 + case "ORCID_SYNC_IMPORT_WORKS_STARTED":
  43 + case "ORCID_SYNC_IMPORT_UPDATES_STARTED":
  44 + start_init = System.nanoTime();
  45 + break;
  46 + //case "ORCID_SYNC_EXPORT_UPDATING_WORKS":
  47 + //case "ORCID_SYNC_EXPORT_ADDING_WORKS":
  48 + case "ORCID_SYNC_EXPORT_WORKS_ITERATION":
  49 + case "ORCID_SYNC_IMPORT_WORKS_ITERATION":
  50 + case "ORCID_SYNC_IMPORT_UPDATES_ITERATION":
  51 + start_process = System.nanoTime();
  52 + default:
  53 + if (time_init == null) {
  54 + time_init = System.nanoTime() - start_init;
  55 + }
  56 + break;
  57 + }
14 58  
  59 +
15 60 }
16 61  
17 62 public void sendError(String message) {
18 63 // TODO Auto-generated method stub
  64 + _log.error(message);
19 65  
20 66 }
21 67  
22   - public void done() {
  68 + public void done() {
23 69 // TODO Auto-generated method stub
24   -
  70 + _log.info("Done");
  71 + if (time_process == null) {
  72 + time_process = System.nanoTime() - start_process;
  73 + }
  74 +
  75 + time_overall = System.nanoTime() - start_overall;
  76 +
  77 + report.add(generateReport());
  78 + time_init = null;
  79 + time_process = null;
  80 + }
  81 +
  82 + private String generateReport () {
  83 + String report = "{\"NUM\":" + (n_executions-1) + "}," +
  84 + "{\"TIME\":" + (((double)time_overall)/1E9) + "}," +
  85 + "{\"INIT\":" + (((double)time_init)/1E9) + "}," +
  86 + "{\"PROC\":" + (((double)time_process)/1E9) + "}," +
  87 + "{\"AVG\":" + (this.calculateAverage()/1E9) + "}," +
  88 + "{\"MAX\":" + (((double)this.calculateMax())/1E9) + "}"
  89 + ;
  90 + return report;
  91 + }
  92 +
  93 + private Long calculateMax () {
  94 + Long[] data = timeEachExecution.toArray(new Long[0]);
  95 + // Finding the largest element
  96 + long max = (data[1]-data[0]);
  97 + for (int i = 1; i < data.length-1; i++) {
  98 + if ((data[i+1]-data[i]) > max) max = (data[i+1]-data[i]);
  99 + }
  100 + return max;
  101 + }
  102 +
  103 + private double calculateAverage() {
  104 + Long[] data = (Long[]) timeEachExecution.toArray(new Long[0]);
  105 + // calculate sum
  106 + long sum = 0;
  107 + for (int i = 0; i < data.length-1; i++){
  108 + sum = sum + (data[i+1]-data[i]);
  109 + }
  110 + // calculate average
  111 + return (sum / (data.length-1));
  112 +
  113 + }
  114 +
  115 +
  116 + public String getReport () {
  117 + return report.toString();
25 118 }
  119 +
  120 +
26 121  
27 122 }
... ...