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;
41 import org.jboss.resteasy.util.HttpResponseCodes;
43 import org.collectionspace.services.jaxb.AbstractCommonList;
44 import org.collectionspace.services.client.CollectionObjectClient;
45 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
46 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
47 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
52 * @version $Revision:$
54 public class PerformanceTest extends CollectionSpacePerformanceTest {
56 /** The Constant MAX_KEYWORDS. */
57 private static final int MAX_KEYWORDS = 10;
59 /** The Constant MAX_SEARCHES. */
60 private static final int MAX_SEARCHES = 10;
63 final Logger logger = LoggerFactory
64 .getLogger(PerformanceTest.class);
66 // Get clients for the CollectionSpace services
68 /** The MA x_ records. */
69 private static int MAX_RECORDS = 1000;
75 public void performanceTest() {
76 roundTripOverhead(10);
77 deleteCollectionObjects();
78 String[] coList = this.createCollectionObjects(MAX_RECORDS);
79 this.searchCollectionObjects(MAX_RECORDS);
80 this.readCollectionObjects(coList);
81 this.deleteCollectionObjects(coList);
82 roundTripOverhead(10);
86 * Round trip overhead.
88 * @param numOfCalls the num of calls
91 private long roundTripOverhead(int numOfCalls) {
93 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
96 ClientResponse<Response> response;
97 for (int i = 0; i < numOfCalls; i++) {
98 Date startTime = new Date();
99 response = collectionObjectClient.roundtrip();
101 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
103 response.releaseConnection();
105 Date stopTime = new Date();
106 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
107 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
110 System.out.println("------------------------------------------------------------------------------");
111 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
112 System.out.println("------------------------------------------------------------------------------");
113 System.out.println("");
119 * Search collection objects.
121 * @param numberOfObjects the number of objects
123 private void searchCollectionObjects(int numberOfObjects) {
124 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
125 Random randomGenerator = new Random(System.currentTimeMillis());
126 ClientResponse<CollectionobjectsCommonList> searchResults;
129 long totalSearchResults = 0;
130 String keywords = "";
132 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
133 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
134 keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
135 for (int i = 0; i < MAX_SEARCHES; i++) {
136 //sandwich the call with timestamps
137 Date startTime = new Date();
138 searchResults = collectionObjectClient.keywordSearch(keywords);
139 Date stopTime = new Date();
141 //extract the result list and release the ClientResponse
142 CollectionobjectsCommonList coListItem = null;
144 coListItem = searchResults.getEntity();
146 searchResults.releaseConnection();
149 long time = stopTime.getTime() - startTime.getTime();
150 times = times + " " + ((float)time / 1000);
151 totalTime = totalTime + time;
152 totalSearchResults = totalSearchResults +
153 coListItem.getCollectionObjectListItem().size();
155 if (logger.isDebugEnabled()) {
156 System.out.println("------------------------------------------------------------------------------");
157 System.out.println("Searched Objects: " + numberOfObjects);
158 System.out.println("Number of keywords: " + numOfKeywords);
159 System.out.println("List of keywords: " + keywords);
160 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
161 System.out.println("Result times: " + times);
162 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
163 System.out.println("------------------------------------------------------------------------------");
170 * Creates the collection object.
172 * @param collectionObjectClient the collection object client
173 * @param identifier the identifier
176 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
177 MultipartOutput multipart) {
178 String result = null;
179 // Make the create call and check the response
180 ClientResponse<Response> response = collectionObjectClient.create(multipart);
182 int responseStatus = response.getStatus();
183 if (logger.isDebugEnabled() == true) {
184 if (responseStatus != Response.Status.CREATED.getStatusCode())
185 logger.debug("Status of call to create CollectionObject was: " +
189 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
190 result = extractId(response);
192 response.releaseConnection();
199 * Creates the collection objects.
201 * @param numberOfObjects the number of objects
202 * @return the string[]
204 public String[] createCollectionObjects(int numberOfObjects) {
205 Random randomGenerator = new Random(System.currentTimeMillis());
206 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
207 String[] coList = new String[numberOfObjects];
210 // First create a CollectionObject
212 CollectionobjectsCommon co = new CollectionobjectsCommon();
213 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
215 // Next, create a part object
216 MultipartOutput multipart = new MultipartOutput();
217 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
218 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
220 int createdObjects = 0;
222 Date startTime = new Date();
223 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
224 coList[i] = createCollectionObject(collectionObjectClient, multipart);
225 if (logger.isDebugEnabled() == true) {
227 // Print out a status every 10 operations
229 logger.debug("Created CollectionObject #: " + i);
232 Date stopTime = new Date();
233 if (logger.isDebugEnabled()) {
234 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
235 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
237 } catch (AssertionError e) {
238 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
240 Assert.assertTrue(false);
250 * Delete collection object.
252 * @param collectionObjectClient the collection object client
253 * @param resourceId the resource id
255 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
257 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
258 res.releaseConnection();
262 * Delete collection objects.
264 * @param arrayOfObjects the array of objects
266 public void readCollectionObjects(String[] arrayOfObjects) {
267 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
269 Date startTime = new Date();
270 for (int i = 0; i < arrayOfObjects.length; i++) {
271 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
273 Date stopTime = new Date();
275 if (logger.isDebugEnabled()) {
276 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects" +
277 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
282 * Delete collection objects.
283 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
285 public void readCollectionObjects() {
286 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
287 ClientResponse<AbstractCommonList> response;
289 List<CollectionObjectListItem> coListItems = null;
291 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
294 CollectionobjectsCommonList commonListElement =
295 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
296 coListItems = commonListElement.getCollectionObjectListItem();
298 response.releaseConnection();
301 Date startTime = new Date();
302 for (CollectionObjectListItem i:coListItems) {
303 readCollectionObject(collectionObjectClient, i.getCsid());
305 Date stopTime = new Date();
307 if (logger.isDebugEnabled()) {
308 System.out.println("Read " + coListItems.size() + " CollectionObjects" +
309 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
311 } while (coListItems.size() > 0);
318 * Delete collection object.
320 * @param collectionObjectClient the collection object client
321 * @param resourceId the resource id
323 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
325 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
326 res.releaseConnection();
330 * Delete collection objects.
332 * @param arrayOfObjects the array of objects
334 public void deleteCollectionObjects(String[] arrayOfObjects) {
335 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
337 Date startTime = new Date();
338 for (int i = 0; i < arrayOfObjects.length; i++) {
339 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
341 Date stopTime = new Date();
343 if (logger.isDebugEnabled()) {
344 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
345 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
350 * Delete collection objects.
351 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
353 public void deleteCollectionObjects() {
354 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
355 ClientResponse<AbstractCommonList> response;
357 List<CollectionObjectListItem> coListItems = null;
359 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
362 CollectionobjectsCommonList commonListElement =
363 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
364 coListItems = commonListElement.getCollectionObjectListItem();
366 response.releaseConnection();
369 Date startTime = new Date();
370 for (CollectionObjectListItem i:coListItems) {
371 deleteCollectionObject(collectionObjectClient, i.getCsid());
373 Date stopTime = new Date();
375 if (logger.isDebugEnabled()) {
376 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
377 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
379 } while (coListItems.size() > 0);