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 = 1;
75 public void performanceTest() {
76 // roundTripOverhead(MAX_RECORDS);
77 // deleteCollectionObjects();
78 String[] coList = this.createCollectionObjects(MAX_RECORDS);
79 // this.searchCollectionObjects(MAX_RECORDS);
80 // this.deleteCollectionObjects(coList);
81 // roundTripOverhead(10);
85 * Round trip overhead.
87 * @param numOfCalls the num of calls
90 private long roundTripOverhead(int numOfCalls) {
92 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
95 ClientResponse<Response> response;
96 for (int i = 0; i < numOfCalls; i++) {
97 Date startTime = new Date();
98 response = collectionObjectClient.roundtrip();
100 Assert.assertEquals(response.getStatus(), HttpResponseCodes.SC_OK);
102 response.releaseConnection();
104 Date stopTime = new Date();
105 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
106 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
109 System.out.println("------------------------------------------------------------------------------");
110 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
111 System.out.println("------------------------------------------------------------------------------");
112 System.out.println("");
118 * Search collection objects.
120 * @param numberOfObjects the number of objects
122 private void searchCollectionObjects(int numberOfObjects) {
123 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
124 Random randomGenerator = new Random(System.currentTimeMillis());
125 ClientResponse<CollectionobjectsCommonList> searchResults;
128 long totalSearchResults = 0;
129 String keywords = "";
131 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
132 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
133 keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
134 for (int i = 0; i < MAX_SEARCHES; i++) {
135 //sandwich the call with timestamps
136 Date startTime = new Date();
137 searchResults = collectionObjectClient.keywordSearch(keywords);
138 Date stopTime = new Date();
140 //extract the result list and release the ClientResponse
141 CollectionobjectsCommonList coListItem = null;
143 coListItem = searchResults.getEntity();
145 searchResults.releaseConnection();
148 long time = stopTime.getTime() - startTime.getTime();
149 times = times + " " + ((float)time / 1000);
150 totalTime = totalTime + time;
151 totalSearchResults = totalSearchResults +
152 coListItem.getCollectionObjectListItem().size();
154 if (logger.isDebugEnabled()) {
155 System.out.println("------------------------------------------------------------------------------");
156 System.out.println("Searched Objects: " + numberOfObjects);
157 System.out.println("Number of keywords: " + numOfKeywords);
158 System.out.println("List of keywords: " + keywords);
159 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
160 System.out.println("Result times: " + times);
161 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
162 System.out.println("------------------------------------------------------------------------------");
169 * Creates the collection object.
171 * @param collectionObjectClient the collection object client
172 * @param identifier the identifier
175 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
177 String result = null;
179 // First create a CollectionObject
181 CollectionobjectsCommon co = new CollectionobjectsCommon();
182 fillCollectionObject(co, Integer.toString(identifier));
184 // Next, create a part object
185 MultipartOutput multipart = new MultipartOutput();
186 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
187 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
188 // Make the create call and check the response
189 ClientResponse<Response> response = collectionObjectClient.create(multipart);
191 int responseStatus = response.getStatus();
192 if (logger.isDebugEnabled() == true) {
193 if (responseStatus != Response.Status.CREATED.getStatusCode())
194 logger.debug("Status of call to create CollectionObject was: " +
198 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
199 result = extractId(response);
201 response.releaseConnection();
208 * Creates the collection objects.
210 * @param numberOfObjects the number of objects
211 * @return the string[]
213 public String[] createCollectionObjects(int numberOfObjects) {
214 Random randomGenerator = new Random(System.currentTimeMillis());
215 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
216 String[] coList = new String[numberOfObjects];
218 int createdObjects = 0;
220 Date startTime = new Date();
221 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
222 coList[i] = createCollectionObject(collectionObjectClient, i + 1);
223 if (logger.isDebugEnabled() == true) {
224 logger.debug("Created CollectionObject #: " + i);
227 Date stopTime = new Date();
228 if (logger.isDebugEnabled()) {
229 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
230 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
232 } catch (AssertionError e) {
233 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
235 Assert.assertTrue(false);
242 * Delete collection object.
244 * @param collectionObjectClient the collection object client
245 * @param resourceId the resource id
247 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
249 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
250 res.releaseConnection();
254 * Delete collection objects.
256 * @param arrayOfObjects the array of objects
258 public void deleteCollectionObjects(String[] arrayOfObjects) {
259 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
261 Date startTime = new Date();
262 for (int i = 0; i < arrayOfObjects.length; i++) {
263 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
265 Date stopTime = new Date();
267 if (logger.isDebugEnabled()) {
268 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
269 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
274 * Delete collection objects.
275 * FIXME: Deletes a page at a time until there are no more CollectionObjects.
277 public void deleteCollectionObjects() {
278 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
279 ClientResponse<AbstractCommonList> response;
281 List<CollectionObjectListItem> coListItems = null;
283 response = collectionObjectClient.readList(Integer.toString(MAX_RECORDS),
284 Integer.toString(0));
286 CollectionobjectsCommonList commonListElement =
287 (CollectionobjectsCommonList)response.getEntity(CollectionobjectsCommonList.class);
288 coListItems = commonListElement.getCollectionObjectListItem();
290 response.releaseConnection();
293 Date startTime = new Date();
294 for (CollectionObjectListItem i:coListItems) {
295 deleteCollectionObject(collectionObjectClient, i.getCsid());
297 Date stopTime = new Date();
299 if (logger.isDebugEnabled()) {
300 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
301 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
303 } while (coListItems.size() > 0);