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.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Date;
29 import java.util.Random;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.Marshaller;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
42 import org.apache.commons.httpclient.Header;
43 import org.apache.commons.httpclient.HttpClient;
44 import org.apache.commons.httpclient.HttpException;
45 import org.apache.commons.httpclient.HttpStatus;
46 import org.apache.commons.httpclient.methods.GetMethod;
47 import org.apache.commons.httpclient.methods.HeadMethod;
48 import org.apache.commons.httpclient.methods.OptionsMethod;
49 import org.apache.commons.httpclient.methods.TraceMethod;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import org.jboss.resteasy.client.ClientResponse;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
59 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
61 import org.collectionspace.services.client.TestServiceClient;
63 import org.collectionspace.services.CollectionObjectJAXBSchema;
64 import org.collectionspace.services.client.CollectionObjectClient;
65 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
66 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
67 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
69 import org.collectionspace.services.IntakeJAXBSchema;
70 import org.collectionspace.services.client.IntakeClient;
71 import org.collectionspace.services.intake.IntakesCommon;
72 import org.collectionspace.services.intake.IntakesCommonList;
74 import org.collectionspace.services.common.relation.RelationJAXBSchema;
75 import org.collectionspace.services.client.RelationClient;
76 import org.collectionspace.services.relation.RelationsCommon;
77 import org.collectionspace.services.relation.RelationsCommonList;
78 import org.collectionspace.services.relation.RelationshipType;
83 * @version $Revision:$
85 public class PerformanceTest extends CollectionSpacePerformanceTest {
87 private static final int MAX_KEYWORDS = 10;
88 private static final int MAX_SEARCHES = 10;
89 final Logger logger = LoggerFactory
90 .getLogger(PerformanceTest.class);
92 // Get clients for the CollectionSpace services
94 private static int MAX_RECORDS = 1000;
97 public void performanceTest() {
98 roundTripOverhead(10);
99 deleteCollectionObjects();
100 String[] coList = this.createCollectionObjects(MAX_RECORDS);
101 this.searchCollectionObjects(MAX_RECORDS);
102 // this.deleteCollectionObjects(coList);
103 roundTripOverhead(10);
106 private long roundTripOverhead(int numOfCalls) {
108 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
111 for (int i = 0; i < numOfCalls; i++) {
112 Date startTime = new Date();
113 collectionObjectClient.roundtrip();
114 Date stopTime = new Date();
115 totalTime = totalTime + (stopTime.getTime() - startTime.getTime());
116 System.out.println("Overhead roundtrip time is: " + (stopTime.getTime() - startTime.getTime()));
119 System.out.println("------------------------------------------------------------------------------");
120 System.out.println("Client to server roundtrip overhead: " + (float)(totalTime / numOfCalls) / 1000);
121 System.out.println("------------------------------------------------------------------------------");
122 System.out.println("");
127 private void searchCollectionObjects(int numberOfObjects) {
128 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
129 Random randomGenerator = new Random(System.currentTimeMillis());
130 ClientResponse<CollectionobjectsCommonList> searchResults;
133 long totalSearchResults = 0;
134 String keywords = "";
136 for (int numOfKeywords = 0; numOfKeywords < MAX_KEYWORDS;
137 numOfKeywords++, totalTime = 0, totalSearchResults = 0, times = "") {
138 keywords = keywords + " " + OBJECT_NAME + randomGenerator.nextInt(numberOfObjects);
139 for (int i = 0; i < MAX_SEARCHES; i++) {
140 Date startTime = new Date();
141 searchResults = collectionObjectClient.keywordSearch(keywords);
142 Date stopTime = new Date();
143 long time = stopTime.getTime() - startTime.getTime();
144 times = times + " " + ((float)time / 1000);
145 totalTime = totalTime + time;
146 totalSearchResults = totalSearchResults +
147 searchResults.getEntity().getCollectionObjectListItem().size();
149 if (logger.isDebugEnabled()) {
150 System.out.println("------------------------------------------------------------------------------");
151 System.out.println("Searched Objects: " + numberOfObjects);
152 System.out.println("Number of keywords: " + numOfKeywords);
153 System.out.println("List of keywords: " + keywords);
154 System.out.println("Number of results: " + totalSearchResults / MAX_SEARCHES);
155 System.out.println("Result times: " + times);
156 System.out.println("Average Retreive time: " + (totalTime / MAX_SEARCHES) / 1000.0 + " seconds.");
157 System.out.println("------------------------------------------------------------------------------");
163 private String createCollectionObject(CollectionObjectClient collectionObjectClient,
165 String result = null;
167 // First create a CollectionObject
169 CollectionobjectsCommon co = new CollectionobjectsCommon();
170 fillCollectionObject(co, Integer.toString(identifier));
172 // Next, create a part object
173 MultipartOutput multipart = new MultipartOutput();
174 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
175 commonPart.getHeaders().add("label", collectionObjectClient.getCommonPartName());
176 // Make the create call and check the response
177 ClientResponse<Response> response = collectionObjectClient.create(multipart);
179 int responseStatus = response.getStatus();
180 if (logger.isDebugEnabled() == true) {
181 if (responseStatus != Response.Status.CREATED.getStatusCode())
182 logger.debug("Status of call to create CollectionObject was: " +
186 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
187 result = extractId(response);
192 public String[] createCollectionObjects(int numberOfObjects) {
193 Random randomGenerator = new Random(System.currentTimeMillis());
194 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
195 String[] coList = new String[numberOfObjects];
197 int createdObjects = 0;
199 Date startTime = new Date();
200 for (int i = 0; i < numberOfObjects; i++, createdObjects++) {
201 coList[i] = createCollectionObject(collectionObjectClient, i + 1);
203 Date stopTime = new Date();
204 if (logger.isDebugEnabled()) {
205 System.out.println("Created " + numberOfObjects + " CollectionObjects" +
206 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
208 } catch (AssertionError e) {
209 System.out.println("FAILURE: Created " + createdObjects + " of " + numberOfObjects +
211 Assert.assertTrue(false);
217 private void deleteCollectionObject(CollectionObjectClient collectionObjectClient,
219 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
222 public void deleteCollectionObjects(String[] arrayOfObjects) {
223 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
225 Date startTime = new Date();
226 for (int i = 0; i < arrayOfObjects.length; i++) {
227 deleteCollectionObject(collectionObjectClient, arrayOfObjects[i]);
230 Date stopTime = new Date();
231 if (logger.isDebugEnabled()) {
232 System.out.println("Deleted " + arrayOfObjects.length + " CollectionObjects" +
233 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");
237 public void deleteCollectionObjects() {
238 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
239 ClientResponse<CollectionobjectsCommonList> commonsList;
240 commonsList = collectionObjectClient.readList();
241 List<CollectionObjectListItem> coListItems = commonsList.getEntity().getCollectionObjectListItem();
243 Date startTime = new Date();
244 for (CollectionObjectListItem i:coListItems) {
245 deleteCollectionObject(collectionObjectClient, i.getCsid());
247 Date stopTime = new Date();
249 if (logger.isDebugEnabled()) {
250 System.out.println("Deleted " + coListItems.size() + " CollectionObjects" +
251 " in " + (stopTime.getTime() - startTime.getTime())/1000.0 + " seconds.");