001: /*
002: * <copyright>
003: *
004: * Copyright 2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.core.persist;
027:
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031: import java.io.Serializable;
032: import java.util.Iterator;
033: import java.util.Random;
034:
035: import org.cougaar.core.plugin.ServiceUserPlugin;
036:
037: /**
038: * This component exercises the blackboard and can be used to debug
039: * trivial persistence problems.
040: */
041: public class Exercise extends ServiceUserPlugin {
042: private Item[] objects = new Item[1000];
043: private Random random = new Random();
044: private int executionMinDelay;
045: private int executionMaxDelay;
046: private int serializationTime;
047: private int maxPublishCount;
048:
049: private static class Item implements Serializable {
050: int serializationTime;
051: int anotherInt;
052:
053: public Item(int st) {
054: serializationTime = st;
055: }
056:
057: public long wasteTime() {
058: long t = 0;
059: for (int i = 0; i < 10 * serializationTime; i++) {
060: t += System.currentTimeMillis();
061: }
062: return t;
063: }
064:
065: private void writeObject(ObjectOutputStream oos)
066: throws IOException {
067: oos.writeInt(serializationTime);
068: long t = wasteTime();
069: oos.writeLong(t);
070: }
071:
072: private void readObject(ObjectInputStream ois)
073: throws IOException {
074: serializationTime = ois.readInt();
075: ois.readInt();
076: }
077: }
078:
079: public Exercise() {
080: super (new Class[0]);
081: }
082:
083: private int parseParameter(String prefix, int dflt) {
084: for (Iterator i = getParameters().iterator(); i.hasNext();) {
085: String param = (String) i.next();
086: if (param.startsWith(prefix)) {
087: try {
088: return Integer.parseInt(param.substring(prefix
089: .length()));
090: } catch (Exception e) {
091: logger.error("parseParameter error: " + param);
092: return dflt;
093: }
094: }
095: }
096: return dflt;
097: }
098:
099: public void setupSubscriptions() {
100: int nItems = parseParameter("nItems=", 1000);
101: executionMinDelay = parseParameter("executionMinDelay=", 5000);
102: executionMaxDelay = parseParameter("executionMaxDelay=", 50000);
103: serializationTime = parseParameter("serializationTime=", 1000);
104: maxPublishCount = parseParameter("maxPublishCount=", 400);
105: objects = new Item[nItems];
106: logger.warn("Running " + blackboardClientName);
107: resetTimer(executionMinDelay
108: + random.nextInt(executionMaxDelay - executionMinDelay));
109: }
110:
111: // private void wasteTime(Item item, int factor) {
112: // for (int i = 0; i < factor; i++) {
113: // item.wasteTime();
114: // }
115: // }
116:
117: public void execute() {
118: if (timerExpired()) {
119: long st = System.currentTimeMillis();
120: cancelTimer();
121: int n = random.nextInt(maxPublishCount);
122: for (int i = 0; i < n; i++) {
123: int x = random.nextInt(objects.length);
124: if (objects[x] == null) {
125: objects[x] = new Item(random
126: .nextInt(serializationTime));
127: // wasteTime(objects[x], 1);
128: blackboard.publishAdd(objects[x]);
129: } else {
130: // wasteTime(objects[i], 1);
131: blackboard.publishChange(objects[x]);
132: }
133: }
134: long et = System.currentTimeMillis();
135: logger.warn("execute for " + (et - st) + " millis");
136: resetTimer(executionMinDelay
137: + random.nextInt(executionMaxDelay
138: - executionMinDelay));
139: }
140: }
141: }
|