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;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.util.HttpResponseCodes;
38 import org.collectionspace.services.client.AbstractCommonListUtils;
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.PayloadOutputPart;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
48 * @version $Revision:$
50 public class PerformanceTest extends CollectionSpacePerformanceTest {
52 /** The Constant MAX_KEYWORDS. */
53 private static final int MAX_KEYWORDS = 10;
54 /** The Constant MAX_SEARCHES. */
55 private static final int MAX_SEARCHES = 10;
57 final Logger logger = LoggerFactory.getLogger(PerformanceTest.class);
59 // Get clients for the CollectionSpace services
61 /** The MA x_ records. */
62 private static int MAX_RECORDS = 100;
68 public void performanceTest() {
69 roundTripOverhead(10);
70 deleteCollectionObjects();
71 String[] coList = this.createCollectionObjects(MAX_RECORDS);
72 this.searchCollectionObjects(MAX_RECORDS);
73 this.readCollectionObjects(coList);
74 //this.deleteCollectionObjects(coList);
75 roundTripOverhead(10);
79 * Round trip overhead.
81 * @param numOfCalls the num of calls
84 private long roundTripOverhead(int numOfCalls) {
86 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
89 ClientResponse<Response> response;
90 for (int i = 0; i < numOfCalls; i++) {
91 Date startTime = new Date();
92 response = collectionObjectClient.roundtrip(0);
94 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
98 Date stopTime = new Date();
99 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
100 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
103 System.out.println("------------------------------------------------------------------------------");
104 System.out.println("Client to server roundtrip overhead: " + (float) (totalTime / numOfCalls) / 1000);
105 System.out.println("------------------------------------------------------------------------------");
106 System.out.println("");
112 * Search collection objects.
114 * @param numberOfObjects the number of objects
116 private void searchCollectionObjects(int numberOfObjects) {
117 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
118 Random randomGenerator = new Random(System.currentTimeMillis());
119 ClientResponse<AbstractCommonList> searchResults;
122 long totalSearchResults = 0;
123 String keywords = "";
125 final boolean NOT_INCLUDING_DELETED_RESOURCES = false;
126 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
127 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
128 keywords = keywords + " " + OBJECT_TITLE + randomGenerator.nextInt(numberOfObjects);
129 for (int i = 0; i < MAX_SEARCHES; i++) {
130 //sandwich the call with timestamps
131 Date startTime = new Date();
132 searchResults = collectionObjectClient.keywordSearchIncludeDeleted(keywords, NOT_INCLUDING_DELETED_RESOURCES);
133 Date stopTime = new Date();
135 //extract the result list and release the ClientResponse
136 AbstractCommonList coListItem = null;
138 coListItem = searchResults.getEntity();
140 searchResults.close();
143 long time = stopTime.getTime() - startTime.getTime();
144 times = times + " " + ((float) time / 1000);
145 totalTime = totalTime + time;
146 totalSearchResults = totalSearchResults
147 + coListItem.getListItem().size();
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("------------------------------------------------------------------------------");
164 * Creates the collection object.
166 * @param collectionObjectClient the collection object client
167 * @param identifier the identifier
170 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
171 PoxPayloadOut multipart) {
172 String result = null;
173 // Make the create call and check the response
174 Response response = collectionObjectClient.create(multipart);
176 int responseStatus = response.getStatus();
177 if (logger.isDebugEnabled() == true) {
178 if (responseStatus != Response.Status.CREATED.getStatusCode()) {
179 logger.debug("Status of call to create CollectionObject was: "
184 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
185 result = extractId(response);
194 * Creates the collection objects.
196 * @param numberOfObjects the number of objects
197 * @return the string[]
199 public String[] createCollectionObjects(int numberOfObjects) {
200 Random randomGenerator = new Random(System.currentTimeMillis());
201 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
202 String[] coList = new String[numberOfObjects];
205 // First create a CollectionObject
207 CollectionobjectsCommon co = new CollectionobjectsCommon();
208 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
210 // Next, create a part object
211 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
212 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
213 commonPart.setLabel(collectionObjectClient.getCommonPartName());
215 int createdObjects = 0;
217 Date startTime = new Date();
218 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
219 coList[i] = createCollectionObject(collectionObjectClient, multipart);
220 if (logger.isDebugEnabled() == true) {
222 // Print out a status every 10 operations
224 logger.debug("Created CollectionObject #: " + i);
228 Date stopTime = new Date();
229 if (logger.isDebugEnabled()) {
230 System.out.println("Created " + numberOfObjects + " CollectionObjects"
231 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
233 } catch (AssertionError e) {
234 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects
235 + " before failing.");
236 Assert.assertTrue(false);
246 * Delete collection object.
248 * @param collectionObjectClient the collection object client
249 * @param resourceId the resource id
251 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
253 Response res = collectionObjectClient.read(resourceId);
258 * Delete collection objects.
260 * @param arrayOfObjects the array of objects
262 public void readCollectionObjects(String[] arrayOfObjects) {
263 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
265 Date startTime = new Date();
266 for (int i = 0; i < arrayOfObjects.length; i++) {
267 readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
269 Date stopTime = new Date();
271 if (logger.isDebugEnabled()) {
272 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects"
273 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
278 * Delete collection objects.
279 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
281 public void readCollectionObjects() {
282 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
284 List<AbstractCommonList.ListItem> coListItems = null;
286 Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
289 AbstractCommonList commonListElement =
290 (AbstractCommonList) res.readEntity(AbstractCommonList.class);
291 coListItems = commonListElement.getListItem();
296 Date startTime = new Date();
297 for (AbstractCommonList.ListItem i : coListItems) {
298 readCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
300 Date stopTime = new Date();
302 if (logger.isDebugEnabled()) {
303 System.out.println("Read " + coListItems.size() + " CollectionObjects"
304 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
306 } while (coListItems.size() > 0);
313 * Delete collection object.
315 * @param collectionObjectClient the collection object client
316 * @param resourceId the resource id
318 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
320 collectionObjectClient.delete(resourceId).close();
324 * Delete collection objects.
326 * @param arrayOfObjects the array of objects
328 private void deleteCollectionObjects(String[] arrayOfObjects) {
329 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
331 Date startTime = new Date();
332 for (int i = 0; i < arrayOfObjects.length; i++) {
333 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
335 Date stopTime = new Date();
337 if (logger.isDebugEnabled()) {
338 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects"
339 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
344 * Delete collection objects.
345 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
347 private void deleteCollectionObjects() {
348 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
350 List<AbstractCommonList.ListItem> coListItems = null;
352 Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
355 AbstractCommonList commonListElement =
356 (AbstractCommonList) res.readEntity(AbstractCommonList.class);
357 coListItems = commonListElement.getListItem();
362 Date startTime = new Date();
363 for (AbstractCommonList.ListItem i : coListItems) {
364 deleteCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
366 Date stopTime = new Date();
368 if (logger.isDebugEnabled()) {
369 System.out.println("Deleted " + coListItems.size() + " CollectionObjects"
370 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
372 } while (coListItems.size() > 0);