001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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:
027: package org.cougaar.mts.std;
028:
029: import java.util.Observable;
030: import java.util.Observer;
031:
032: import org.cougaar.bootstrap.SystemProperties;
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.qos.metrics.Metric;
035: import org.cougaar.core.qos.metrics.MetricImpl;
036: import org.cougaar.core.qos.metrics.MetricsService;
037: import org.cougaar.core.qos.metrics.MetricsUpdateService;
038:
039: import org.cougaar.mts.base.StandardAspect;
040:
041: /**
042: * This test Aspect throws lots of data into the metrics service and
043: * subscrobes to a formula which uses that data.
044: */
045: public class MetricsBlastTestAspect extends StandardAspect implements
046: Observer {
047:
048: MetricsUpdateService update;
049: MetricsService svc;
050: Thread blasterThread = null;
051:
052: String key, path;
053:
054: long callbackCount = 0;
055: long blastCount = 0;
056: long lastCallbackDelay = 0;
057: long lastPrintTime = 0;
058:
059: private void dumpCounters(long now) {
060: if (1000 < (now - lastPrintTime)) {
061: System.out.println("blast count=" + blastCount
062: + " callback count=" + callbackCount
063: + " Last delay=" + lastCallbackDelay);
064: lastPrintTime = now;
065: }
066: }
067:
068: public Object getDelegate(Object delegatee, Class type) {
069: return null;
070:
071: }
072:
073: public void load() {
074: super .load();
075: ServiceBroker sb = getServiceBroker();
076: update = (MetricsUpdateService) sb.getService(this ,
077: MetricsUpdateService.class, null);
078: svc = (MetricsService) sb.getService(this ,
079: MetricsService.class, null);
080:
081: path = SystemProperties
082: .getProperty("org.cougaar.metrics.callback");
083: if (path != null) {
084: svc.subscribeToValue(path, this );
085: System.out.println("Subscribed to " + path);
086:
087: key = SystemProperties
088: .getProperty("org.cougaar.metrics.key");
089: if (key != null) {
090: System.out.println("Blasting to " + key);
091: blasterThread = new Thread(new Blaster(), "blaster");
092: blasterThread.start();
093: }
094:
095: }
096: }
097:
098: public void update(Observable o, Object arg) {
099: callbackCount++;
100: long now = System.currentTimeMillis();
101: long value = ((Metric) arg).longValue();
102: lastCallbackDelay = now - value;
103: }
104:
105: class Blaster implements Runnable {
106: long now;
107: long startTime;
108:
109: public void run() {
110: // Wait a bit for the Node to initialize
111: try {
112: Thread.sleep(10000);
113: } catch (InterruptedException xxx) {
114: }
115: // Loop forever turning blaster on and off
116: while (true) {
117: System.out.println("Starting Blaster");
118: startTime = System.currentTimeMillis();
119: // Blast for 5 seconds and then stop
120: while (5000 > (now - startTime)) {
121: now = System.currentTimeMillis();
122: Metric m = new MetricImpl(new Long(System
123: .currentTimeMillis()), 0.3, "",
124: "MetricsTestAspect");
125: update.updateValue(key, m);
126: blastCount++;
127: dumpCounters(now);
128: try {
129: Thread.sleep(0);
130: } catch (InterruptedException xxx) {
131: }
132: }
133:
134: System.out.println("Stopped Blaster");
135: startTime = System.currentTimeMillis();
136: now = System.currentTimeMillis();
137: // wait and see how long it takes for the updates to stop
138: while (10000 > (now - startTime)) {
139: dumpCounters(now);
140: try {
141: Thread.sleep(1000);
142: } catch (InterruptedException xxx) {
143: }
144: now = System.currentTimeMillis();
145: }
146: }
147: }
148: }
149: }
|