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