1 8 package org.jahia.sqlprofiler; 9 10 import java.io.*; 11 import org.apache.regexp.RE; 12 import org.apache.regexp.RESyntaxException; 13 import java.util.*; 14 import org.jahia.sqlparser.*; 15 import antlr.*; 16 import antlr.collections.AST; 17 import java.text.NumberFormat ; 18 import org.apache.commons.cli.*; 19 20 28 29 public class LogFileParser { 30 31 private static final String GENERATED_INDEXES_FILE = "indexes.sql"; 32 33 private QueryStatistics queryStats = new QueryStatistics(); 34 35 public LogFileParser() { 36 } 37 38 public void parseFile(String fileName, boolean displayQueries, 39 boolean generateIndexes) throws FileNotFoundException, 40 IOException, RESyntaxException { 41 42 File inputFile = new File(fileName); 43 long fileLength = inputFile.length(); 44 BufferedReader in 45 = new BufferedReader(new FileReader(fileName)); 46 System.out.print("Processing file : " + fileName + "..."); 47 RE selectWithOrderQuery = new RE("select[:blank:].*[:blank:]from[:blank:].*[:blank:]where[:blank:](.*)[:blank:]order[:blank:]by(.*)([:blank:](asc)|(desc))?"); 48 RE selectQuery = new RE( 49 "select[:blank:].*[:blank:]from[:blank:].*[:blank:]where[:blank:](.*)"); 50 String curLine = null; 51 String lowerCurLine = null; 52 int lineCount = 0; 53 long curFilePos = 0; 54 int lastCompletion = 0; 55 do { 56 curLine = in.readLine(); 57 if (curLine == null) { 58 break; 59 } 60 curFilePos += curLine.getBytes().length; 61 lineCount++; 62 lowerCurLine = curLine.toLowerCase(); 63 String occurenceString = null; 64 if (selectWithOrderQuery.match(lowerCurLine)) { 65 occurenceString = selectWithOrderQuery.getParen(0); 66 } else if (selectQuery.match(lowerCurLine)) { 67 occurenceString = selectQuery.getParen(0); 68 } 69 if (occurenceString != null) { 70 QueryEntry queryEntry = new QueryEntry(); 71 queryEntry.setSqlStatement(occurenceString); 72 queryStats.processSQL(queryEntry); 73 } 74 double completionPercentage = (100.0 * curFilePos) / fileLength; 75 int roundedCompletion = new Double (completionPercentage).intValue(); 76 if ( ( (roundedCompletion % 10) == 0) && 77 (lastCompletion != roundedCompletion)) { 78 System.out.print(roundedCompletion + "%..."); 79 lastCompletion = roundedCompletion; 80 } 81 82 } while (curLine != null); 83 System.out.println("100%"); 84 85 86 System.out.println("Lines read=" + lineCount + " occurencesFound=" + 87 queryStats.getOccurenceCount()); 88 } 89 90 private void displayOccurenceStats(boolean displayQueries) { 91 Iterator queryStatByOccurenceIter = queryStats.getQueryStatsByOccurence().iterator(); 92 while (queryStatByOccurenceIter.hasNext()) { 93 QueryStatEntry curQueryStat = (QueryStatEntry) 94 queryStatByOccurenceIter.next(); 95 double occurencePourcentage = (100.0 * curQueryStat.getOccurences()) / 96 queryStats.getOccurenceCount(); 97 NumberFormat nf = NumberFormat.getInstance(); 98 nf.setMaximumFractionDigits(2); 99 System.out.println(nf.format(occurencePourcentage) + 100 "% Occurences=" + curQueryStat.getOccurences() + 101 " Table(s)=" + curQueryStat.getTableNames() + 102 " Column(s)=" + curQueryStat.getColumnNames()); 103 104 if (displayQueries) { 105 Iterator queryIter = curQueryStat.getQueries().iterator(); 106 while (queryIter.hasNext()) { 107 String curQuery = (String ) queryIter.next(); 108 System.out.println(" " + curQuery); 109 } 110 } 111 } 112 } 113 114 115 public static void main(String [] args) { 116 LogFileParser logfileParser = new LogFileParser(); 117 CommandLineParser parser = new PosixParser(); 119 boolean displayQueries = false; 120 boolean generateIndexes = false; 121 String indexesFileName = GENERATED_INDEXES_FILE; 122 123 Options options = new Options(); 125 Option indexesOption = OptionBuilder.withLongOpt("indexes") 126 .hasOptionalArg() 127 .withArgName("filename") 128 .withDescription("the file name to which to output the indexes ") 129 .create('i'); 130 options.addOption("q", "with-queries", false, 131 "Display the queries along with the table and column statistics"); 132 options.addOption(indexesOption); 133 options.addOption("h", "help", false, 134 "Print this help message"); 135 136 try { 137 CommandLine line = parser.parse(options, args); 139 140 if (line.hasOption('h') || (args.length == 0)) { 141 HelpFormatter helpFormatter = new HelpFormatter(); 142 StringWriter strWriter = new StringWriter(); 143 PrintWriter ptrWriter = new PrintWriter(strWriter); 144 helpFormatter.printHelp(ptrWriter, 80, 145 "java -jar sqlprofiler.jar log_file_name [options]", "", 146 options, 10, 10, ""); 147 System.out.println(strWriter.toString()); 148 System.exit(0); 149 } 150 151 if (line.hasOption('q')) { 152 displayQueries = true; 153 System.out.println("Query display activated."); 154 } 155 156 if (line.hasOption('i')) { 157 generateIndexes = true; 158 String optionIndexesFileName = line.getOptionValue('i'); 159 if (optionIndexesFileName != null) { 160 indexesFileName = optionIndexesFileName; 161 } 162 System.out.println("Indexes generation in file [" + 163 indexesFileName + "] activated."); 164 } 165 166 } catch (ParseException exp) { 167 System.out.println("Unexpected exception:"); 168 exp.printStackTrace(); 169 System.exit(0); 170 } 171 try { 172 logfileParser.parseFile(args[0], displayQueries, generateIndexes); 173 logfileParser.displayOccurenceStats(displayQueries); 174 } catch (FileNotFoundException fnfe) { 175 fnfe.printStackTrace(); 176 } catch (IOException ioe) { 177 ioe.printStackTrace(); 178 } catch (RESyntaxException rese) { 179 rese.printStackTrace(); 180 } 181 182 if (generateIndexes) { 183 System.out.print("Writing indexes SQL to file [" + indexesFileName + 184 "]..."); 185 try { 186 FileWriter fileWriter = new FileWriter(indexesFileName); 187 PrintWriter writer = new PrintWriter(fileWriter); 188 Iterator indexNameIter = logfileParser.queryStats.getGeneratedIndexes(). 189 keySet().iterator(); 190 while (indexNameIter.hasNext()) { 191 String curIndexName = (String ) indexNameIter.next(); 192 String indexSql = (String ) logfileParser.queryStats.getGeneratedIndexes(). 193 get(curIndexName); 194 writer.println(indexSql); 195 } 196 writer.flush(); 197 writer.close(); 198 } catch (IOException ioe) { 199 ioe.printStackTrace(); 200 } 201 System.out.println("done."); 202 } 203 } 204 205 206 }
| Popular Tags
|