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.collectionobject.CollectionobjectsCommon;
47 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
48 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
53 * @version $Revision:$
55 public class PerformanceTest extends CollectionSpacePerformanceTest {
57 /** The Constant MAX_KEYWORDS. */
58 private static final int MAX_KEYWORDS = 10;
60 /** The Constant MAX_SEARCHES. */
61 private static final int MAX_SEARCHES = 10;
64 final Logger logger = LoggerFactory
65 .getLogger(PerformanceTest.class);
67 // Get clients for the CollectionSpace services
69 /** The MA x_ records. */
70 private static int MAX_RECORDS = 100;
76 public void performanceTest() {
77 roundTripOverhead(10);
78 deleteCollectionObjects();
79 String[] coList = this.createCollectionObjects(MAX_RECORDS);
80 this.searchCollectionObjects(MAX_RECORDS);
81 this.readCollectionObjects(coList);
82 //this.deleteCollectionObjects(coList);
83 roundTripOverhead(10);
87 * Round trip overhead.
89 * @param numOfCalls the num of calls
92 private long roundTripOverhead(int numOfCalls) {
94 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
97 ClientResponse<Response> response;
98 for (int i = 0; i < numOfCalls; i++) {
99 Date startTime = new Date();
100 response = collectionObjectClient.roundtrip(0);
102 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
104 response.releaseConnection();
106 Date stopTime = new Date();
107 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
108 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
111 System.out.println("------------------------------------------------------------------------------");
112 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
113 System.out.println("------------------------------------------------------------------------------");
114 System.out.println("");
120 * Search collection objects.
122 * @param numberOfObjects the number of objects
124 private void searchCollectionObjects(int numberOfObjects) {
125 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
126 Random randomGenerator = new Random(System.currentTimeMillis());
127 ClientResponse<CollectionobjectsCommonList> searchResults;
130 long totalSearchResults = 0;
131 String keywords = "";
133 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
134 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
135 keywords = keywords + " " + OBJECT_TITLE + randomGenerator.nextInt(numberOfObjects);
136 for (int i = 0; i < MAX_SEARCHES; i++) {
137 //sandwich the call with timestamps
138 Date startTime = new Date();
139 searchResults = collectionObjectClient.keywordSearch(keywords);
140 Date stopTime = new Date();
142 //extract the result list and release the ClientResponse
143 CollectionobjectsCommonList coListItem = null;
145 coListItem = searchResults.getEntity();
147 searchResults.releaseConnection();
150 long time = stopTime.getTime() - startTime.getTime();
151 times = times + " " + ((float)time / 1000);
152 totalTime = totalTime + time;
153 totalSearchResults = totalSearchResults +
154 coListItem.getCollectionObjectListItem().size();
156 if (logger.isDebugEnabled()) {
157 System.out.println("------------------------------------------------------------------------------");
158 System.out.println("Searched Objects: " + numberOfObjects);
159 System.out.println("Number of keywords: " + numOfKeywords);
160 System.out.println("List of keywords: " + keywords);
161 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
162 System.out.println("Result times: " + times);
163 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
164 System.out.println("------------------------------------------------------------------------------");
171 * Creates the collection object.
173 * @param collectionObjectClient the collection object client
174 * @param identifier the identifier
177 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
178 MultipartOutput multipart) {
179 String result = null;
180 // Make the create call and check the response
181 ClientResponse<Response> response = collectionObjectClient.create(multipart);
183 int responseStatus = response.getStatus();
184 if (logger.isDebugEnabled() == true) {
185 if (responseStatus != Response.Status.CREATED.getStatusCode())
186 logger.debug("Status of call to create CollectionObject was: " +
190 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
191 result = extractId(response);
193 response.releaseConnection();
200 * Creates the collection objects.
202 * @param numberOfObjects the number of objects
203 * @return the string[]
205 public String[] createCollectionObjects(int numberOfObjects) {
206 Random randomGenerator = new Random(System.currentTimeMillis());
207 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
208 String[] coList = new String[numberOfObjects];
211 // First create a CollectionObject
213 CollectionobjectsCommon co = new CollectionobjectsCommon();
214 fillCollectionObject(co, Long.toString(System.currentTimeMillis()));
216 // Next, create a part object
217 MultipartOutput multipart = new MultipartOutput();
218 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
219 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
221 int createdObjects = 0;
223 Date startTime = new Date();
224 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
225 coList[i] = createCollectionObject(collectionObjectClient, multipart);
226 if (logger.isDebugEnabled() == true) {
228 // Print out a status every 10 operations
230 logger.debug("Created CollectionObject #: " + i);
233 Date stopTime = new Date();
234 if (logger.isDebugEnabled()) {
235 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
236 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
238 } catch (AssertionError e) {
239 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
241 Assert.assertTrue(false);
251 * Delete collection object.
253 * @param collectionObjectClient the collection object client
254 * @param resourceId the resource id
256 private void readCollectionObject(CollectionObjectClient collectionObjectClient,
258 ClientResponse<MultipartInput> res = collectionObjectClient.read(resourceId);
259 res.releaseConnection();
263 * Delete collection objects.
265 * @param arrayOfObjects the array of objects
267 public void readCollectionObjects(String[] arrayOfObjects) {
268 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
270 Date startTime = new Date();
271 for (int i = 0; i < arrayOfObjects.length; i++) {
272 readCollectionObject(collectionObjectClient, arrayOfObjects[i]);
274 Date stopTime = new Date();
276 if (logger.isDebugEnabled()) {
277 System.out.println("Read " + arrayOfObjects.length + " CollectionObjects" +
278 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
283 * Delete collection objects.
284 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
286 public void readCollectionObjects() {
287 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
288 ClientResponse<AbstractCommonList> response;
290 List<CollectionObjectListItem> coListItems = null;
292 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
295 CollectionobjectsCommonList commonListElement =
296 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
297 coListItems = commonListElement.getCollectionObjectListItem();
299 response.releaseConnection();
302 Date startTime = new Date();
303 for (CollectionObjectListItem i:coListItems) {
304 readCollectionObject(collectionObjectClient, i.getCsid());
306 Date stopTime = new Date();
308 if (logger.isDebugEnabled()) {
309 System.out.println("Read " + coListItems.size() + " CollectionObjects" +
310 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
312 } while (coListItems.size() > 0);
319 * Delete collection object.
321 * @param collectionObjectClient the collection object client
322 * @param resourceId the resource id
324 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
326 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
327 res.releaseConnection();
331 * Delete collection objects.
333 * @param arrayOfObjects the array of objects
335 private void deleteCollectionObjects(String[] arrayOfObjects) {
336 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
338 Date startTime = new Date();
339 for (int i = 0; i < arrayOfObjects.length; i++) {
340 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
342 Date stopTime = new Date();
344 if (logger.isDebugEnabled()) {
345 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
346 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
351 * Delete collection objects.
352 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
354 private void deleteCollectionObjects() {
355 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
356 ClientResponse<AbstractCommonList> response;
358 List<CollectionObjectListItem> coListItems = null;
360 response = collectionObjectClient.readList(new Long(MAX_RECORDS),
363 CollectionobjectsCommonList commonListElement =
364 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
365 coListItems = commonListElement.getCollectionObjectListItem();
367 response.releaseConnection();
370 Date startTime = new Date();
371 for (CollectionObjectListItem i:coListItems) {
372 deleteCollectionObject(collectionObjectClient, i.getCsid());
374 Date stopTime = new Date();
376 if (logger.isDebugEnabled()) {
377 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
378 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
380 } while (coListItems.size() > 0);