001: package org.cougaar.core.qos.profile;
002:
003: import java.lang.reflect.*;
004: import java.io.*;
005: import java.text.*;
006: import java.util.*;
007: import java.util.regex.*;
008: import org.cougaar.core.agent.*;
009: import org.cougaar.core.component.*;
010: import org.cougaar.core.mts.*;
011: import org.cougaar.core.node.*;
012: import org.cougaar.core.qos.metrics.*;
013: import org.cougaar.core.service.*;
014: import org.cougaar.core.service.wp.*;
015: import org.cougaar.core.thread.*;
016: import org.cougaar.core.wp.resolver.*;
017: import org.cougaar.util.*;
018:
019: /**
020: * This component profiles the aggregate "messages per second"
021: * throughput for all agents on the local node.
022: * <p>
023: * Example output:<pre>
024: * throughput - #agent_send_count_per_second, total_send_count_per_second,
025: * agent_send_count, total_send_count,
026: * throughput - 160.89, 161.11,
027: * 2913, 2929
028: * </pre>
029: * <p>
030: * For more detailed message traffic profiling, see the {@link
031: * NodeTraffic} profiler.
032: * <p>
033: * This profiler is useful in ping testing, where the first field will
034: * show the total ping throughput in messages per second (assuming all
035: * ping agents are non-node agents, to exclude naming service
036: * traffic).
037: *
038: * @see ProfilerCoordinator required coordinator component
039: */
040: public class Throughput extends ProfilerBase {
041: private static final DecimalFormat FORMAT_DECIMAL = new DecimalFormat(
042: "#0.00");
043: private static final String[] FIELDS = new String[] {
044: "agent_send_count_per_second",
045: "total_send_count_per_second", "agent_send_count",
046: "total_send_count", };
047: private static final String HEADER = toHeader(FIELDS);
048:
049: private MessageAddress localNode;
050: public MessageStatisticsService mss;
051: public AgentStatusService as;
052:
053: private long lastTime;
054: private long lastAgent;
055: private long lastTotal;
056:
057: public void load() {
058: super .load();
059: localNode = findLocalNode();
060: findServiceLater("mss",
061: "org.cougaar.core.service.MessageStatisticsService");
062: findServiceLater("as",
063: "org.cougaar.core.mts.AgentStatusService");
064: }
065:
066: public void run() {
067: log("org.cougaar.core.qos.profile.throughput", HEADER,
068: getThroughput());
069: }
070:
071: private String getThroughput() {
072: long totalSendCount = getTotalSendCount();
073: long nodeSendCount = getNodeSendCount();
074: long agentSendCount = totalSendCount - nodeSendCount;
075:
076: long now = System.currentTimeMillis();
077:
078: double agentSendRate = 0.0;
079: double totalSendRate = 0.0;
080: if (lastTime > 0) {
081: double t = (long) ((now - lastTime) / 1000);
082: if (t < 1.0) {
083: t = 1.0;
084: }
085: agentSendRate = ((double) (agentSendCount - lastAgent) / t);
086: totalSendRate = ((double) (totalSendCount - lastTotal) / t);
087: }
088:
089: lastTime = now;
090: lastAgent = agentSendCount;
091: lastTotal = totalSendCount;
092:
093: return FORMAT_DECIMAL.format(agentSendRate) + ", "
094: + FORMAT_DECIMAL.format(totalSendRate) + ", "
095: + agentSendCount + ", " + totalSendCount;
096: }
097:
098: private long getTotalSendCount() {
099: MessageStatistics.Statistics stats = mss
100: .getMessageStatistics(false);
101: return (stats == null ? 0 : stats.totalSentMessageCount);
102: }
103:
104: private long getNodeSendCount() {
105: AgentStatusService.AgentState state = as
106: .getRemoteAgentState(localNode);
107: return (state == null ? 0 : state.deliveredCount);
108: }
109:
110: private MessageAddress findLocalNode() {
111: NodeIdentificationService nis = (NodeIdentificationService) sb
112: .getService(this , NodeIdentificationService.class, null);
113: if (nis == null) {
114: return null;
115: }
116: MessageAddress ret = nis.getMessageAddress();
117: sb.releaseService(this , NodeIdentificationService.class, nis);
118: return ret;
119: }
120: }
|