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.MultipartInput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.jboss.resteasy.util.HttpResponseCodes;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.client.CollectionObjectClient;
46 import org.collectionspace.services.client.PayloadOutputPart;
47 import org.collectionspace.services.client.PoxPayloadOut;
48 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
49 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
50 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
55 * @version $Revision:$
57 public class PerformanceTest extends CollectionSpacePerformanceTest {
59 /** The Constant MAX_KEYWORDS. */
60 private static final int MAX_KEYWORDS = 10;
62 /** The Constant MAX_SEARCHES. */
63 private static final int MAX_SEARCHES = 10;
66 final Logger logger = LoggerFactory
67 .getLogger(PerformanceTest.class);
69 // Get clients for the CollectionSpace services
71 /** The MA x_ records. */
72 private static int MAX_RECORDS = 100;
78 public void performanceTest() {
79 roundTripOverhead(10);
80 deleteCollectionObjects();
81 String[] coList = this.createCollectionObjects(MAX_RECORDS);
82 this.searchCollectionObjects(MAX_RECORDS);
83 this.readCollectionObjects(coList);
84 //this.deleteCollectionObjects(coList);
85 roundTripOverhead(10);
89 * Round trip overhead.
91 * @param numOfCalls the num of calls
94 private long roundTripOverhead(int numOfCalls) {
96 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
99 ClientResponse<Response> response;
100 for (int i = 0; i < numOfCalls; i++) {
101 Date startTime = new Date();
102 response = collectionObjectClient.roundtrip(0);
104 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
106 response.releaseConnection();
108 Date stopTime = new Date();
109 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
110 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
113 System.out.println("------------------------------------------------------------------------------");
114 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
115 System.out.println("------------------------------------------------------------------------------");
116 System.out.println("");
122 * Search collection objects.
124 * @param numberOfObjects the number of objects
126 private void searchCollectionObjects(int numberOfObjects) {
127 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
128 Random randomGenerator = new Random(System.currentTimeMillis());
129 ClientResponse<CollectionobjectsCommonList> searchResults;
132 long totalSearchResults = 0;
133 String keywords = "";
135 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
136 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
137 keywords = keywords + " " + OBJECT_TITLE + randomGenerator.nextInt(numberOfObjects);
138 for (int i = 0; i < MAX_SEARCHES; i++) {
139 //sandwich the call with timestamps
140 Date startTime = new Date();
141 searchResults = collectionObjectClient.keywordSearch(keywords);
142 Date stopTime = new Date();
144 //extract the result list and release the ClientResponse
145 CollectionobjectsCommonList coListItem = null;
147 coListItem = searchResults.getEntity();
149 searchResults.releaseConnection();
152 long time = stopTime.getTime() - startTime.getTime();
153 times = times + " " + ((float)time / 1000);
154 totalTime = totalTime + time;
155 totalSearchResults = totalSearchResults +
156 coListItem.getCollectionObjectListItem().size();
158 if (logger.isDebugEnabled()) {
159 System.out.println("------------------------------------------------------------------------------");
160 System.out.println("Searched Objects: " + numberOfObjects);
161 System.out.println("Number of keywords: " + numOfKeywords);
162 System.out.println("List of keywords: " + keywords);
163 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
164 System.out.println("Result times: " + times);
165 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
166 System.out.println("------------------------------------------------------------------------------");
173 * Creates the collection object.
175 * @param collectionObjectClient the collection object client
176 * @param identifier the identifier
179 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
180 PoxPayloadOut multipart) {
181 String result = null;
182 // Make the create call and check the response
183 ClientResponse<Response> response = collectionObjectClient.create(multipart);
185 int responseStatus = response.getStatus();
186 if (logger.isDebugEnabled() == true) {
187 if (responseStatus != Response.Status.CREATED.getStatusCode())
188 logger.debug("Status of call to create CollectionObject was: " +
192 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
193 result = extractId(response);
195 response.releaseConnection();
202 * Creates the collection objects.
204 * @param numberOfObjects the number of objects
205 * @return the string[]
207 public String[] createCollectionObjects(int numberOfObjects) {
208 Random randomGenerator = new Random(System.currentTimeMillis());
209 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
210 String[] coList = new String[numberOfObjects];
213 // First create a CollectionObject
215 CollectionobjectsCommon co = new CollectionobjectsCommon();
216 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
218 // Next, create a part object
219 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
220 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
221 commonPart.setLabel(collectionObjectClient.getCommonPartName());
223 int createdObjects = 0;
225 Date startTime = new Date();
226 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
227 coList[i] = createCollectionObject(collectionObjectClient, multipart);
228 if (logger.isDebugEnabled() == true) {
230 // Print out a status every 10 operations
232 logger.debug("Created CollectionObject #: " + i);
235 Date stopTime = new Date();
236 if (logger.isDebugEnabled()) {
237 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
238 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
240 } catch (AssertionError e) {
241 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
243 Assert.assertTrue(false);
253 * Delete collection object.
255 * @param collectionObjectClient the collection object client
256 * @param resourceId the resource id
258 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
260 ClientResponse<String> res = collectionObjectClient.read(resourceId);
261 res.releaseConnection();
265 * Delete collection objects.
267 * @param arrayOfObjects the array of objects
269 public void readCollectionObjects(String[] arrayOfObjects) {
270 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
272 Date startTime = new Date();
273 for (int i = 0; i < arrayOfObjects.length; i++) {
274 readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
276 Date stopTime = new Date();
278 if (logger.isDebugEnabled()) {
279 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects" +
280 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
285 * Delete collection objects.
286 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
288 public void readCollectionObjects() {
289 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
290 ClientResponse<AbstractCommonList> response;
292 List<CollectionObjectListItem> coListItems = null;
294 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
297 CollectionobjectsCommonList commonListElement =
298 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
299 coListItems = commonListElement.getCollectionObjectListItem();
301 response.releaseConnection();
304 Date startTime = new Date();
305 for (CollectionObjectListItem i:coListItems) {
306 readCollectionObject(collectionObjectClient, i.getCsid());
308 Date stopTime = new Date();
310 if (logger.isDebugEnabled()) {
311 System.out.println("Read " + coListItems.size() + " CollectionObjects" +
312 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
314 } while (coListItems.size() > 0);
321 * Delete collection object.
323 * @param collectionObjectClient the collection object client
324 * @param resourceId the resource id
326 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
328 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
329 res.releaseConnection();
333 * Delete collection objects.
335 * @param arrayOfObjects the array of objects
337 private void deleteCollectionObjects(String[] arrayOfObjects) {
338 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
340 Date startTime = new Date();
341 for (int i = 0; i < arrayOfObjects.length; i++) {
342 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
344 Date stopTime = new Date();
346 if (logger.isDebugEnabled()) {
347 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
348 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
353 * Delete collection objects.
354 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
356 private void deleteCollectionObjects() {
357 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
358 ClientResponse<AbstractCommonList> response;
360 List<CollectionObjectListItem> coListItems = null;
362 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
365 CollectionobjectsCommonList commonListElement =
366 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
367 coListItems = commonListElement.getCollectionObjectListItem();
369 response.releaseConnection();
372 Date startTime = new Date();
373 for (CollectionObjectListItem i:coListItems) {
374 deleteCollectionObject(collectionObjectClient, i.getCsid());
376 Date stopTime = new Date();
378 if (logger.isDebugEnabled()) {
379 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
380 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
382 } while (coListItems.size() > 0);