001 package jp.osdl.jbento2.analyzer;
002
003 import java.io.File;
004 import java.util.ArrayList;
005 import java.util.Arrays;
006 import java.util.HashMap;
007 import java.util.Iterator;
008 import java.util.List;
009 import java.util.Map;
010 import java.util.Set;
011
012 public class SarCPUTotalAverageHandler implements SarAverageHandler {
013
014 private Map data = new HashMap();
015
016 private File src;
017
018 private String key1 = null;
019 private String key2 = null;
020
021 private String[] categories = {"%usr", "%nice","%sys", "%wio"};
022
023 private int[] indexes = new int[4];
024
025 private double value = 0;
026 private long count = 0;
027
028 public void handleFile(File src) {
029 this.src = src;
030 key1 = getKey1(src);
031 key2 = getKey2(src);
032 value = 0;
033 count = 0;
034 }
035
036 public void handleFirstLine(String[] firstLine) {
037 Arrays.fill(indexes, -1);
038 for (int i = 0; i < categories.length; i++) {
039 String category = categories[i];
040 for (int j = 0; j < firstLine.length; j++) {
041 if (category.equals(firstLine[j])) {
042 indexes[i] = j;
043 }
044 }
045 if (indexes[i] == -1) {
046 throw new IllegalArgumentException("category " + category
047 + " is not found in " + src.getName());
048 }
049 }
050 }
051
052 public void handleLine(String[] line) {
053 double usr = Double.parseDouble(line[indexes[0]]);
054 double nice = Double.parseDouble(line[indexes[1]]);
055 double sys = Double.parseDouble(line[indexes[2]]);
056 double wio = Double.parseDouble(line[indexes[3]]);
057
058 double total = usr + nice + sys + wio;
059 value += total;
060 count++;
061 }
062
063 public void handleEnd() {
064 Map values = (Map) data.get(key1);
065 if (values == null) {
066 values = new HashMap();
067 data.put(key1, values);
068 }
069 values.put(key2, new Double(value / count).toString());
070 }
071
072 public TextTable getResult() {
073 int count = 0;
074 Set keys = data.keySet();
075 Iterator ite = keys.iterator();
076 List columns = new ArrayList();
077 while (ite.hasNext()) {
078 Map values = (Map) data.get(ite.next());
079 if (count < values.size()) {
080 count = values.size();
081 columns.clear();
082 Iterator ite2 = values.keySet().iterator();
083 while (ite2.hasNext()) {
084 columns.add(ite2.next());
085 }
086 }
087 }
088 columns.add(0, "");
089
090 TextTable tt = new TextTable(count, TextTable.ALIGN_RIGHT, ",");
091 tt.addRow((String[]) columns.toArray(new String[columns.size()]));
092
093 ite = keys.iterator();
094 while(ite.hasNext()) {
095 String key = (String) ite.next();
096 Map values = (Map) data.get(key);
097 List data = new ArrayList();
098 data.add(key);
099 for(int i = 1; i < columns.size(); i++) {
100 data.add(values.get(columns.get(i)));
101 }
102 tt.addRow((String[]) data.toArray(new String[data.size()]));
103 }
104
105 return tt;
106
107 }
108
109 protected String getKey1(File src) {
110 return src.getParentFile().getParentFile().getName();
111 }
112
113 protected String getKey2(File src) {
114 return src.getParentFile().getName();
115 }
116 }