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 Response searchResultsResponse;
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 searchResultsResponse = collectionObjectClient.keywordSearchIncludeDeleted(keywords,
133 NOT_INCLUDING_DELETED_RESOURCES);
134 Date stopTime = new Date();
136 //extract the result list and release the ClientResponse
137 AbstractCommonList coListItem = null;
139 coListItem = searchResultsResponse.readEntity(AbstractCommonList.class);
141 searchResultsResponse.close();
144 long time = stopTime.getTime() - startTime.getTime();
145 times = times + " " + ((float) time / 1000);
146 totalTime = totalTime + time;
147 totalSearchResults = totalSearchResults
148 + coListItem.getListItem().size();
151 if (logger.isDebugEnabled()) {
152 System.out.println("------------------------------------------------------------------------------");
153 System.out.println("Searched Objects: " + numberOfObjects);
154 System.out.println("Number of keywords: " + numOfKeywords);
155 System.out.println("List of keywords: " + keywords);
156 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
157 System.out.println("Result times: " + times);
158 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
159 System.out.println("------------------------------------------------------------------------------");
166 * Creates the collection object.
168 * @param collectionObjectClient the collection object client
169 * @param identifier the identifier
172 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
173 PoxPayloadOut multipart) {
174 String result = null;
175 // Make the create call and check the response
176 Response response = collectionObjectClient.create(multipart);
178 int responseStatus = response.getStatus();
179 if (logger.isDebugEnabled() == true) {
180 if (responseStatus != Response.Status.CREATED.getStatusCode()) {
181 logger.debug("Status of call to create CollectionObject was: "
186 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
187 result = extractId(response);
196 * Creates the collection objects.
198 * @param numberOfObjects the number of objects
199 * @return the string[]
201 public String[] createCollectionObjects(int numberOfObjects) {
202 Random randomGenerator = new Random(System.currentTimeMillis());
203 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
204 String[] coList = new String[numberOfObjects];
207 // First create a CollectionObject
209 CollectionobjectsCommon co = new CollectionobjectsCommon();
210 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
212 // Next, create a part object
213 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
214 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
215 commonPart.setLabel(collectionObjectClient.getCommonPartName());
217 int createdObjects = 0;
219 Date startTime = new Date();
220 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
221 coList[i] = createCollectionObject(collectionObjectClient, multipart);
222 if (logger.isDebugEnabled() == true) {
224 // Print out a status every 10 operations
226 logger.debug("Created CollectionObject #: " + i);
230 Date stopTime = new Date();
231 if (logger.isDebugEnabled()) {
232 System.out.println("Created " + numberOfObjects + " CollectionObjects"
233 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
235 } catch (AssertionError e) {
236 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects
237 + " before failing.");
238 Assert.assertTrue(false);
248 * Delete collection object.
250 * @param collectionObjectClient the collection object client
251 * @param resourceId the resource id
253 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
255 Response res = collectionObjectClient.read(resourceId);
260 * Delete collection objects.
262 * @param arrayOfObjects the array of objects
264 public void readCollectionObjects(String[] arrayOfObjects) {
265 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
267 Date startTime = new Date();
268 for (int i = 0; i < arrayOfObjects.length; i++) {
269 readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
271 Date stopTime = new Date();
273 if (logger.isDebugEnabled()) {
274 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects"
275 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
280 * Delete collection objects.
281 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
283 public void readCollectionObjects() {
284 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
286 List<AbstractCommonList.ListItem> coListItems = null;
288 Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
291 AbstractCommonList commonListElement =
292 (AbstractCommonList) res.readEntity(AbstractCommonList.class);
293 coListItems = commonListElement.getListItem();
298 Date startTime = new Date();
299 for (AbstractCommonList.ListItem i : coListItems) {
300 readCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
302 Date stopTime = new Date();
304 if (logger.isDebugEnabled()) {
305 System.out.println("Read " + coListItems.size() + " CollectionObjects"
306 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
308 } while (coListItems.size() > 0);
315 * Delete collection object.
317 * @param collectionObjectClient the collection object client
318 * @param resourceId the resource id
320 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
322 collectionObjectClient.delete(resourceId).close();
326 * Delete collection objects.
328 * @param arrayOfObjects the array of objects
330 private void deleteCollectionObjects(String[] arrayOfObjects) {
331 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
333 Date startTime = new Date();
334 for (int i = 0; i < arrayOfObjects.length; i++) {
335 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
337 Date stopTime = new Date();
339 if (logger.isDebugEnabled()) {
340 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects"
341 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
346 * Delete collection objects.
347 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
349 private void deleteCollectionObjects() {
350 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
352 List<AbstractCommonList.ListItem> coListItems = null;
354 Response res = collectionObjectClient.readList(new Long(MAX_RECORDS),
357 AbstractCommonList commonListElement =
358 (AbstractCommonList) res.readEntity(AbstractCommonList.class);
359 coListItems = commonListElement.getListItem();
364 Date startTime = new Date();
365 for (AbstractCommonList.ListItem i : coListItems) {
366 deleteCollectionObject(collectionObjectClient, AbstractCommonListUtils.ListItemGetElementValue(i, "csid"));
368 Date stopTime = new Date();
370 if (logger.isDebugEnabled()) {
371 System.out.println("Deleted " + coListItems.size() + " CollectionObjects"
372 + " in " + (stopTime.getTime() - startTime.getTime()) / 1000.0 + " seconds.");
374 } while (coListItems.size() > 0);