001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.stress;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import edu.iu.uis.eden.clientapp.WorkflowDocument;
024: import edu.iu.uis.eden.clientapp.WorkflowInfo;
025: import edu.iu.uis.eden.clientapp.vo.ActionRequestVO;
026: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
027: import edu.iu.uis.eden.clientapp.vo.WorkflowAttributeDefinitionVO;
028:
029: public class BasicTest extends AbstractTest {
030:
031: static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
032: .getLogger(BasicTest.class);
033:
034: private String initiator;
035: private String documentTypeName;
036: private String applicationContent;
037: private List attributeDefinitions = new ArrayList();
038: private boolean createIt = true;
039: private Long documentId;
040: private int errorsThusFar = 0;
041: private int maxErrors = 5;
042:
043: public BasicTest() {
044: }
045:
046: public BasicTest(String initiator, String documentTypeName)
047: throws Exception {
048: this (initiator, documentTypeName, null, new ArrayList());
049: }
050:
051: public BasicTest(String initiator, String documentTypeName,
052: String applicationContent, List attributeDefinitions)
053: throws Exception {
054: LOG.info("Initializing Basic Test with initiator=" + initiator
055: + ", documentType=" + documentTypeName
056: + ", applicationContent=" + applicationContent);
057: this .initiator = initiator;
058: this .documentTypeName = documentTypeName;
059: if (attributeDefinitions == null)
060: attributeDefinitions = new ArrayList();
061: this .applicationContent = applicationContent;
062: this .attributeDefinitions = attributeDefinitions;
063: }
064:
065: public boolean doWork() throws Exception {
066: long start = System.currentTimeMillis();
067: boolean finalized = false;
068: try {
069: if (createIt) {
070: long createStart = System.currentTimeMillis();
071: WorkflowDocument document = createDocument();
072: long createEnd = System.currentTimeMillis();
073: LOG.info("Created test document: "
074: + (createEnd - createStart) + " ms");
075: document.routeDocument("Stress Test route. initiator="
076: + initiator + ", documentType="
077: + documentTypeName);
078: long routeEnd = System.currentTimeMillis();
079: TestInfo.markCallToServer();
080: LOG.info("Routed test document: "
081: + document.getRouteHeaderId() + " in "
082: + (routeEnd - createEnd) + " ms.");
083: if (document.getRouteHeaderId() == null) {
084: throw new Exception(
085: "Document was not assigned a Document id.");
086: }
087: }
088: createIt = false;
089: long finalizeStart = System.currentTimeMillis();
090: finalized = finalizeDocument();
091: long finalizeEnd = System.currentTimeMillis();
092: LOG.info("Time to run finalizeDocument(): "
093: + (finalizeEnd - finalizeStart));
094: } catch (Exception e) {
095: errorsThusFar++;
096: if (errorsThusFar >= maxErrors) {
097: LOG.fatal("Test exceeded its maxium number of errors!",
098: e);
099: throw e;
100: }
101: LOG.error(
102: "An error occurred when attempting to do work on test, errorsThusFar="
103: + errorsThusFar, e);
104: }
105: long end = System.currentTimeMillis();
106: LOG.info("Total time to doWork(): " + (end - start) + " ms");
107: return finalized;
108: }
109:
110: private WorkflowDocument createDocument() throws Exception {
111: WorkflowDocument document = new WorkflowDocument(
112: new NetworkIdVO(initiator), documentTypeName);
113: documentId = document.getRouteHeaderId();
114: TestInfo.markCallToServer();
115: TestInfo.addRouteHeaderId(document.getRouteHeaderId());
116: for (Iterator iterator = attributeDefinitions.iterator(); iterator
117: .hasNext();) {
118: WorkflowAttributeDefinitionVO definition = (WorkflowAttributeDefinitionVO) iterator
119: .next();
120: document.addAttributeDefinition(definition);
121: }
122: if (applicationContent != null
123: && !applicationContent.equals("")) {
124: document.setApplicationContent(applicationContent);
125: }
126: return document;
127: }
128:
129: protected boolean finalizeDocument() throws Exception {
130: LOG.info("Finalizing document " + documentId);
131: NetworkIdVO initiatorId = new NetworkIdVO(initiator);
132: //do {
133: //LOG.info("Sleeping for 10 seconds...");
134: //Thread.sleep(10000);
135: long t1 = System.currentTimeMillis();
136: WorkflowDocument document = new WorkflowDocument(initiatorId,
137: documentId);
138: long t2 = System.currentTimeMillis();
139: TestInfo.markCallToServer();
140: if (document.stateIsFinal()) {
141: return true;
142: }
143: if (document.stateIsException() || document.stateIsCanceled()
144: || document.stateIsDisapproved()) {
145: String message = "Document ended up in an illegal state: "
146: + document.getStatusDisplayValue();
147: LOG.error(message);
148: throw new Exception(message);
149: }
150: long t3 = System.currentTimeMillis();
151: ActionRequestVO[] requests = new WorkflowInfo()
152: .getActionRequests(documentId);
153: long t4 = System.currentTimeMillis();
154: TestInfo.markCallToServer();
155: StressTestUtils.handleRequests(documentId, requests);
156: long t5 = System.currentTimeMillis();
157: document = new WorkflowDocument(initiatorId, documentId);
158: long t6 = System.currentTimeMillis();
159: TestInfo.markCallToServer();
160: LOG.info("Time to load initial document: " + (t2 - t1));
161: LOG.info("Time to do some minor logic: " + (t3 - t2));
162: LOG.info("Time to load Action Requests: " + (t4 - t3));
163: LOG.info("Total time to Handle all Requests: " + (t5 - t4));
164: LOG.info("Time to load document after handling requests: "
165: + (t6 - t5));
166: return document.stateIsFinal();
167: //} while (!document.stateIsFinal());
168: }
169:
170: public String getApplicationContent() {
171: return applicationContent;
172: }
173:
174: public void setApplicationContent(String applicationContent) {
175: this .applicationContent = applicationContent;
176: }
177:
178: public List getAttributeDefinitions() {
179: return attributeDefinitions;
180: }
181:
182: public void setAttributeDefinitions(List attributeDefinitions) {
183: this .attributeDefinitions = attributeDefinitions;
184: }
185:
186: public String getDocumentTypeName() {
187: return documentTypeName;
188: }
189:
190: public void setDocumentTypeName(String documentTypeName) {
191: this .documentTypeName = documentTypeName;
192: }
193:
194: public String getInitiator() {
195: return initiator;
196: }
197:
198: public void setInitiator(String initiator) {
199: this .initiator = initiator;
200: }
201:
202: public Long getDocumentId() {
203: return documentId;
204: }
205:
206: public void setDocumentId(Long documentId) {
207: this.documentId = documentId;
208: }
209: }
|