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