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.util.HttpResponseCodes;
41 import org.collectionspace.services.client.AbstractCommonListUtils;
42 import org.collectionspace.services.client.CollectionObjectClient;
43 import org.collectionspace.services.client.PayloadOutputPart;
44 import org.collectionspace.services.client.PoxPayloadOut;
45 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
51 * @version $Revision:$
53 public class PerformanceTest extends CollectionSpacePerformanceTest {
55 /** The Constant MAX_KEYWORDS. */
56 private static final int MAX_KEYWORDS = 10;
57 /** The Constant MAX_SEARCHES. */
58 private static final int MAX_SEARCHES = 10;
60 final Logger logger = LoggerFactory.getLogger(PerformanceTest.class);
62 // Get clients for the CollectionSpace services
64 /** The MA x_ records. */
65 private static int MAX_RECORDS = 100;
71 public void performanceTest() {
72 roundTripOverhead(10);
73 deleteCollectionObjects();
74 String[] coList = this.createCollectionObjects(MAX_RECORDS);
75 this.searchCollectionObjects(MAX_RECORDS);
76 this.readCollectionObjects(coList);
77 //this.deleteCollectionObjects(coList);
78 roundTripOverhead(10);
82 * Round trip overhead.
84 * @param numOfCalls the num of calls
87 private long roundTripOverhead(int numOfCalls) {
89 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
92 ClientResponse<Response> response;
93 for (int i = 0; i < numOfCalls; i++) {
94 Date startTime = new Date();
95 response = collectionObjectClient.roundtrip(0);
97 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
99 response.releaseConnection();
101 Date stopTime = new Date();
102 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
103 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
106 System.out.println("------------------------------------------------------------------------------");
107 System.out.println("Client to server roundtrip overhead: " + (float) (totalTime / numOfCalls) / 1000);
108 System.out.println("------------------------------------------------------------------------------");
109 System.out.println("");
115 * Search collection objects.
117 * @param numberOfObjects the number of objects
119 private void searchCollectionObjects(int numberOfObjects) {
120 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
121 Random randomGenerator = new Random(System.currentTimeMillis());
122 ClientResponse<AbstractCommonList> searchResults;
125 long totalSearchResults = 0;
126 String keywords = "";
128 final boolean NOT_INCLUDING_DELETED_RESOURCES = false;
129 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
130 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
131 keywords = keywords + " " + OBJECT_TITLE + randomGenerator.nextInt(numberOfObjects);
132 for (int i = 0; i < MAX_SEARCHES; i++) {
133 //sandwich the call with timestamps
134 Date startTime = new Date();
135 searchResults = collectionObjectClient.keywordSearchIncludeDeleted(keywords, NOT_INCLUDING_DELETED_RESOURCES);
136 Date stopTime = new Date();
138 //extract the result list and release the ClientResponse
139 AbstractCommonList coListItem = null;
141 coListItem = searchResults.getEntity();
143 searchResults.releaseConnection();
146 long time = stopTime.getTime() - startTime.getTime();
147 times = times + " " + ((float) time / 1000);
148 totalTime = totalTime + time;
149 totalSearchResults = totalSearchResults
150 + coListItem.getListItem().size();
152 if (logger.isDebugEnabled()) {
153 System.out.println("------------------------------------------------------------------------------");
154 System.out.println("Searched Objects: " + numberOfObjects);
155 System.out.println("Number of keywords: " + numOfKeywords);
156 System.out.println("List of keywords: " + keywords);
157 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
158 System.out.println("Result times: " + times);
159 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
160 System.out.println("------------------------------------------------------------------------------");
167 * Creates the collection object.
169 * @param collectionObjectClient the collection object client
170 * @param identifier the identifier
173 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
174 PoxPayloadOut multipart) {
175 String result = null;
176 // Make the create call and check the response
177 Response response = collectionObjectClient.create(multipart);
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: "
187 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
188 result = extractId(response);
197 * Creates the collection objects.
199 * @param numberOfObjects the number of objects
200 * @return the string[]
202 public String[] createCollectionObjects(int numberOfObjects) {
203 Random randomGenerator = new Random(System.currentTimeMillis());
204 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
205 String[] coList = new String[numberOfObjects];
208 // First create a CollectionObject
210 CollectionobjectsCommon co = new CollectionobjectsCommon();
211 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
213 // Next, create a part object
214 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
215 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
216 commonPart.setLabel(collectionObjectClient.getCommonPartName());
218 int createdObjects = 0;
220 Date startTime = new Date();
221 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
222 coList[i] = createCollectionObject(collectionObjectClient, multipart);
223 if (logger.isDebugEnabled() == true) {
225 // Print out a status every 10 operations
227 logger.debug("Created CollectionObject #: " + i);
231 Date stopTime = new Date();
232 if (logger.isDebugEnabled()) {
233 System.out.println("Created " + numberOfObjects + " CollectionObjects"
234 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
236 } catch (AssertionError e) {
237 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects
238 + " before failing.");
239 Assert.assertTrue(false);
249 * Delete collection object.
251 * @param collectionObjectClient the collection object client
252 * @param resourceId the resource id
254 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
256 ClientResponse<String> res = collectionObjectClient.read(resourceId);
257 res.releaseConnection();
261 * Delete collection objects.
263 * @param arrayOfObjects the array of objects
265 public void readCollectionObjects(String[] arrayOfObjects) {
266 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
268 Date startTime = new Date();
269 for (int i = 0; i < arrayOfObjects.length; i++) {
270 readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
272 Date stopTime = new Date();
274 if (logger.isDebugEnabled()) {
275 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects"
276 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
281 * Delete collection objects.
282 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
284 public void readCollectionObjects() {
285 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
286 ClientResponse<AbstractCommonList> response;
288 List<AbstractCommonList.ListItem> coListItems = null;
290 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
293 AbstractCommonList commonListElement =
294 (AbstractCommonList) response.getEntity(AbstractCommonList.class);
295 coListItems = commonListElement.getListItem();
297 response.releaseConnection();
300 Date startTime = new Date();
301 for (AbstractCommonList.ListItem i : coListItems) {
302 readCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
304 Date stopTime = new Date();
306 if (logger.isDebugEnabled()) {
307 System.out.println("Read " + coListItems.size() + " CollectionObjects"
308 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
310 } while (coListItems.size() > 0);
317 * Delete collection object.
319 * @param collectionObjectClient the collection object client
320 * @param resourceId the resource id
322 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
324 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
325 res.releaseConnection();
329 * Delete collection objects.
331 * @param arrayOfObjects the array of objects
333 private void deleteCollectionObjects(String[] arrayOfObjects) {
334 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
336 Date startTime = new Date();
337 for (int i = 0; i < arrayOfObjects.length; i++) {
338 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
340 Date stopTime = new Date();
342 if (logger.isDebugEnabled()) {
343 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects"
344 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
349 * Delete collection objects.
350 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
352 private void deleteCollectionObjects() {
353 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
354 ClientResponse<AbstractCommonList> response;
356 List<AbstractCommonList.ListItem> coListItems = null;
358 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
361 AbstractCommonList commonListElement =
362 (AbstractCommonList) response.getEntity(AbstractCommonList.class);
363 coListItems = commonListElement.getListItem();
365 response.releaseConnection();
368 Date startTime = new Date();
369 for (AbstractCommonList.ListItem i : coListItems) {
370 deleteCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
372 Date stopTime = new Date();
374 if (logger.isDebugEnabled()) {
375 System.out.println("Deleted " + coListItems.size() + " CollectionObjects"
376 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
378 } while (coListItems.size() > 0);