001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.activemq.tool.reports.plugins;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.StringTokenizer;
026    
027    import org.apache.activemq.tool.reports.PerformanceStatisticsUtil;
028    
029    public class ThroughputReportPlugin implements ReportPlugin {
030        public static final String KEY_SYS_TOTAL_TP = "SystemTotalTP";
031        public static final String KEY_SYS_TOTAL_CLIENTS = "SystemTotalClients";
032        public static final String KEY_SYS_AVE_TP = "SystemAveTP";
033        public static final String KEY_SYS_AVE_EMM_TP = "SystemAveEMMTP";
034        public static final String KEY_SYS_AVE_CLIENT_TP = "SystemAveClientTP";
035        public static final String KEY_SYS_AVE_CLIENT_EMM_TP = "SystemAveClientEMMTP";
036        public static final String KEY_MIN_CLIENT_TP = "MinClientTP";
037        public static final String KEY_MAX_CLIENT_TP = "MaxClientTP";
038        public static final String KEY_MIN_CLIENT_TOTAL_TP = "MinClientTotalTP";
039        public static final String KEY_MAX_CLIENT_TOTAL_TP = "MaxClientTotalTP";
040        public static final String KEY_MIN_CLIENT_AVE_TP = "MinClientAveTP";
041        public static final String KEY_MAX_CLIENT_AVE_TP = "MaxClientAveTP";
042        public static final String KEY_MIN_CLIENT_AVE_EMM_TP = "MinClientAveEMMTP";
043        public static final String KEY_MAX_CLIENT_AVE_EMM_TP = "MaxClientAveEMMTP";
044    
045        protected Map<String, List> clientThroughputs = new HashMap<String, List>();
046    
047        public void handleCsvData(String csvData) {
048            StringTokenizer tokenizer = new StringTokenizer(csvData, ",");
049            String data;
050            String key;
051            String val;
052            String clientName = null;
053            Long throughput = null;
054            while (tokenizer.hasMoreTokens()) {
055                data = tokenizer.nextToken();
056                key = data.substring(0, data.indexOf("="));
057                val = data.substring(data.indexOf("=") + 1);
058    
059                if (key.equalsIgnoreCase("clientName")) {
060                    clientName = val;
061                } else if (key.equalsIgnoreCase("throughput")) {
062                    throughput = Long.valueOf(val);
063                } else {
064                    // Ignore unknown token
065                }
066            }
067            addToClientTPList(clientName, throughput);
068        }
069    
070        public Map<String, String> getSummary() {
071            // Check if tp sampler wasn't used.
072            if (clientThroughputs.size() == 0) {
073                return new HashMap<String, String>();
074            }
075    
076            long minClientTP = Long.MAX_VALUE; // TP = throughput
077            long maxClientTP = Long.MIN_VALUE;
078            long minClientTotalTP = Long.MAX_VALUE;
079            long maxClientTotalTP = Long.MIN_VALUE;
080            long systemTotalTP = 0;
081    
082            double minClientAveTP = Double.MAX_VALUE;
083            double maxClientAveTP = Double.MIN_VALUE;
084            double minClientAveEMMTP = Double.MAX_VALUE; // EMM = Excluding Min/Max
085            double maxClientAveEMMTP = Double.MIN_VALUE;
086            double systemAveTP = 0.0;
087            double systemAveEMMTP = 0.0;
088    
089            String nameMinClientTP = "";
090            String nameMaxClientTP = "";
091            String nameMinClientTotalTP = "";
092            String nameMaxClientTotalTP = "";
093            String nameMinClientAveTP = "";
094            String nameMaxClientAveTP = "";
095            String nameMinClientAveEMMTP = "";
096            String nameMaxClientAveEMMTP = "";
097    
098            Set<String> clientNames = clientThroughputs.keySet();
099            String clientName;
100            List clientTPList;
101            long tempLong;
102            double tempDouble;
103            int clientCount = 0;
104            for (Iterator<String> i = clientNames.iterator(); i.hasNext();) {
105                clientName = i.next();
106                clientTPList = clientThroughputs.get(clientName);
107                clientCount++;
108    
109                tempLong = PerformanceStatisticsUtil.getMin(clientTPList);
110                if (tempLong < minClientTP) {
111                    minClientTP = tempLong;
112                    nameMinClientTP = clientName;
113                }
114    
115                tempLong = PerformanceStatisticsUtil.getMax(clientTPList);
116                if (tempLong > maxClientTP) {
117                    maxClientTP = tempLong;
118                    nameMaxClientTP = clientName;
119                }
120    
121                tempLong = PerformanceStatisticsUtil.getSum(clientTPList);
122                systemTotalTP += tempLong; // Accumulate total TP
123                if (tempLong < minClientTotalTP) {
124                    minClientTotalTP = tempLong;
125                    nameMinClientTotalTP = clientName;
126                }
127    
128                if (tempLong > maxClientTotalTP) {
129                    maxClientTotalTP = tempLong;
130                    nameMaxClientTotalTP = clientName;
131                }
132    
133                tempDouble = PerformanceStatisticsUtil.getAve(clientTPList);
134                systemAveTP += tempDouble; // Accumulate ave throughput
135                if (tempDouble < minClientAveTP) {
136                    minClientAveTP = tempDouble;
137                    nameMinClientAveTP = clientName;
138                }
139    
140                if (tempDouble > maxClientAveTP) {
141                    maxClientAveTP = tempDouble;
142                    nameMaxClientAveTP = clientName;
143                }
144    
145                tempDouble = PerformanceStatisticsUtil.getAveEx(clientTPList);
146                systemAveEMMTP += tempDouble; // Accumulate ave throughput
147                                                // excluding min/max
148                if (tempDouble < minClientAveEMMTP) {
149                    minClientAveEMMTP = tempDouble;
150                    nameMinClientAveEMMTP = clientName;
151                }
152    
153                if (tempDouble > maxClientAveEMMTP) {
154                    maxClientAveEMMTP = tempDouble;
155                    nameMaxClientAveEMMTP = clientName;
156                }
157            }
158    
159            Map<String, String> summary = new HashMap<String, String>();
160            summary.put(KEY_SYS_TOTAL_TP, String.valueOf(systemTotalTP));
161            summary.put(KEY_SYS_TOTAL_CLIENTS, String.valueOf(clientCount));
162            summary.put(KEY_SYS_AVE_TP, String.valueOf(systemAveTP));
163            summary.put(KEY_SYS_AVE_EMM_TP, String.valueOf(systemAveEMMTP));
164            summary.put(KEY_SYS_AVE_CLIENT_TP, String.valueOf(systemAveTP / clientCount));
165            summary.put(KEY_SYS_AVE_CLIENT_EMM_TP, String.valueOf(systemAveEMMTP / clientCount));
166            summary.put(KEY_MIN_CLIENT_TP, nameMinClientTP + "=" + minClientTP);
167            summary.put(KEY_MAX_CLIENT_TP, nameMaxClientTP + "=" + maxClientTP);
168            summary.put(KEY_MIN_CLIENT_TOTAL_TP, nameMinClientTotalTP + "=" + minClientTotalTP);
169            summary.put(KEY_MAX_CLIENT_TOTAL_TP, nameMaxClientTotalTP + "=" + maxClientTotalTP);
170            summary.put(KEY_MIN_CLIENT_AVE_TP, nameMinClientAveTP + "=" + minClientAveTP);
171            summary.put(KEY_MAX_CLIENT_AVE_TP, nameMaxClientAveTP + "=" + maxClientAveTP);
172            summary.put(KEY_MIN_CLIENT_AVE_EMM_TP, nameMinClientAveEMMTP + "=" + minClientAveEMMTP);
173            summary.put(KEY_MAX_CLIENT_AVE_EMM_TP, nameMaxClientAveEMMTP + "=" + maxClientAveEMMTP);
174    
175            return summary;
176        }
177    
178        protected void addToClientTPList(String clientName, Long throughput) {
179            // Write to client's throughput list
180            if (clientName == null || throughput == null) {
181                throw new IllegalArgumentException("Invalid Throughput CSV Data: clientName=" + clientName + ", throughput=" + throughput);
182            }
183    
184            List<Long> clientTPList = clientThroughputs.get(clientName);
185            if (clientTPList == null) {
186                clientTPList = new ArrayList<Long>();
187                clientThroughputs.put(clientName, clientTPList);
188            }
189            clientTPList.add(throughput);
190        }
191    }