KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > reports > plugins > CpuReportPlugin


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.tool.reports.plugins;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.activemq.tool.reports.PerformanceStatisticsUtil;
23
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 public class CpuReportPlugin implements ReportPlugin {
31     private static final Log log = LogFactory.getLog(CpuReportPlugin.class);
32
33     public static final String JavaDoc NAME_IGNORE_LIST = "$index$timeUnit$r$b$w$swpd$inact$active$free$buff$cache$si$so$in$";
34     public static final String JavaDoc NAME_BLOCK_RECV = "bi";
35     public static final String JavaDoc NAME_BLOCK_SENT = "bo";
36     public static final String JavaDoc NAME_CTX_SWITCH = "cs";
37     public static final String JavaDoc NAME_USER_TIME = "us";
38     public static final String JavaDoc NAME_SYS_TIME = "sy";
39     public static final String JavaDoc NAME_IDLE_TIME = "id";
40     public static final String JavaDoc NAME_WAIT_TIME = "wa";
41
42     public static final String JavaDoc KEY_BLOCK_RECV = "BlocksReceived";
43     public static final String JavaDoc KEY_BLOCK_SENT = "BlocksSent";
44     public static final String JavaDoc KEY_CTX_SWITCH = "ContextSwitches";
45     public static final String JavaDoc KEY_USER_TIME = "UserTime";
46     public static final String JavaDoc KEY_SYS_TIME = "SystemTime";
47     public static final String JavaDoc KEY_IDLE_TIME = "IdleTime";
48     public static final String JavaDoc KEY_WAIT_TIME = "WaitingTime";
49
50     public static final String JavaDoc KEY_AVE_BLOCK_RECV = "AveBlocksReceived";
51     public static final String JavaDoc KEY_AVE_BLOCK_SENT = "AveBlocksSent";
52     public static final String JavaDoc KEY_AVE_CTX_SWITCH = "AveContextSwitches";
53     public static final String JavaDoc KEY_AVE_USER_TIME = "AveUserTime";
54     public static final String JavaDoc KEY_AVE_SYS_TIME = "AveSystemTime";
55     public static final String JavaDoc KEY_AVE_IDLE_TIME = "AveIdleTime";
56     public static final String JavaDoc KEY_AVE_WAIT_TIME = "AveWaitingTime";
57
58     protected List JavaDoc blockRecv = new ArrayList JavaDoc();
59     protected List JavaDoc blockSent = new ArrayList JavaDoc();
60     protected List JavaDoc ctxSwitch = new ArrayList JavaDoc();
61     protected List JavaDoc userTime = new ArrayList JavaDoc();
62     protected List JavaDoc sysTime = new ArrayList JavaDoc();
63     protected List JavaDoc idleTime = new ArrayList JavaDoc();
64     protected List JavaDoc waitTime = new ArrayList JavaDoc();
65
66     public void handleCsvData(String JavaDoc csvData) {
67         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(csvData, ",");
68         String JavaDoc data, key, val;
69         while (tokenizer.hasMoreTokens()) {
70             data = tokenizer.nextToken();
71             key = data.substring(0, data.indexOf("="));
72             val = data.substring(data.indexOf("=") + 1);
73
74             addToCpuList(key, val);
75         }
76     }
77
78     public Map JavaDoc getSummary() {
79         long val;
80
81         Map JavaDoc summary = new HashMap JavaDoc();
82
83         if (blockRecv.size() > 0) {
84             val = PerformanceStatisticsUtil.getSum(blockRecv);
85             summary.put(KEY_BLOCK_RECV, String.valueOf(val));
86             summary.put(KEY_AVE_BLOCK_RECV, String.valueOf((double)val / (double)blockRecv.size()));
87         }
88
89         if (blockSent.size() > 0) {
90             val = PerformanceStatisticsUtil.getSum(blockSent);
91             summary.put(KEY_BLOCK_SENT, String.valueOf(val));
92             summary.put(KEY_AVE_BLOCK_SENT, String.valueOf((double)val / (double)blockSent.size()));
93         }
94
95         if (ctxSwitch.size() > 0) {
96             val = PerformanceStatisticsUtil.getSum(ctxSwitch);
97             summary.put(KEY_CTX_SWITCH, String.valueOf(val));
98             summary.put(KEY_AVE_CTX_SWITCH, String.valueOf((double)val / (double)ctxSwitch.size()));
99         }
100
101         if (userTime.size() > 0) {
102             val = PerformanceStatisticsUtil.getSum(userTime);
103             summary.put(KEY_USER_TIME, String.valueOf(val));
104             summary.put(KEY_AVE_USER_TIME, String.valueOf((double)val / (double)userTime.size()));
105         }
106
107         if (sysTime.size() > 0) {
108             val = PerformanceStatisticsUtil.getSum(sysTime);
109             summary.put(KEY_SYS_TIME, String.valueOf(val));
110             summary.put(KEY_AVE_SYS_TIME, String.valueOf((double)val / (double)sysTime.size()));
111         }
112
113         if (idleTime.size() > 0) {
114             val = PerformanceStatisticsUtil.getSum(idleTime);
115             summary.put(KEY_IDLE_TIME, String.valueOf(val));
116             summary.put(KEY_AVE_IDLE_TIME, String.valueOf((double)val / (double)idleTime.size()));
117         }
118
119         if (waitTime.size() > 0) {
120             val = PerformanceStatisticsUtil.getSum(waitTime);
121             summary.put(KEY_WAIT_TIME, String.valueOf(val));
122             summary.put(KEY_AVE_WAIT_TIME, String.valueOf((double)val / (double)waitTime.size()));
123         }
124
125         if (summary.size() > 0) {
126             return summary;
127         } else {
128             return null;
129         }
130     }
131
132     protected void addToCpuList(String JavaDoc key, String JavaDoc val) {
133         if (key.equals(NAME_BLOCK_RECV)) {
134             blockRecv.add(Long.valueOf(val));
135         } else if (key.equals(NAME_BLOCK_SENT)) {
136             blockSent.add(Long.valueOf(val));
137         } else if (key.equals(NAME_CTX_SWITCH)) {
138             ctxSwitch.add(Long.valueOf(val));
139         } else if (key.equals(NAME_USER_TIME)) {
140             userTime.add(Long.valueOf(val));
141         } else if (key.equals(NAME_SYS_TIME)) {
142             sysTime.add(Long.valueOf(val));
143         } else if (key.equals(NAME_IDLE_TIME)) {
144             idleTime.add(Long.valueOf(val));
145         } else if (key.equals(NAME_WAIT_TIME)) {
146             waitTime.add(Long.valueOf(val));
147         } else if (NAME_IGNORE_LIST.indexOf("$" + key + "$") != -1) {
148             // Ignore key
149
} else {
150             log.warn("Unrecognized CPU data. " + key + "=" + val);
151         }
152     }
153 }
154
Popular Tags