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