|
| 1 | +package gin.util.analysis; |
| 2 | +import java.io.File; |
| 3 | +import java.io.FileReader; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.FileFilter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.TreeSet; |
| 16 | + |
| 17 | +import org.apache.commons.io.FileUtils; |
| 18 | +import org.apache.commons.io.filefilter.WildcardFileFilter; |
| 19 | +import org.pmw.tinylog.Logger; |
| 20 | + |
| 21 | +import com.opencsv.CSVReader; |
| 22 | +import com.opencsv.CSVReaderBuilder; |
| 23 | +import com.opencsv.CSVWriterBuilder; |
| 24 | +import com.opencsv.ICSVWriter; |
| 25 | +import com.sampullara.cli.Args; |
| 26 | +import com.sampullara.cli.Argument; |
| 27 | + |
| 28 | + |
| 29 | +public class MergeProfilerFiles { |
| 30 | + |
| 31 | + @Argument(alias = "d", description = "Root directory for filename patterns; defaults to current dir", required = false) |
| 32 | + protected String dir = "."; |
| 33 | + |
| 34 | + @Argument(alias = "if", description = "Comma separated list of filename patterns, e.g. spark.Profiler_output_*.csv or spark.Profiler_output_1.csv,spark.Profiler_output_2.csv,spark.Profiler_output_3.csv", required = true) |
| 35 | + protected String inputFiles; |
| 36 | + |
| 37 | + @Argument(alias = "of", description = "Output: e.g. spark.Profiler_output.csv", required = true) |
| 38 | + protected File outputFile; |
| 39 | + |
| 40 | + @Argument(alias = "n", description = "Only methods in at least n repeats will be included; defaults to the number of files (so keep only the intersection of hot methods)", required = false) |
| 41 | + protected int n = -1; |
| 42 | + |
| 43 | + |
| 44 | + // this will |
| 45 | + // only methods in at least n repeats will be included |
| 46 | + // count is total |
| 47 | + // rank is based on total of counts |
| 48 | + // tests include all seen over the files (union) |
| 49 | + |
| 50 | + public static void main(String[] args) { |
| 51 | + Logger.info("A tool to merge Gin profiler files."); |
| 52 | + Logger.info("Only methods in at least n repeats will be included"); |
| 53 | + Logger.info("n defaults to the number of files (so keep only the intersection of hot methods)"); |
| 54 | + Logger.info("In the output, 'count' is sum of counts across all repeat runs; 'rank' is based on the aggregated count"); |
| 55 | + Logger.info("Tests for each method are all those seen over the input files (union)"); |
| 56 | + |
| 57 | + MergeProfilerFiles m = new MergeProfilerFiles(args); |
| 58 | + try { |
| 59 | + m.mergeProfiles(); |
| 60 | + } catch (IOException e) { |
| 61 | + Logger.error("IOException processing files."); |
| 62 | + Logger.error(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public MergeProfilerFiles(String[] args) { |
| 67 | + Args.parseOrExit(this, args); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public void mergeProfiles() throws IOException { |
| 72 | + |
| 73 | + // figure out the filenames |
| 74 | + List<File> inputs = new ArrayList<>(); |
| 75 | + |
| 76 | + for (String pattern : inputFiles.split(",")) { |
| 77 | + File[] files = new File(dir).listFiles((FileFilter)new WildcardFileFilter(pattern)); |
| 78 | + inputs.addAll(Arrays.asList(files)); |
| 79 | + } |
| 80 | + Collections.sort(inputs); |
| 81 | + |
| 82 | + if (n < 0) { |
| 83 | + n = inputs.size(); |
| 84 | + } else { |
| 85 | + n = Math.min(n, inputs.size()); |
| 86 | + } |
| 87 | + |
| 88 | + Logger.info("Found " + inputs.size() + " profiler files. Keeping hot methods appearing in at least " + n + " files."); |
| 89 | + |
| 90 | + Map<String, ProfiledMethod> methods = new HashMap<String, ProfiledMethod>(); |
| 91 | + String project = ""; |
| 92 | + |
| 93 | + for (File input : inputs) { |
| 94 | + CSVReader inCSV = new CSVReader(new FileReader(input)); |
| 95 | + |
| 96 | + inCSV.skip(1); |
| 97 | + for (String[] s : inCSV) { |
| 98 | + // cols are: Project,Rank,Method,Count,Tests |
| 99 | + project = s[0]; |
| 100 | + ProfiledMethod pm = methods.get(s[2]); |
| 101 | + if (pm == null) { |
| 102 | + pm = new ProfiledMethod(s[2]); |
| 103 | + methods.put(s[2], pm); |
| 104 | + } |
| 105 | + pm.counts.add(Integer.parseInt(s[3])); |
| 106 | + String[] tests = s[4].split(","); |
| 107 | + pm.tests.addAll(Arrays.asList(tests)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + List<ProfiledMethod> sortedMethods = new ArrayList<>(); |
| 112 | + for (ProfiledMethod pm : methods.values()) { |
| 113 | + if (pm.counts.size() == n) { |
| 114 | + sortedMethods.add(pm); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Collections.sort(sortedMethods, new Comparator<ProfiledMethod>() { |
| 119 | + @Override |
| 120 | + public int compare(ProfiledMethod arg0, ProfiledMethod arg1) { |
| 121 | + return Integer.compare(arg1.getTotalCount(), arg0.getTotalCount()); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + ICSVWriter writer = new CSVWriterBuilder(new FileWriter(outputFile)).build(); |
| 126 | + writer.writeNext(new String[] {"Project","Rank","Method","Count","Tests"}); |
| 127 | + |
| 128 | + int i = 1; |
| 129 | + for (ProfiledMethod pm : sortedMethods) { |
| 130 | + writer.writeNext(new String[] { |
| 131 | + project, |
| 132 | + Integer.toString(i++), |
| 133 | + pm.methodSignature, |
| 134 | + Integer.toString(pm.getTotalCount()), |
| 135 | + pm.getCSVTests()} |
| 136 | + ); |
| 137 | + } |
| 138 | + writer.close(); |
| 139 | + |
| 140 | + |
| 141 | + Logger.info("All done."); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + private static class ProfiledMethod { |
| 146 | + List<Integer> counts = new ArrayList<>(); |
| 147 | + Set<String> tests = new TreeSet<>(); |
| 148 | + String methodSignature; |
| 149 | + public ProfiledMethod(String methodSignature) { |
| 150 | + this.methodSignature = methodSignature; |
| 151 | + } |
| 152 | + |
| 153 | + public int getTotalCount() { |
| 154 | + int sum = 0; |
| 155 | + for (Integer i : counts) { |
| 156 | + sum += i; |
| 157 | + } |
| 158 | + return sum; |
| 159 | + } |
| 160 | + |
| 161 | + public String getCSVTests() { |
| 162 | + String rval = ""; |
| 163 | + for (String s : tests) { |
| 164 | + if (!rval.isEmpty()) { |
| 165 | + rval += ","; |
| 166 | + } |
| 167 | + rval += s; |
| 168 | + } |
| 169 | + return rval; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments