001 package jp.osdl.jbento2.analyzer;
002
003 import java.io.File;
004 import java.util.regex.Matcher;
005 import java.util.regex.Pattern;
006
007 import org.apache.commons.cli.CommandLine;
008 import org.apache.commons.cli.CommandLineParser;
009 import org.apache.commons.cli.HelpFormatter;
010 import org.apache.commons.cli.OptionBuilder;
011 import org.apache.commons.cli.Options;
012 import org.apache.commons.cli.ParseException;
013 import org.apache.commons.cli.PosixParser;
014
015 /**
016 * Generate graphs. graph file name and title is same source csv file.
017 *
018 *
019 */
020 public class GCAllCli {
021
022 private static final String FILE_NAME = ".*\\.gc\\.log";
023 private String srcDirName;
024
025 private String destDirName;
026
027 private String startTime;
028
029 private long rampUpMillis = 0;
030
031 private long durationMillis = 0;
032
033 public static void main(String[] args) throws Exception {
034 Options options = new Options();
035
036 options.addOption(OptionBuilder.withArgName("source dir").hasArg()
037 .withDescription("source directory.").withLongOpt("source")
038 .create("s"));
039
040 options.addOption(OptionBuilder.withArgName("dest dir").hasArg()
041 .withDescription("destination directory.").withLongOpt("dest")
042 .create("d"));
043
044 options.addOption(OptionBuilder.withArgName("start time").hasArg()
045 .withDescription("start time[HH:mm:ss]").withLongOpt("start")
046 .create("t"));
047
048 options.addOption(OptionBuilder.withArgName("skip time").hasArg()
049 .withDescription("skip time[sec]").withLongOpt("skip")
050 .create("k"));
051
052 options.addOption(OptionBuilder.withArgName("mean time").hasArg()
053 .withDescription("mean time[sec]").withLongOpt("mean")
054 .create("m"));
055
056 options.addOption("h", "help", false, "display help");
057
058 if (args.length == 0) {
059 HelpFormatter formatter = new HelpFormatter();
060 formatter.printHelp("GCAllCli", options, true);
061 return;
062 }
063
064 CommandLineParser parser = new PosixParser();
065 CommandLine line = null;
066 try {
067 line = parser.parse(options, args);
068 } catch (ParseException ex) {
069 throw ex;
070 }
071
072 if (line.hasOption("h")
073 || (line.getOptions().length == 0 && line.getArgs().length == 0)) {
074 HelpFormatter formatter = new HelpFormatter();
075 formatter.printHelp("GCAllCli", options, true);
076 return;
077 }
078
079 GCAllCli cli = new GCAllCli();
080 cli.setSrcDirName(line.getOptionValue("s"));
081 cli.setDestDirName(line.getOptionValue("d"));
082 cli.setStartTime(line.getOptionValue("t"));
083 cli.setRampUpMillis(line.getOptionValue("k"));
084 cli.setDurationMillis(line.getOptionValue("m"));
085 cli.execute();
086 }
087
088 public void execute() throws Exception {
089 File srcDir = new File(srcDirName);
090 if (!srcDir.isDirectory()) {
091 throw new IllegalArgumentException(srcDir.getName()
092 + "is not a directory");
093 }
094 traverse(srcDir);
095 }
096
097 void traverse(File dir) throws Exception {
098 File[] files = dir.listFiles();
099 for (int i = 0; i < files.length; i++) {
100 File file = files[i];
101 if (file.isDirectory()) {
102 traverse(file);
103 } else {
104 Pattern pattern = Pattern.compile(FILE_NAME);
105 Matcher matcher = pattern.matcher(file.getName());
106 if (matcher.matches()) {
107 GCAnalyzer analyzer = new GCAnalyzer();
108 analyzer.setSrc(file.getAbsolutePath());
109 if (destDirName != null) {
110 analyzer.setDest(getDest(file.getAbsolutePath()));
111 }
112 analyzer.setStartTime(startTime);
113 analyzer.setRampUpMillis(rampUpMillis);
114 analyzer.setDurationMillis(durationMillis);
115 System.out.println("analyzing " + file.getName());
116 analyzer.analyze();
117 }
118 }
119 }
120 }
121
122 private String getDest(String filePath) {
123 // for cygwin
124 String srcDirName = this.srcDirName.replace('/', File.separatorChar);
125 if (srcDirName.charAt(srcDirName.length() - 1) == File.separatorChar) {
126 srcDirName = srcDirName.substring(0, srcDirName.length() - 1);
127 }
128
129 String postDir = null;
130
131 try {
132 int index = filePath.indexOf(srcDirName);
133 postDir = filePath.substring(index + srcDirName.length(),
134 filePath.lastIndexOf(File.separatorChar));
135 } catch (IndexOutOfBoundsException e) {
136 System.err.println(e.getMessage());
137 System.err.println("filePath=" + filePath);
138 System.err.println("srcDirName=" + srcDirName);
139 System.err.println("File.separator=" + File.separator);
140 throw e;
141 }
142
143 String dest = destDirName + postDir;
144 File destDir = new File(dest);
145 if (!destDir.isDirectory()) {
146 if (!destDir.mkdirs()) {
147 throw new IllegalArgumentException("destDir " + destDir
148 + " cannot be created");
149 }
150 }
151 String destName = dest
152 + filePath.substring(filePath.lastIndexOf(File.separator),
153 filePath.lastIndexOf('.')) + ".csv";
154 return destName;
155 }
156
157 public String getDestDirName() {
158 return destDirName;
159 }
160
161 public void setDestDirName(String destDirName) {
162 this.destDirName = destDirName;
163 }
164
165 public String getStartTime() {
166 return startTime;
167 }
168
169 public void setStartTime(String startTime) {
170 this.startTime = startTime;
171 }
172
173 public String getSrcDirName() {
174 return srcDirName;
175 }
176
177 public void setSrcDirName(String srcDirName) {
178 this.srcDirName = srcDirName;
179 }
180
181 public long getDurationMillis() {
182 return durationMillis;
183 }
184
185 public void setDurationMillis(String durationMillis) {
186 if (durationMillis != null) {
187 this.durationMillis = Long.parseLong(durationMillis) * 1000;
188 }
189 }
190
191 public long getRampUpMillis() {
192 return rampUpMillis;
193 }
194
195 public void setRampUpMillis(String rampUpMillis) {
196 if (rampUpMillis != null) {
197 this.rampUpMillis = Long.parseLong(rampUpMillis) * 1000;
198 }
199 }
200
201 }