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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.PerformanceTests.test;
25 import java.util.List;
26 import java.util.Date;
27 import java.util.Random;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.testng.Assert;
33 import org.testng.annotations.Test;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
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;
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;
51 * @version $Revision:$
53 public class PerformanceTest extends CollectionSpacePerformanceTest {
55 /** The Constant MAX_KEYWORDS. */
56 private static final int MAX_KEYWORDS = 10;
58 /** The Constant MAX_SEARCHES. */
59 private static final int MAX_SEARCHES = 10;
62 final Logger logger = LoggerFactory
63 .getLogger(PerformanceTest.class);
65 // Get clients for the CollectionSpace services
67 /** The MA x_ records. */
68 private static int MAX_RECORDS = 100;
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);
84 * Round trip overhead.
86 * @param numOfCalls the num of calls
89 private long roundTripOverhead(int numOfCalls) {
91 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
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()));
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("");
111 * Search collection objects.
113 * @param numberOfObjects the number of objects
115 private void searchCollectionObjects(int numberOfObjects) {
116 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
117 Random randomGenerator = new Random(System.currentTimeMillis());
118 ClientResponse<CollectionobjectsCommonList> searchResults;
121 long totalSearchResults = 0;
122 String keywords = "";
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();
133 //extract the result list and release the ClientResponse
134 CollectionobjectsCommonList coListItem = null;
136 coListItem = searchResults.getEntity();
138 searchResults.releaseConnection();
141 long time = stopTime.getTime() - startTime.getTime();
142 times = times + " " + ((float)time / 1000);
143 totalTime = totalTime + time;
144 totalSearchResults = totalSearchResults +
145 coListItem.getCollectionObjectListItem().size();
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("------------------------------------------------------------------------------");
162 * Creates the collection object.
164 * @param collectionObjectClient the collection object client
165 * @param identifier the identifier
168 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
170 String result = null;
172 // First create a CollectionObject
174 CollectionobjectsCommon co = new CollectionobjectsCommon();
175 fillCollectionObject(co, Integer.toString(identifier));
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);
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: " +
191 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
192 result = extractId(response);
194 response.releaseConnection();
201 * Creates the collection objects.
203 * @param numberOfObjects the number of objects
204 * @return the string[]
206 public String[] createCollectionObjects(int numberOfObjects) {
207 Random randomGenerator = new Random(System.currentTimeMillis());
208 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
209 String[] coList = new String[numberOfObjects];
211 int createdObjects = 0;
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);
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.");
225 } catch (AssertionError e) {
226 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
228 Assert.assertTrue(false);
235 * Delete collection object.
237 * @param collectionObjectClient the collection object client
238 * @param resourceId the resource id
240 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
242 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
243 res.releaseConnection();
247 * Delete collection objects.
249 * @param arrayOfObjects the array of objects
251 public void deleteCollectionObjects(String[] arrayOfObjects) {
252 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
254 Date startTime = new Date();
255 for (int i = 0; i < arrayOfObjects.length; i++) {
256 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
258 Date stopTime = new Date();
260 if (logger.isDebugEnabled()) {
261 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
262 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
267 * Delete collection objects.
268 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
270 public void deleteCollectionObjects() {
271 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
272 ClientResponse<AbstractCommonList> response;
274 List<CollectionObjectListItem> coListItems = null;
276 response = collectionObjectClient.readList(Integer.toString(MAX_RECORDS),
277 Integer.toString(0));
279 CollectionobjectsCommonList commonListElement =
280 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
281 coListItems = commonListElement.getCollectionObjectListItem();
283 response.releaseConnection();
286 Date startTime = new Date();
287 for (CollectionObjectListItem i:coListItems) {
288 deleteCollectionObject(collectionObjectClient, i.getCsid());
290 Date stopTime = new Date();
292 if (logger.isDebugEnabled()) {
293 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
294 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
296 } while (coListItems.size() > 0);