]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b610d8c898edc6fbf319f1bf74f3cdf9ac1757b8
[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 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.util.HttpResponseCodes;
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         ClientResponse<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         ClientResponse<AbstractCommonList> searchResults;
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                 searchResults = collectionObjectClient.keywordSearchIncludeDeleted(keywords, NOT_INCLUDING_DELETED_RESOURCES);
133                 Date stopTime = new Date();
134
135                 //extract the result list and release the ClientResponse
136                 AbstractCommonList coListItem = null;
137                 try {
138                     coListItem = searchResults.getEntity();
139                 } finally {
140                     searchResults.close();
141                 }
142
143                 long time = stopTime.getTime() - startTime.getTime();
144                 times = times + " " + ((float) time / 1000);
145                 totalTime = totalTime + time;
146                 totalSearchResults = totalSearchResults
147                         + coListItem.getListItem().size();
148             }
149             if (logger.isDebugEnabled()) {
150                 System.out.println("------------------------------------------------------------------------------");
151                 System.out.println("Searched Objects: " + numberOfObjects);
152                 System.out.println("Number of keywords: " + numOfKeywords);
153                 System.out.println("List of keywords: " + keywords);
154                 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
155                 System.out.println("Result times: " + times);
156                 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
157                 System.out.println("------------------------------------------------------------------------------");
158             }
159         }
160         return;
161     }
162
163     /**
164      * Creates the collection object.
165      *
166      * @param collectionObjectClient the collection object client
167      * @param identifier the identifier
168      * @return the string
169      */
170     private String createCollectionObject(CollectionObjectClient collectionObjectClient,
171             PoxPayloadOut multipart) {
172         String result = null;
173         // Make the create call and check the response
174         Response response = collectionObjectClient.create(multipart);
175         try {
176             int responseStatus = response.getStatus();
177             if (logger.isDebugEnabled() == true) {
178                 if (responseStatus != Response.Status.CREATED.getStatusCode()) {
179                     logger.debug("Status of call to create CollectionObject was: "
180                             + responseStatus);
181                 }
182             }
183
184             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
185             result = extractId(response);
186         } finally {
187             response.close();
188         }
189
190         return result;
191     }
192
193     /**
194      * Creates the collection objects.
195      *
196      * @param numberOfObjects the number of objects
197      * @return the string[]
198      */
199     public String[] createCollectionObjects(int numberOfObjects) {
200         Random randomGenerator = new Random(System.currentTimeMillis());
201         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
202         String[] coList = new String[numberOfObjects];
203
204         //
205         // First create a CollectionObject
206         //
207         CollectionobjectsCommon co = new CollectionobjectsCommon();
208         fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
209
210         // Next, create a part object
211         PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
212         PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
213         commonPart.setLabel(collectionObjectClient.getCommonPartName());
214
215         int createdObjects = 0;
216         try {
217             Date startTime = new Date();
218             for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
219                 coList[i] = createCollectionObject(collectionObjectClient, multipart);
220                 if (logger.isDebugEnabled() == true) {
221                     //
222                     // Print out a status every 10 operations
223                     if (i % 10 == 0) {
224                         logger.debug("Created CollectionObject #: " + i);
225                     }
226                 }
227             }
228             Date stopTime = new Date();
229             if (logger.isDebugEnabled()) {
230                 System.out.println("Created " + numberOfObjects + " CollectionObjects"
231                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
232             }
233         } catch (AssertionError e) {
234             System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects
235                     + " before failing.");
236             Assert.assertTrue(false);
237         }
238
239         return coList;
240     }
241     //
242     //
243     //
244
245     /**
246      * Delete collection object.
247      *
248      * @param collectionObjectClient the collection object client
249      * @param resourceId the resource id
250      */
251     private void readCollectionObject(CollectionObjectClient collectionObjectClient,
252             String resourceId) {
253         Response res = collectionObjectClient.read(resourceId);
254         res.close();
255     }
256
257     /**
258      * Delete collection objects.
259      *
260      * @param arrayOfObjects the array of objects
261      */
262     public void readCollectionObjects(String[] arrayOfObjects) {
263         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
264
265         Date startTime = new Date();
266         for (int i = 0; i < arrayOfObjects.length; i++) {
267             readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
268         }
269         Date stopTime = new Date();
270
271         if (logger.isDebugEnabled()) {
272             System.out.println("Read " + arrayOfObjects.length + " CollectionObjects"
273                     + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
274         }
275     }
276
277     /**
278      * Delete collection objects.
279      * FIXME: Deletes a page at a time until there are no more CollectionObjects.
280      */
281     public void readCollectionObjects() {
282         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
283
284         List<AbstractCommonList.ListItem> coListItems = null;
285         do {
286             Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
287                     new Long(0));
288             try {
289                 AbstractCommonList commonListElement =
290                         (AbstractCommonList) res.readEntity(AbstractCommonList.class);
291                 coListItems = commonListElement.getListItem();
292             } finally {
293                 res.close();
294             }
295
296             Date startTime = new Date();
297             for (AbstractCommonList.ListItem i : coListItems) {
298                 readCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
299             }
300             Date stopTime = new Date();
301
302             if (logger.isDebugEnabled()) {
303                 System.out.println("Read " + coListItems.size() + " CollectionObjects"
304                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
305             }
306         } while (coListItems.size() > 0);
307     }
308
309     //
310     //
311     //
312     /**
313      * Delete collection object.
314      *
315      * @param collectionObjectClient the collection object client
316      * @param resourceId the resource id
317      */
318     private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
319             String resourceId) {
320         collectionObjectClient.delete(resourceId).close();
321     }
322
323     /**
324      * Delete collection objects.
325      *
326      * @param arrayOfObjects the array of objects
327      */
328     private void deleteCollectionObjects(String[] arrayOfObjects) {
329         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
330
331         Date startTime = new Date();
332         for (int i = 0; i < arrayOfObjects.length; i++) {
333             deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
334         }
335         Date stopTime = new Date();
336
337         if (logger.isDebugEnabled()) {
338             System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects"
339                     + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
340         }
341     }
342
343     /**
344      * Delete collection objects.
345      * FIXME: Deletes a page at a time until there are no more CollectionObjects.
346      */
347     private void deleteCollectionObjects() {
348         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
349
350         List<AbstractCommonList.ListItem> coListItems = null;
351         do {
352             Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
353                     new Long(0));
354             try {
355                 AbstractCommonList commonListElement =
356                         (AbstractCommonList) res.readEntity(AbstractCommonList.class);
357                 coListItems = commonListElement.getListItem();
358             } finally {
359                 res.close();
360             }
361
362             Date startTime = new Date();
363             for (AbstractCommonList.ListItem i : coListItems) {
364                 deleteCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
365             }
366             Date stopTime = new Date();
367
368             if (logger.isDebugEnabled()) {
369                 System.out.println("Deleted " + coListItems.size() + " CollectionObjects"
370                         + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
371             }
372         } while (coListItems.size() > 0);
373     }
374 }