001 package jp.osdl.jbento2.analyzer; 002 003 import java.io.BufferedReader; 004 import java.io.File; 005 import java.io.FileNotFoundException; 006 import java.io.FileReader; 007 import java.io.IOException; 008 import java.util.ArrayList; 009 import java.util.Collection; 010 import java.util.HashMap; 011 import java.util.Iterator; 012 import java.util.List; 013 import java.util.Map; 014 import java.util.Set; 015 import java.util.regex.Matcher; 016 import java.util.regex.Pattern; 017 018 import org.apache.commons.cli.*; 019 import org.apache.commons.io.FileUtils; 020 import org.apache.commons.io.filefilter.TrueFileFilter; 021 022 public class MultiSarResultCollectorAllCli { 023 024 private final String JBENTO_FILE = "jbento.conf"; 025 private String delimiter = ","; 026 private Map map = new HashMap(); 027 private String targetRow = null; 028 private String targetColumn = null; 029 private List seriesName = new ArrayList(); 030 private String xLabelName = null; 031 032 public static void main(String[] args) throws Exception { 033 Options options = new Options(); 034 035 options.addOption( 036 OptionBuilder. 037 withArgName("source dir"). 038 hasArg(). 039 withDescription("source directory."). 040 withLongOpt("source"). 041 create("s")); 042 043 options.addOption( 044 OptionBuilder. 045 withArgName("config file"). 046 hasArg(). 047 withDescription("config file"). 048 withLongOpt("conf"). 049 create("f")); 050 051 if (args.length == 0) { 052 HelpFormatter formatter = new HelpFormatter(); 053 formatter.printHelp("MultiResultCollectorAllCli", options, true); 054 return; 055 } 056 057 CommandLineParser parser = new PosixParser(); 058 CommandLine line = null; 059 try { 060 line = parser.parse(options, args); 061 } catch (ParseException ex) { 062 throw ex; 063 } 064 065 if (line.hasOption("h") || 066 (line.getOptions().length == 0 && line.getArgs().length == 0)) { 067 HelpFormatter formatter = new HelpFormatter(); 068 formatter.printHelp("MultiResultCollectorAllCli", options, true); 069 return; 070 } 071 072 String configFile = line.getOptionValue("f"); 073 String srcDir = line.getOptionValue("s"); 074 075 new MultiSarResultCollectorAllCli().execute(configFile, srcDir); 076 } 077 078 public void execute(String configFileName, String srcDirName) 079 throws IOException, ParseException { 080 File configFile = new File(configFileName); 081 List config = parseConfig(configFile); 082 083 File srcDir = new File(srcDirName); 084 085 if (!srcDir.isDirectory()) { 086 throw new IllegalArgumentException(srcDir.getName() 087 + "is not a directory"); 088 } 089 090 Iterator ite = config.iterator(); 091 while (ite.hasNext()) { 092 ResultCollectConfig conf = (ResultCollectConfig) ite.next(); 093 094 Map constName = new HashMap();// ResultCollector.conf?ス?ス--constName?スノ設定さ?ス黷ス?スl?ス?ス?ス?ス?ス 095 List param = conf.getParams(); 096 097 for (int j = 0; j < param.size(); j++) { 098 if (param.get(j).equals("--constName")) { 099 constName.put(param.get(j + 1), param.get(j + 2)); 100 } else if (param.get(j).equals("--xLabelName")) { 101 xLabelName = (String) param.get(j + 1); 102 } else if (param.get(j).equals("--seriesName")) { 103 seriesName.add(param.get(j + 1)); 104 105 } else if (param.get(j).equals("--target")) { 106 targetRow = (String) param.get(j + 1); 107 targetColumn = (String) param.get(j + 2); 108 } 109 } 110 111 traverse(srcDir, conf, constName); 112 113 MultiResultCollector collector = new MultiResultCollector(); 114 collector.setTarget(targetRow); 115 collector.setMap(map); 116 117 String destDirName = srcDir.getAbsolutePath() + File.separator 118 + "result_collect"; 119 File destDir = new File(destDirName); 120 destDir.mkdirs(); 121 122 // TODO ?スo?スヘ撰ソスt?ス@?スC?ス?ス?ス?ス?ス?スw?ス?スナゑソス?ス?ス謔、?スノゑソス?ス?ス 123 String destFileName = "cpu.csv"; 124 collector.setDestName(destDirName + File.separator + destFileName); 125 126 collector.analyze(); 127 } 128 } 129 130 // ?スR?ス?ス?スt?スB?スO?スt?ス@?スC?ス?ス?スヌみ搾ソス?ス?ス 131 private void traverse(File src, ResultCollectConfig config, Map constName) 132 throws IOException, ParseException { 133 Map confMap = new HashMap(); 134 String regexName = null;// ?ス?ス?ス?スホ象フ?ス@?スC?ス?ス?スフ撰ソス?スK?ス\?ス?ス 135 136 File[] files = src.listFiles(); 137 regexName = config.getRegexName(); 138 139 for (int i = 0; i < files.length; i++) { 140 File file = files[i]; 141 if (file.isDirectory()) { 142 traverse(file, config, constName); 143 } else { 144 145 if (file.getName().equals(JBENTO_FILE)) { 146 try { 147 boolean isMatch = false; 148 149 confMap = readConfFile(file); 150 151 Set const_keyset = constName.keySet(); 152 Iterator ite_const = const_keyset.iterator(); 153 154 // constName?スw?ス?スネゑソス 155 if (const_keyset.size() == 0 156 && confMap.containsKey(xLabelName)) { 157 isMatch = true; 158 } 159 while (ite_const.hasNext()) { 160 String const_key = (String) ite_const.next(); 161 if (confMap.get(const_key).equals( 162 constName.get(const_key))) { 163 if (confMap.containsKey(xLabelName)) { 164 isMatch = true; 165 } 166 } else { 167 isMatch = false; 168 break; 169 } 170 } 171 172 // List xyset = new ArrayList(); 173 // match?ス?ス?ス?ス?ス?スA?スホ象フ?ス@?スC?ス?ス?ス?スヌみ搾ソス?ス?ス 174 if (isMatch) { 175 findFile(file, (String) confMap.get(xLabelName), 176 regexName, targetRow); 177 // continue; 178 } else { 179 // noop 180 // continue; 181 } 182 183 } catch (IndexOutOfBoundsException e) { 184 e.printStackTrace(); 185 } 186 } 187 } 188 } 189 } 190 191 protected Map readConfFile(File src) throws IOException { 192 193 BufferedReader reader = null; 194 Map confMap = new HashMap(); 195 try { 196 reader = new BufferedReader(new FileReader(src)); 197 198 while (true) { 199 String line = reader.readLine(); 200 if (line == null) { 201 break; 202 } 203 String[] data = line.split(" "); 204 confMap.put(data[0], data[1]); 205 } 206 207 } finally { 208 if (reader != null) { 209 try { 210 reader.close(); 211 } catch (IOException ignore) { 212 // noop. 213 } 214 } 215 } 216 return confMap; 217 } 218 219 private File determineTargetDir(File src) { 220 String path = src.getAbsolutePath(); 221 if (path.lastIndexOf("conf" + File.separator) == -1) { 222 return null; 223 } 224 return new File( 225 path.substring(0, path.lastIndexOf("conf" + File.separator)) + 226 "report"); 227 } 228 229 private void findFile(File src, String xKey, String regexName, 230 String target) throws IOException { 231 232 File targetDir = determineTargetDir(src); 233 if (targetDir == null) { 234 return; 235 } 236 237 try { 238 Collection c = 239 FileUtils.listFiles(targetDir, 240 TrueFileFilter.INSTANCE, 241 TrueFileFilter.INSTANCE); 242 243 for (Iterator it = c.iterator(); it.hasNext();) { 244 File f = (File) it.next(); 245 246 Pattern pattern = Pattern.compile((String) regexName); 247 Matcher matcher = pattern.matcher(f.getName()); 248 249 if (matcher.matches()) { 250 251 BufferedReader reader = null; 252 try { 253 reader = new BufferedReader(new FileReader(f)); 254 255 String firstLine = reader.readLine(); 256 String[] columns = firstLine.split(delimiter); 257 int columns_index = -1; 258 259 for (int i = 0; i < columns.length; i++) { 260 261 columns[i] = columns[i].trim(); 262 if (columns[i].equals(target)) { 263 columns_index = i; 264 } 265 } 266 267 List keyList = new ArrayList(); 268 keyList.add(matcher.group(1)); 269 keyList.add(xKey); 270 271 while (true) { 272 String line = reader.readLine(); 273 if (line == null) { 274 break; 275 } 276 String[] data = line.split(delimiter); 277 278 for (int i = 0; i < data.length; i++) { 279 data[i] = data[i].trim(); 280 } 281 282 if (!data[0].equals(targetColumn)) { 283 continue; 284 } else { 285 map.put(keyList, data[columns_index]); 286 } 287 } 288 } catch (FileNotFoundException e) { 289 // e.printStackTrace(System.err); 290 291 // noop 292 } finally { 293 if (reader != null) { 294 try { 295 reader.close(); 296 } catch (IOException ignore) { 297 // noop. 298 } 299 } 300 } 301 } 302 } 303 304 } catch (IllegalArgumentException e) { 305 return; 306 } 307 } 308 309 private List parseConfig(File confFile) throws IOException { 310 List result = new ArrayList(); 311 312 BufferedReader reader = new BufferedReader(new FileReader(confFile)); 313 314 String line = null; 315 ResultCollectConfig config = null; 316 while (true) { 317 line = reader.readLine(); 318 if (line == null) { 319 break; 320 } else if (line.startsWith("#")) { 321 continue; 322 } else if (line.length() == 0) { 323 continue; 324 } else if (line.startsWith(" ") || line.startsWith("\t")) { 325 if (config == null) { 326 throw new IllegalStateException("parse error around " 327 + line); 328 } 329 List list = config.getParams(); 330 line = line.trim(); 331 String[] datas = parseLine(line); 332 for (int i = 0; i < datas.length; i++) { 333 String data = datas[i]; 334 list.add(data); 335 } 336 } else { 337 config = new ResultCollectConfig(line); 338 result.add(config); 339 } 340 341 } 342 return result; 343 } 344 345 private String[] parseLine(String str) { 346 ArrayList result = new ArrayList(); 347 char[] chars = str.toCharArray(); 348 StringBuffer buffer = new StringBuffer(); 349 boolean isInternalQuotes = false; 350 for (int i = 0; i < chars.length; i++) { 351 char c = chars[i]; 352 if (c == ' ') { 353 if (isInternalQuotes) { 354 buffer.append(c); 355 } else { 356 result.add(buffer.toString()); 357 buffer = new StringBuffer(); 358 } 359 } else if (c == '"') { 360 isInternalQuotes = !isInternalQuotes; 361 } else { 362 buffer.append(c); 363 } 364 } 365 if (buffer.length() > 0) { 366 result.add(buffer.toString()); 367 } 368 return (String[]) result.toArray(new String[0]); 369 } 370 371 private class ResultCollectConfig { 372 private String regexName = null; 373 374 private List params = new ArrayList(); 375 376 public ResultCollectConfig(String regexName) { 377 this.regexName = regexName; 378 } 379 380 public List getParams() { 381 return params; 382 } 383 384 public String getRegexName() { 385 return regexName; 386 } 387 } 388 }