]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c9d6821095f839d1c2180f60c681cd713d2a3447
[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 = 1;
70
71         /**
72          * Performance test.
73          */
74         @Test
75         public void performanceTest() {
76 //              roundTripOverhead(MAX_RECORDS);
77 //              deleteCollectionObjects();
78                 String[] coList = this.createCollectionObjects(MAX_RECORDS);
79 //              this.searchCollectionObjects(MAX_RECORDS);
80 //              this.deleteCollectionObjects(coList);
81 //              roundTripOverhead(10);
82         }
83         
84         /**
85          * Round trip overhead.
86          *
87          * @param numOfCalls the num of calls
88          * @return the long
89          */
90         private long roundTripOverhead(int numOfCalls) {
91                 long result = 0;
92                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
93                 
94                 long totalTime = 0;
95                 ClientResponse<Response> response;
96                 for (int i = 0; i < numOfCalls; i++) {
97                         Date startTime = new Date();                    
98                         response = collectionObjectClient.roundtrip();
99                         try {
100                                 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
101                         } finally {
102                                 response.releaseConnection();
103                         }
104                         Date stopTime = new Date();
105                         totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
106                         System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
107                 }
108                 
109                 System.out.println("------------------------------------------------------------------------------");
110                 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
111                 System.out.println("------------------------------------------------------------------------------");
112                 System.out.println("");
113                 
114                 return result;
115         }
116         
117         /**
118          * Search collection objects.
119          *
120          * @param numberOfObjects the number of objects
121          */
122         private void searchCollectionObjects(int numberOfObjects) {
123                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
124                 Random randomGenerator = new Random(System.currentTimeMillis());                                
125                 ClientResponse<CollectionobjectsCommonList> searchResults;
126                 
127                 long totalTime = 0;
128                 long totalSearchResults = 0;
129                 String keywords = "";
130                 String times = "";
131                 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
132                                 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
133                         keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
134                         for (int i = 0; i < MAX_SEARCHES; i++) {
135                                 //sandwich the call with timestamps
136                                 Date startTime = new Date();
137                                 searchResults = collectionObjectClient.keywordSearch(keywords);                         
138                                 Date stopTime = new Date();
139                                 
140                                 //extract the result list and release the ClientResponse
141                                 CollectionobjectsCommonList coListItem = null;
142                                 try {
143                                         coListItem = searchResults.getEntity();
144                                 } finally {
145                                         searchResults.releaseConnection();
146                                 }
147                                 
148                                 long time = stopTime.getTime() - startTime.getTime();
149                                 times = times + " " + ((float)time / 1000);
150                                 totalTime = totalTime + time;                           
151                                 totalSearchResults = totalSearchResults + 
152                                         coListItem.getCollectionObjectListItem().size();
153                         }
154                         if (logger.isDebugEnabled()) {
155                                 System.out.println("------------------------------------------------------------------------------");
156                                 System.out.println("Searched Objects: " + numberOfObjects);
157                                 System.out.println("Number of keywords: " + numOfKeywords);
158                                 System.out.println("List of keywords: " + keywords);
159                                 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
160                                 System.out.println("Result times: " + times);
161                                 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
162                                 System.out.println("------------------------------------------------------------------------------");                                   
163                         }
164                 }
165                 return;
166         }
167         
168         /**
169          * Creates the collection object.
170          *
171          * @param collectionObjectClient the collection object client
172          * @param identifier the identifier
173          * @return the string
174          */
175         private String createCollectionObject(CollectionObjectClient collectionObjectClient,
176                         int identifier) {
177                 String result = null;
178                 //
179                 // First create a CollectionObject
180                 //
181                 CollectionobjectsCommon co = new CollectionobjectsCommon();
182                 fillCollectionObject(co, Integer.toString(identifier));
183                 
184                 // Next, create a part object
185                 MultipartOutput multipart = new MultipartOutput();
186                 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
187                 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
188                 // Make the create call and check the response
189                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
190                 try {
191                         int responseStatus = response.getStatus();
192                         if (logger.isDebugEnabled() == true) {
193                                 if (responseStatus != Response.Status.CREATED.getStatusCode())
194                                         logger.debug("Status of call to create CollectionObject was: " +
195                                                         responseStatus);
196                         }
197                         
198                         Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());             
199                         result = extractId(response);
200                 } finally {
201                         response.releaseConnection();
202                 }
203                 
204                 return result;
205         }
206         
207         /**
208          * Creates the collection objects.
209          *
210          * @param numberOfObjects the number of objects
211          * @return the string[]
212          */
213         public String[] createCollectionObjects(int numberOfObjects) {
214                 Random randomGenerator = new Random(System.currentTimeMillis());
215                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
216                 String[] coList = new String[numberOfObjects];          
217                 
218                 int createdObjects = 0;
219                 try {
220                         Date startTime = new Date();
221                         for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
222                                 coList[i] = createCollectionObject(collectionObjectClient, i + 1);
223                                 if (logger.isDebugEnabled() == true) {
224                                         logger.debug("Created CollectionObject #: " + i);
225                                 }
226                         }
227                         Date stopTime = new Date();
228                         if (logger.isDebugEnabled()) {
229                                 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
230                                                 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
231                         }
232                 } catch (AssertionError e) {
233                         System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
234                                         " before failing.");
235                         Assert.assertTrue(false);
236                 }
237                 
238                 return coList;
239         }
240         
241         /**
242          * Delete collection object.
243          *
244          * @param collectionObjectClient the collection object client
245          * @param resourceId the resource id
246          */
247         private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
248                         String resourceId) {
249                 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
250                 res.releaseConnection();
251         }
252
253         /**
254          * Delete collection objects.
255          *
256          * @param arrayOfObjects the array of objects
257          */
258         public void deleteCollectionObjects(String[] arrayOfObjects) {
259                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
260
261                 Date startTime = new Date();            
262                 for (int i = 0; i < arrayOfObjects.length; i++) {
263                         deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
264                 }               
265                 Date stopTime = new Date();
266                 
267                 if (logger.isDebugEnabled()) {
268                         System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
269                                         " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
270                 }
271         }
272         
273         /**
274          * Delete collection objects.
275          * FIXME: Deletes a page at a time until there are no more CollectionObjects.
276          */
277         public void deleteCollectionObjects() {
278                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
279                 ClientResponse<AbstractCommonList> response;
280                 
281                 List<CollectionObjectListItem> coListItems = null;              
282                 do {
283                         response = collectionObjectClient.readList(Integer.toString(MAX_RECORDS),
284                                         Integer.toString(0));
285                         try {
286                                 CollectionobjectsCommonList commonListElement = 
287                                         (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
288                                 coListItems = commonListElement.getCollectionObjectListItem();
289                         } finally {
290                                 response.releaseConnection();
291                         }
292                         
293                         Date startTime = new Date();
294                         for (CollectionObjectListItem i:coListItems) {
295                                 deleteCollectionObject(collectionObjectClient, i.getCsid());
296                         }
297                         Date stopTime = new Date();
298                         
299                         if (logger.isDebugEnabled()) {
300                                 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
301                                                 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
302                         }
303                 } while (coListItems.size() > 0);
304         }
305
306 }