Merge remote-tracking branch 'upstream/master'

This commit is contained in:
wenshao
2012-08-27 15:52:27 +08:00
5 changed files with 113 additions and 19 deletions
@@ -25,6 +25,7 @@ public class Option {
private PrintStyle style = PrintStyle.HORIZONTAL;
private int printDataType = 0;
private int vmid = -1;
private int id = -1;
public void addPrintDataType(int newValue) {
this.printDataType= this.printDataType | newValue;
@@ -33,7 +34,7 @@ public class Option {
public static boolean isPrintHelp(String[] args) {
if (args == null ) return true;
for (String arg: args) {
if (arg.equals("-h") || arg.equals("--help")) {
if (arg.equals("-help") ) {
return true;
}
}
@@ -112,6 +113,13 @@ public class Option {
option.setStyle(PrintStyle.HORIZONTAL);
} else if (args[i].equals("-s2")) {
option.setStyle(PrintStyle.VERTICAL);
} else if (args[i].equals("-id")) {
try {
int id = Integer.parseInt(args[i+1]);
option.setId(id);
} catch (NumberFormatException e) {
throw new OptionParseException("vmid argument is not a integer!");
}
}
i++;
}
@@ -122,7 +130,7 @@ public class Option {
}
public static void printHelp() {
System.out.println("Usage: druidStat -help | -sql -ds -act -s1 -s2 vmid");
System.out.println("Usage: druidStat -help | -sql -ds -act -s1 -s2 -id vmid");
System.out.println();
System.out.println("arguments: ");
System.out.println(" vmid the process id of jvm that running druid");
@@ -131,8 +139,17 @@ public class Option {
System.out.println(" -act print active connection stacktrace data");
System.out.println(" -s1 print field name in first row");
System.out.println(" -s2 print field name in first column");
System.out.println(" -id only print the sql data matched with id, use this with -sql argument" );
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@@ -19,11 +19,26 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.sql.SQLUtils;
public class TabledDataPrinter {
private static final int SQL_MAX_LEN = 32;
private static final String[] sqlFields = new String[] {
private static final int MAX_COL = 4;
private static final String[] sqlRowTitle = new String[] {
"ID", "SQL", "ExecCount", "ExecTime",
"ExecMax","Txn", "Error","Update","FetchRow","Running","Concurrent",
"ExecHisto", "ExecRsHisto","FetchRowHisto","UpdateHisto" };
private static final String[] sqlRowField = new String[] {
"ID", "SQL", "ExecuteCount", "TotalTime", "MaxTimespan",
"InTransactionCount", "ErrorCount", "EffectedRowCount", "FetchRowCount",
"RunningCount","ConcurrentMax","Histogram","ExecuteAndResultHoldTimeHistogram",
"FetchRowCountHistogram","EffectedRowCountHistogram" };
private static final String[] sqlColField = new String[] {
"ID", "DataSource", "SQL", "ExecuteCount", "ErrorCount",
"TotalTime", "LastTime", "MaxTimespan", "LastError", "EffectedRowCount",
"FetchRowCount", "MaxTimespanOccurTime", "BatchSizeMax", "BatchSizeTotal", "ConcurrentMax",
@@ -33,7 +48,21 @@ public class TabledDataPrinter {
"Histogram", "LastSlowParameters", "ResultSetHoldTime", "ExecuteAndResultSetHoldTime", "FetchRowCountHistogram",
"EffectedRowCountHistogram", "ExecuteAndResultHoldTimeHistogram", "EffectedRowCountMax", "FetchRowCountMax", "ClobOpenCount" };
private static final String[] dsFields = new String[] {
private static final String[] dsRowTitle = new String[] {
"Name", "DbType",
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
"ActiveCount", "ActivePeak", "ActivePeakTime",
"ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" };
private static final String[] dsRowField = new String[] {
"Name", "DbType",
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
"ActiveCount", "ActivePeak", "ActivePeakTime",
"ExecuteCount", "ErrorCount", "CommitCount", "RollbackCount" };
private static final String[] dsColField = new String[] {
"Identity", "Name", "DbType", "DriverClassName", "URL", "UserName", "FilterClassNames",
"WaitThreadCount", "NotEmptyWaitCount", "NotEmptyWaitMillis",
"PoolingCount", "PoolingPeak", "PoolingPeakTime",
@@ -60,29 +89,48 @@ public class TabledDataPrinter {
public static void printDataSourceData(List<Map<String, Object>> content,Option opt) {
if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) {
System.out.println(getFormattedOutput(content, dsFields));
System.out.println(getFormattedOutput(content, dsRowTitle, dsRowField));
} else {
System.out.println(getVerticalFormattedOutput(content, dsFields));
System.out.println(getVerticalFormattedOutput(content, dsColField));
}
}
public static void printSqlData(List<Map<String, Object>> content, Option opt) {
if (opt.getId() != -1) {
List<Map<String,Object>> matchedContent = new ArrayList<Map<String,Object>>();
for (Map<String, Object> sqlStat : content) {
Integer idStr = (Integer)sqlStat.get("ID");
if (idStr.intValue() == opt.getId()) {
matchedContent.add(sqlStat);
String dbType = (String) sqlStat.get("DbType");
String sql = (String) sqlStat.get("SQL");
System.out.println(SQLUtils.format(sql, dbType));
System.out.println("===============================\n");
break;
}
}
content = matchedContent;
}
if (opt.getStyle() == Option.PrintStyle.HORIZONTAL) {
System.out.println(getFormattedOutput(content, sqlFields));
System.out.println(getFormattedOutput(content, sqlRowTitle, sqlRowField));
} else {
System.out.println(getVerticalFormattedOutput(content, sqlFields));
System.out.println(getVerticalFormattedOutput(content, sqlColField));
}
}
public static String getFormattedOutput(List<Map<String, Object>> content, String[] titleFields) {
public static String getFormattedOutput(List<Map<String, Object>> content,
String[] title , String[] rowField) {
List<String[]> printContents = new ArrayList<String[]>();
printContents.add(titleFields);
printContents.add(title);
for (Map<String, Object> sqlStat : content) {
String[] row = new String[titleFields.length];
for (int i = 0; i < titleFields.length; ++i) {
Object value = sqlStat.get(titleFields[i]);
row[i] = handleAndConvert(value,titleFields[i]);
String[] row = new String[rowField.length];
for (int i = 0; i < rowField.length; ++i) {
Object value = sqlStat.get(rowField[i]);
row[i] = handleAndConvert(value,rowField[i]);
}
printContents.add(row);
}
@@ -94,10 +142,12 @@ public class TabledDataPrinter {
public static String getVerticalFormattedOutput(List<Map<String, Object>> content, String[] titleFields) {
List<String[]> printContents = new ArrayList<String[]>();
int maxCol = content.size() > MAX_COL ? MAX_COL: content.size();
for (int i = 0; i < titleFields.length; ++i) {
String[] row = new String[content.size()+1];
String[] row = new String[maxCol+1];
row[0] = titleFields[i];
for (int j=0; j<content.size(); j++) {
for (int j=0; j< maxCol ; j++) {
Map<String,Object> sqlStat = content.get(j);
Object value = sqlStat.get(titleFields[i]);
row[j+1] = handleAndConvert(value, titleFields[i]);
@@ -112,6 +162,8 @@ public class TabledDataPrinter {
if (value == null) value = "";
if (fieldName.equals("SQL")) {
String sql = (String)value;
sql = sql.replace("\n", " ");
sql = sql.replace("\t", " ");
if (sql.length() > SQL_MAX_LEN) {
sql = sql.substring(0,SQL_MAX_LEN-3) + "...";
}