]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
56357790340b9b6c8b59d4d03f55c6de39482ef5
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 package org.collectionspace.services.PerformanceTests.test;
24
25 import java.util.List;
26 import java.util.Date;
27 import java.util.Random;
28
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.testng.Assert;
33 import org.testng.annotations.Test;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import org.jboss.resteasy.client.ClientResponse;
39 import org.jboss.resteasy.util.HttpResponseCodes;
40
41 import org.collectionspace.services.client.AbstractCommonListUtils;
42 import org.collectionspace.services.client.CollectionObjectClient;
43 import org.collectionspace.services.client.PayloadOutputPart;
44 import org.collectionspace.services.client.PoxPayloadOut;
45 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47
48 /**
49  * A ServiceTest.
50  * 
51  * @version $Revision:$
52  */
53 public class PerformanceTest extends CollectionSpacePerformanceTest {
54
55     /** The Constant MAX_KEYWORDS. */
56     private static final int MAX_KEYWORDS = 10;
57     /** The Constant MAX_SEARCHES. */
58     private static final int MAX_SEARCHES = 10;
59     /** The logger. */
60     final Logger logger = LoggerFactory.getLogger(PerformanceTest.class);
61     //
62     // Get clients for the CollectionSpace services
63     //
64     /** The MA x_ records. */
65     private static int MAX_RECORDS = 100;
66
67     /**
68      * Performance test.
69      */
70     @Test
71     public void performanceTest() {
72         roundTripOverhead(10);
73         deleteCollectionObjects();
74         String[] coList = this.createCollectionObjects(MAX_RECORDS);
75         this.searchCollectionObjects(MAX_RECORDS);
76         this.readCollectionObjects(coList);
77         //this.deleteCollectionObjects(coList);
78         roundTripOverhead(10);
79     }
80
81     /**
82      * Round trip overhead.
83      *
84      * @param numOfCalls the num of calls
85      * @return the long
86      */
87     private long roundTripOverhead(int numOfCalls) {
88         long result = 0;
89         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
90
91         long totalTime = 0;
92         ClientResponse<Response> response;
93         for (int i = 0; i < numOfCalls; i++) {
94             Date startTime = new Date();
95             response = collectionObjectClient.roundtrip(0);
96             try {
97                 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
98             } finally {
99                 response.releaseConnection();
100             }
101             Date stopTime = new Date();
102             totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
103             System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
104         }
105
106         System.out.println("------------------------------------------------------------------------------");
107         System.out.println("Client to server roundtrip overhead: " + (float) (totalTime / numOfCalls) / 1000);
108         System.out.println("------------------------------------------------------------------------------");
109         System.out.println("");
110
111         return result;
112     }
113
114     /**
115      * Search collection objects.
116      *
117      * @param numberOfObjects the number of objects
118      */
119     private void searchCollectionObjects(int numberOfObjects) {
120         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
121         Random randomGenerator = new Random(System.currentTimeMillis());
122         ClientResponse<AbstractCommonList> searchResults;
123
124         long totalTime = 0;
125         long totalSearchResults = 0;
126         String keywords = "";
127         String times = "";
128         final boolean NOT_INCLUDING_DELETED_RESOURCES = false;
129         for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
130                 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
131             keywords = keywords + " " + OBJECT_TITLE + randomGenerator.nextInt(numberOfObjects);
132             for (int i = 0; i < MAX_SEARCHES; i++) {
133                 //sandwich the call with timestamps
134                 Date startTime = new Date();
135                 searchResults = collectionObjectClient.keywordSearchIncludeDeleted(keywords, NOT_INCLUDING_DELETED_RESOURCES);
136                 Date stopTime = new Date();
137
138                 //extract the result list and release the ClientResponse
139                 AbstractCommonList coListItem = null;
140                 try {
141                     coListItem = searchResults.getEntity();
142                 } finally {
143                     searchResults.releaseConnection();
144                 }
145
146                 long time = stopTime.getTime() - startTime.getTime();
147                 times = times + " " + ((float) time / 1000);
148                 totalTime = totalTime + time;
149                 totalSearchResults = totalSearchResults
150                         + coListItem.getListItem().size();
151             }
152             if (logger.isDebugEnabled()) {
153                 System.out.println("------------------------------------------------------------------------------");
154                 System.out.println("Searched Objects: " + numberOfObjects);
155                 System.out.println("Number of keywords: " + numOfKeywords);
156                 System.out.println("List of keywords: " + keywords);
157                 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
158                 System.out.println("Result times: " + times);
159                 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
160                 System.out.println("------------------------------------------------------------------------------");
161             }
162         }
163         return;
164     }
165
166     /**
167      * Creates the collection object.
168      *
169      * @param collectionObjectClient the collection object client
170      * @param identifier the identifier
171      * @return the string
172      */
173     private String createCollectionObject(CollectionObjectClient collectionObjectClient,
174             PoxPayloadOut multipart) {
175         String result = null;
176         // Make the create call and check the response
177         Response response = collectionObjectClient.create(multipart);
178         try {
179             int responseStatus = response.getStatus();
180             if (logger.isDebugEnabled() == true) {
181                 if (responseStatus != Response.Status.CREATED.getStatusCode()) {
182                     logger.debug("Status of call to create CollectionObject was: "
183                             + responseStatus);
184                 }
185             }
186
187             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
188             result = extractId(response);
189         } finally {
190             response.close();
191         }
192
193         return result;
194     }
195
196     /**
197      * Creates the collection objects.
198      *
199      * @param numberOfObjects the number of objects
200      * @return the string[]
201      */
202     public String[] createCollectionObjects(int numberOfObjects) {
203         Random randomGenerator = new Random(System.currentTimeMillis());
204         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
205         String[] coList = new String[numberOfObjects];
206
207         //
208         // First create a CollectionObject
209         //
210         CollectionobjectsCommon co = new CollectionobjectsCommon();
211         fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
212
213         // Next, create a part object
214         PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
215         PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
216         commonPart.setLabel(collectionObjectClient.getCommonPartName());
217
218         int createdObjects = 0;
219         try {
220             Date startTime = new Date();
221             for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
222                 coList[i] = createCollectionObject(collectionObjectClient, multipart);
223                 if (logger.isDebugEnabled() == true) {
224                     //
225                     // Print out a status every 10 operations
226                     if (i % 10 == 0) {
227                         logger.debug("Created CollectionObject #: " + i);
228                     }
229                 }
230             }
231             Date stopTime = new Date();
232             if (logger.isDebugEnabled()) {
233                 System.out.println("Created " + numberOfObjects + " CollectionObjects"
234                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
235             }
236         } catch (AssertionError e) {
237             System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects
238                     + " before failing.");
239             Assert.assertTrue(false);
240         }
241
242         return coList;
243     }
244     //
245     //
246     //
247
248     /**
249      * Delete collection object.
250      *
251      * @param collectionObjectClient the collection object client
252      * @param resourceId the resource id
253      */
254     private void readCollectionObject(CollectionObjectClient collectionObjectClient,
255             String resourceId) {
256         ClientResponse<String> res = collectionObjectClient.read(resourceId);
257         res.releaseConnection();
258     }
259
260     /**
261      * Delete collection objects.
262      *
263      * @param arrayOfObjects the array of objects
264      */
265     public void readCollectionObjects(String[] arrayOfObjects) {
266         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
267
268         Date startTime = new Date();
269         for (int i = 0; i < arrayOfObjects.length; i++) {
270             readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
271         }
272         Date stopTime = new Date();
273
274         if (logger.isDebugEnabled()) {
275             System.out.println("Read " + arrayOfObjects.length + " CollectionObjects"
276                     + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
277         }
278     }
279
280     /**
281      * Delete collection objects.
282      * FIXME: Deletes a page at a time until there are no more CollectionObjects.
283      */
284     public void readCollectionObjects() {
285         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
286         ClientResponse<AbstractCommonList> response;
287
288         List<AbstractCommonList.ListItem> coListItems = null;
289         do {
290             response = collectionObjectClient.readList(new Long(MAX_RECORDS),
291                     new Long(0));
292             try {
293                 AbstractCommonList commonListElement =
294                         (AbstractCommonList) response.getEntity(AbstractCommonList.class);
295                 coListItems = commonListElement.getListItem();
296             } finally {
297                 response.releaseConnection();
298             }
299
300             Date startTime = new Date();
301             for (AbstractCommonList.ListItem i : coListItems) {
302                 readCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
303             }
304             Date stopTime = new Date();
305
306             if (logger.isDebugEnabled()) {
307                 System.out.println("Read " + coListItems.size() + " CollectionObjects"
308                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
309             }
310         } while (coListItems.size() > 0);
311     }
312
313     //
314     //
315     //
316     /**
317      * Delete collection object.
318      *
319      * @param collectionObjectClient the collection object client
320      * @param resourceId the resource id
321      */
322     private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
323             String resourceId) {
324         ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
325         res.releaseConnection();
326     }
327
328     /**
329      * Delete collection objects.
330      *
331      * @param arrayOfObjects the array of objects
332      */
333     private void deleteCollectionObjects(String[] arrayOfObjects) {
334         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
335
336         Date startTime = new Date();
337         for (int i = 0; i < arrayOfObjects.length; i++) {
338             deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
339         }
340         Date stopTime = new Date();
341
342         if (logger.isDebugEnabled()) {
343             System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects"
344                     + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
345         }
346     }
347
348     /**
349      * Delete collection objects.
350      * FIXME: Deletes a page at a time until there are no more CollectionObjects.
351      */
352     private void deleteCollectionObjects() {
353         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
354         ClientResponse<AbstractCommonList> response;
355
356         List<AbstractCommonList.ListItem> coListItems = null;
357         do {
358             response = collectionObjectClient.readList(new Long(MAX_RECORDS),
359                     new Long(0));
360             try {
361                 AbstractCommonList commonListElement =
362                         (AbstractCommonList) response.getEntity(AbstractCommonList.class);
363                 coListItems = commonListElement.getListItem();
364             } finally {
365                 response.releaseConnection();
366             }
367
368             Date startTime = new Date();
369             for (AbstractCommonList.ListItem i : coListItems) {
370                 deleteCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
371             }
372             Date stopTime = new Date();
373
374             if (logger.isDebugEnabled()) {
375                 System.out.println("Deleted " + coListItems.size() + " CollectionObjects"
376                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
377             }
378         } while (coListItems.size() > 0);
379     }
380 }