]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c3a1881c1abc2a337270a0cb3c34351a90e15b84
[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 = 10000;
95
96         @Test
97         public void performanceTest() {
98                 roundTripOverhead(10);
99                 deleteCollectionObjects();
100                 String[] coList = this.createCollectionObjects(MAX_RECORDS);
101                 this.searchCollectionObjects(MAX_RECORDS);
102 //              this.deleteCollectionObjects(coList);
103                 roundTripOverhead(10);
104         }
105         
106         private long roundTripOverhead(int numOfCalls) {
107                 long result = 0;
108                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
109                 
110                 long totalTime = 0;
111                 for (int i = 0; i < numOfCalls; i++) {
112                         Date startTime = new Date();
113                         collectionObjectClient.roundtrip();
114                         Date stopTime = new Date();
115                         totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
116                         System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
117                 }
118                 
119                 System.out.println("------------------------------------------------------------------------------");
120                 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
121                 System.out.println("------------------------------------------------------------------------------");
122                 System.out.println("");
123                 
124                 return result;
125         }
126         
127         private void searchCollectionObjects(int numberOfObjects) {
128                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
129                 Random randomGenerator = new Random(System.currentTimeMillis());                                
130                 ClientResponse<CollectionobjectsCommonList> searchResults;
131                 
132                 long totalTime = 0;
133                 long totalSearchResults = 0;
134                 String keywords = "";
135                 String times = "";
136                 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
137                                 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
138                         keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
139                         for (int i = 0; i < MAX_SEARCHES; i++) {
140                                 Date startTime = new Date();
141                                 searchResults = collectionObjectClient.keywordSearch(keywords);
142                                 Date stopTime = new Date();
143                                 long time = stopTime.getTime() - startTime.getTime();
144                                 times = times + " " + ((float)time / 1000);
145                                 totalTime = totalTime + time;                           
146                                 totalSearchResults = totalSearchResults +
147                                         searchResults.getEntity().getCollectionObjectListItem().size();
148                         }
149                         if (logger.isDebugEnabled()) {
150                                 System.out.println("------------------------------------------------------------------------------");
151                                 System.out.println("Searched Objects: " + numberOfObjects);
152                                 System.out.println("Number of keywords: " + numOfKeywords);
153                                 System.out.println("List of keywords: " + keywords);
154                                 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
155                                 System.out.println("Result times: " + times);
156                                 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
157                                 System.out.println("------------------------------------------------------------------------------");                                   
158                         }
159                 }
160                 return;
161         }
162         
163         private String createCollectionObject(CollectionObjectClient collectionObjectClient,
164                         int identifier) {
165                 String result = null;
166                 //
167                 // First create a CollectionObject
168                 //
169                 CollectionobjectsCommon co = new CollectionobjectsCommon();
170                 fillCollectionObject(co, Integer.toString(identifier));
171                 
172                 // Next, create a part object
173                 MultipartOutput multipart = new MultipartOutput();
174                 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
175                 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
176                 // Make the create call and check the response
177                 ClientResponse<Response> response = collectionObjectClient.create(multipart);
178                 
179                 int responseStatus = response.getStatus();
180                 if (logger.isDebugEnabled() == true) {
181                         if (responseStatus != Response.Status.CREATED.getStatusCode())
182                                 logger.debug("Status of call to create CollectionObject was: " +
183                                                 responseStatus);
184                 }
185                 
186                 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());             
187                 result = extractId(response);
188                 
189                 return result;
190         }
191         
192         public String[] createCollectionObjects(int numberOfObjects) {
193                 Random randomGenerator = new Random(System.currentTimeMillis());
194                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
195                 String[] coList = new String[numberOfObjects];          
196                 
197                 int createdObjects = 0;
198                 try {
199                         Date startTime = new Date();
200                         for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
201                                 coList[i] = createCollectionObject(collectionObjectClient, i + 1);
202                         }
203                         Date stopTime = new Date();
204                         if (logger.isDebugEnabled()) {
205                                 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
206                                                 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
207                         }
208                 } catch (AssertionError e) {
209                         System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
210                                         " before failing.");
211                         Assert.assertTrue(false);
212                 }
213                 
214                 return coList;
215         }
216         
217         private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
218                         String resourceId) {
219                 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);                       
220         }
221
222         public void deleteCollectionObjects(String[] arrayOfObjects) {
223                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
224
225                 Date startTime = new Date();            
226                 for (int i = 0; i < arrayOfObjects.length; i++) {
227                         deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
228                 }
229                 
230                 Date stopTime = new Date();
231                 if (logger.isDebugEnabled()) {
232                         System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
233                                         " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
234                 }
235         }
236         
237         public void deleteCollectionObjects() {
238                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
239                 ClientResponse<CollectionobjectsCommonList> commonsList;
240                 commonsList = collectionObjectClient.readList();
241                 List<CollectionObjectListItem> coListItems = commonsList.getEntity().getCollectionObjectListItem();
242                 
243                 Date startTime = new Date();
244                 for (CollectionObjectListItem i:coListItems) {
245                         deleteCollectionObject(collectionObjectClient, i.getCsid());
246                 }
247                 Date stopTime = new Date();
248                 
249                 if (logger.isDebugEnabled()) {
250                         System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
251                                         " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
252                 }
253         }
254
255 }