]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d0aed20865ceb8daa2f91f07e22249206afa94e0
[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.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Date;
29 import java.util.Random;
30
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.Marshaller;
36
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
41
42 import org.apache.commons.httpclient.Header;
43 import org.apache.commons.httpclient.HttpClient;
44 import org.apache.commons.httpclient.HttpException;
45 import org.apache.commons.httpclient.HttpStatus;
46 import org.apache.commons.httpclient.methods.GetMethod;
47 import org.apache.commons.httpclient.methods.HeadMethod;
48 import org.apache.commons.httpclient.methods.OptionsMethod;
49 import org.apache.commons.httpclient.methods.TraceMethod;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import org.jboss.resteasy.client.ClientResponse;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
59 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
60
61 import org.collectionspace.services.client.TestServiceClient;
62
63 import org.collectionspace.services.CollectionObjectJAXBSchema;
64 import org.collectionspace.services.client.CollectionObjectClient;
65 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
66 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
67 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
68
69 import org.collectionspace.services.IntakeJAXBSchema;
70 import org.collectionspace.services.client.IntakeClient;
71 import org.collectionspace.services.intake.IntakesCommon;
72 import org.collectionspace.services.intake.IntakesCommonList;
73
74 import org.collectionspace.services.common.relation.RelationJAXBSchema;
75 import org.collectionspace.services.client.RelationClient;
76 import org.collectionspace.services.relation.RelationsCommon;
77 import org.collectionspace.services.relation.RelationsCommonList;
78 import org.collectionspace.services.relation.RelationshipType;
79
80 /**
81  * A ServiceTest.
82  * 
83  * @version $Revision:$
84  */
85 public class PerformanceTest extends CollectionSpacePerformanceTest {
86
87         private static final int MAX_KEYWORDS = 10;
88         private static final int MAX_SEARCHES = 10;
89         final Logger logger = LoggerFactory
90                         .getLogger(PerformanceTest.class);
91         //
92         // Get clients for the CollectionSpace services
93         //
94         private static int MAX_RECORDS = 1000;
95
96         @Test
97         public void performanceTest() {
98             if(!isEnabled()) {
99                 logger.info("Performance tests are not enabled. Use mvn -Dcspace.perf -DforkMode=never test to enable");
100                 return;
101             }
102                 roundTripOverhead(10);
103                 deleteCollectionObjects();
104                 String[] coList = this.createCollectionObjects(MAX_RECORDS);
105                 this.searchCollectionObjects(MAX_RECORDS);
106 //              this.deleteCollectionObjects(coList);
107                 roundTripOverhead(10);
108         }
109         
110         private long roundTripOverhead(int numOfCalls) {
111                 long result = 0;
112                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
113                 
114                 long totalTime = 0;
115                 for (int i = 0; i < numOfCalls; i++) {
116                         Date startTime = new Date();
117                         collectionObjectClient.roundtrip();
118                         Date stopTime = new Date();
119                         totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
120                         System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
121                 }
122                 
123                 System.out.println("------------------------------------------------------------------------------");
124                 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
125                 System.out.println("------------------------------------------------------------------------------");
126                 System.out.println("");
127                 
128                 return result;
129         }
130         
131         private void searchCollectionObjects(int numberOfObjects) {
132                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
133                 Random randomGenerator = new Random(System.currentTimeMillis());                                
134                 ClientResponse<CollectionobjectsCommonList> searchResults;
135                 
136                 long totalTime = 0;
137                 long totalSearchResults = 0;
138                 String keywords = "";
139                 String times = "";
140                 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
141                                 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
142                         keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
143                         for (int i = 0; i < MAX_SEARCHES; i++) {
144                                 Date startTime = new Date();
145                                 searchResults = collectionObjectClient.keywordSearch(keywords);
146                                 Date stopTime = new Date();
147                                 long time = stopTime.getTime() - startTime.getTime();
148                                 times = times + " " + ((float)time / 1000);
149                                 totalTime = totalTime + time;                           
150                                 totalSearchResults = totalSearchResults +
151                                         searchResults.getEntity().getCollectionObjectListItem().size();
152                         }
153                         if (logger.isDebugEnabled()) {
154                                 System.out.println("------------------------------------------------------------------------------");
155                                 System.out.println("Searched Objects: " + numberOfObjects);
156                                 System.out.println("Number of keywords: " + numOfKeywords);
157                                 System.out.println("List of keywords: " + keywords);
158                                 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
159                                 System.out.println("Result times: " + times);
160                                 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
161                                 System.out.println("------------------------------------------------------------------------------");                                   
162                         }
163                 }
164                 return;
165         }
166         
167         private String createCollectionObject(CollectionObjectClient collectionObjectClient,
168                         int identifier) {
169                 String result = null;
170                 //
171                 // First create a CollectionObject
172                 //
173                 CollectionobjectsCommon co = new CollectionobjectsCommon();
174                 fillCollectionObject(co, Integer.toString(identifier));
175                 
176                 // Next, create a part object
177                 MultipartOutput multipart = new MultipartOutput();
178                 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
179                 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
180                 // Make the create call and check the response
181                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
182                 
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                 
193                 return result;
194         }
195         
196         public String[] createCollectionObjects(int numberOfObjects) {
197                 Random randomGenerator = new Random(System.currentTimeMillis());
198                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
199                 String[] coList = new String[numberOfObjects];          
200                 
201                 int createdObjects = 0;
202                 try {
203                         Date startTime = new Date();
204                         for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
205                                 coList[i] = createCollectionObject(collectionObjectClient, i + 1);
206                         }
207                         Date stopTime = new Date();
208                         if (logger.isDebugEnabled()) {
209                                 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
210                                                 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
211                         }
212                 } catch (AssertionError e) {
213                         System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
214                                         " before failing.");
215                         Assert.assertTrue(false);
216                 }
217                 
218                 return coList;
219         }
220         
221         private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
222                         String resourceId) {
223                 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);                       
224         }
225
226         public void deleteCollectionObjects(String[] arrayOfObjects) {
227                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
228
229                 Date startTime = new Date();            
230                 for (int i = 0; i < arrayOfObjects.length; i++) {
231                         deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
232                 }
233                 
234                 Date stopTime = new Date();
235                 if (logger.isDebugEnabled()) {
236                         System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
237                                         " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
238                 }
239         }
240         
241         public void deleteCollectionObjects() {
242                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
243                 ClientResponse<CollectionobjectsCommonList> commonsList;
244                 commonsList = collectionObjectClient.readList();
245                 List<CollectionObjectListItem> coListItems = commonsList.getEntity().getCollectionObjectListItem();
246                 
247                 Date startTime = new Date();
248                 for (CollectionObjectListItem i:coListItems) {
249                         deleteCollectionObject(collectionObjectClient, i.getCsid());
250                 }
251                 Date stopTime = new Date();
252                 
253                 if (logger.isDebugEnabled()) {
254                         System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
255                                         " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
256                 }
257         }
258
259 }