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