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