|
| 1 | +package es.upv.mist.slicing.benchmark; |
| 2 | + |
| 3 | +import com.github.javaparser.ParseException; |
| 4 | +import com.github.javaparser.Problem; |
| 5 | +import com.github.javaparser.StaticJavaParser; |
| 6 | +import com.github.javaparser.ast.CompilationUnit; |
| 7 | +import com.github.javaparser.ast.NodeList; |
| 8 | +import com.github.javaparser.ast.stmt.ReturnStmt; |
| 9 | +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; |
| 10 | +import es.upv.mist.slicing.arcs.pdg.StructuralArc; |
| 11 | +import es.upv.mist.slicing.graphs.augmented.ASDG; |
| 12 | +import es.upv.mist.slicing.graphs.augmented.PSDG; |
| 13 | +import es.upv.mist.slicing.graphs.exceptionsensitive.AllenSDG; |
| 14 | +import es.upv.mist.slicing.graphs.exceptionsensitive.ESSDG; |
| 15 | +import es.upv.mist.slicing.graphs.jsysdg.JSysDG; |
| 16 | +import es.upv.mist.slicing.graphs.jsysdg.OriginalJSysDG; |
| 17 | +import es.upv.mist.slicing.graphs.sdg.SDG; |
| 18 | +import es.upv.mist.slicing.nodes.GraphNode; |
| 19 | +import es.upv.mist.slicing.nodes.SyntheticNode; |
| 20 | +import es.upv.mist.slicing.slicing.OriginalJSysDGSlicingAlgorithm; |
| 21 | +import es.upv.mist.slicing.slicing.SlicingCriterion; |
| 22 | +import es.upv.mist.slicing.utils.NodeHashSet; |
| 23 | +import es.upv.mist.slicing.utils.StaticTypeSolver; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileNotFoundException; |
| 27 | +import java.io.PrintWriter; |
| 28 | +import java.util.*; |
| 29 | +import java.util.function.Predicate; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +public class BenchSC { |
| 34 | + protected static final int BUILD_TIMES = 0, SLICE_TIMES = 1, SLICE_SIZES = 2, EXIT = 3; |
| 35 | + protected final String[] dirIncludeSet = System.getProperty("sInclude", "").split(":"); |
| 36 | + protected String graphType; |
| 37 | + |
| 38 | + public void benchmark() { |
| 39 | + // Obtain parameters |
| 40 | + String baselineGraph = System.getProperty("sGraphBaseline"); |
| 41 | + String benchGraph = System.getProperty("sGraphBench"); |
| 42 | + int minIter = Integer.parseInt(System.getProperty("sMinIter", "100")); |
| 43 | + String outputPrefix = System.getProperty("sOutputPrefix", "result"); |
| 44 | + |
| 45 | + // Files |
| 46 | + File buildBaseTime = new File(outputPrefix + "buildBaseTime.out"); |
| 47 | + File buildBenchTime = new File(outputPrefix + "buildBenchTime.out"); |
| 48 | + File nodeCount = new File(outputPrefix + "nodesBaseline.out"); |
| 49 | + File sliceBaseTime = new File(outputPrefix + "sliceBaseTime.out"); |
| 50 | + File sliceBenchTime = new File(outputPrefix + "sliceBenchTime.out"); |
| 51 | + |
| 52 | + // Configure JavaParser |
| 53 | + StaticJavaParser.getConfiguration().setAttributeComments(false); |
| 54 | + StaticTypeSolver.addTypeSolverJRE(); |
| 55 | + for (String directory : dirIncludeSet) |
| 56 | + StaticTypeSolver.addTypeSolver(new JavaParserTypeSolver(directory)); |
| 57 | + |
| 58 | + while (true) { |
| 59 | + switch (selectOption()) { |
| 60 | + case BUILD_TIMES: |
| 61 | + graphType = baselineGraph; |
| 62 | + timedRun(this::buildGraph, minIter, buildBaseTime); |
| 63 | + graphType = benchGraph; |
| 64 | + timedRun(this::buildGraph, minIter, buildBenchTime); |
| 65 | + break; |
| 66 | + case SLICE_SIZES: |
| 67 | + try (PrintWriter pw = new PrintWriter(nodeCount)) { |
| 68 | + graphType = "JSysDG"; |
| 69 | + SDG baseSDG = buildGraph(); |
| 70 | + pw.println("# File format: for each SDG type, the total number of nodes and then the number of "); |
| 71 | + pw.println(baseSDG.vertexSet().size()); |
| 72 | + Collection<SlicingCriterion> baseCriteria = findSCs(baseSDG); |
| 73 | + System.out.printf("There are %d return object SCs", findReturnObjectSCs(baseSDG).size()); |
| 74 | + System.out.printf("There are %d real nodes SCs", findRealSCs(baseSDG).size()); |
| 75 | + System.exit(0); |
| 76 | + for (SlicingCriterion sc : baseCriteria) { |
| 77 | + int baseNodes = new OriginalJSysDGSlicingAlgorithm((JSysDG) baseSDG).traverse(sc.findNode(baseSDG)).getGraphNodes().size(); |
| 78 | + int benchNodes = baseSDG.slice(sc).getGraphNodes().size(); |
| 79 | + pw.printf("\"%s\",%d,%d\n", sc, baseNodes, benchNodes); |
| 80 | + } |
| 81 | + } catch (FileNotFoundException e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + break; |
| 85 | + case SLICE_TIMES: |
| 86 | + try { |
| 87 | + graphType = baselineGraph; |
| 88 | + SDG sdg1 = buildGraph(); |
| 89 | + try (PrintWriter pw = new PrintWriter(sliceBaseTime)) { |
| 90 | + pw.println("# SC id, SC time sequence"); |
| 91 | + for (SlicingCriterion sc : findSCs(sdg1)) |
| 92 | + timedRun(() -> sdg1.slice(sc), minIter, pw); |
| 93 | + } |
| 94 | + graphType = benchGraph; |
| 95 | + SDG sdg2 = buildGraph(); |
| 96 | + try (PrintWriter pw = new PrintWriter(sliceBenchTime)) { |
| 97 | + pw.println("# SC id, SC time sequence"); |
| 98 | + for (SlicingCriterion sc : findSCs(sdg2)) |
| 99 | + timedRun(() -> sdg2.slice(sc), minIter, pw); |
| 100 | + } |
| 101 | + } catch (FileNotFoundException e) { |
| 102 | + throw new RuntimeException(e); |
| 103 | + } |
| 104 | + break; |
| 105 | + case EXIT: |
| 106 | + return; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + protected int selectOption() { |
| 112 | + Scanner in = new Scanner(System.in); |
| 113 | + System.out.println("Select an option:"); |
| 114 | + System.out.println("\t[0]: Time the building of the graphs"); |
| 115 | + System.out.println("\t[1]: Time the slicing of the graphs"); |
| 116 | + System.out.println("\t[2]: Number of nodes per slice"); |
| 117 | + System.out.println("\t[3]: Exit"); |
| 118 | + System.out.print("> "); |
| 119 | + return in.nextInt(); |
| 120 | + } |
| 121 | + |
| 122 | + protected SDG buildGraph() { |
| 123 | + try { |
| 124 | + // Build the SDG |
| 125 | + Set<CompilationUnit> units = new NodeHashSet<>(); |
| 126 | + List<Problem> problems = new LinkedList<>(); |
| 127 | + for (File file : (Iterable<File>) findAllJavaFiles(dirIncludeSet)::iterator) |
| 128 | + parse(file, units, problems); |
| 129 | + if (!problems.isEmpty()) { |
| 130 | + for (Problem p : problems) |
| 131 | + System.out.println(" * " + p.getVerboseMessage()); |
| 132 | + throw new ParseException("Some problems were found while parsing files or folders"); |
| 133 | + } |
| 134 | + |
| 135 | + SDG sdg = createGraph(graphType); |
| 136 | + sdg.build(new NodeList<>(units)); |
| 137 | + return sdg; |
| 138 | + } catch (ParseException e) { |
| 139 | + throw new RuntimeException(e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void parse(File file, Set<CompilationUnit> units, List<Problem> problems) { |
| 144 | + try { |
| 145 | + units.add(StaticJavaParser.parse(file)); |
| 146 | + } catch (FileNotFoundException e) { |
| 147 | + problems.add(new Problem(e.getLocalizedMessage(), null, e)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + protected Stream<File> findAllJavaFiles(String[] files) { |
| 152 | + Stream.Builder<File> builder = Stream.builder(); |
| 153 | + for (String fileName : files) { |
| 154 | + File file = new File(fileName); |
| 155 | + if (file.isDirectory()) |
| 156 | + findAllJavaFiles(file, builder); |
| 157 | + else |
| 158 | + builder.accept(file); |
| 159 | + } |
| 160 | + return builder.build(); |
| 161 | + } |
| 162 | + |
| 163 | + protected void findAllJavaFiles(File directory, Stream.Builder<File> builder) { |
| 164 | + File[] files = directory.listFiles(); |
| 165 | + if (files == null) |
| 166 | + return; |
| 167 | + for (File f : files) { |
| 168 | + if (f.isDirectory()) |
| 169 | + findAllJavaFiles(f, builder); |
| 170 | + else if (f.getName().endsWith(".java")) |
| 171 | + builder.accept(f); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + protected SDG createGraph(String graphName) { |
| 176 | + switch (graphName) { |
| 177 | + case "SDG": return new SDG(); |
| 178 | + case "ASDG": return new ASDG(); |
| 179 | + case "PSDG": return new PSDG(); |
| 180 | + case "ESSDG": return new ESSDG(); |
| 181 | + case "AllenSDG": return new AllenSDG(); |
| 182 | + case "JSysDG": return new JSysDG(); |
| 183 | + case "OriginalJSysDG": return new OriginalJSysDG(); |
| 184 | + default: |
| 185 | + throw new IllegalArgumentException(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + protected long[] timedRun(Runnable runnable, int iterations) { |
| 190 | + long[] times = new long[iterations]; |
| 191 | + long t1, t2; |
| 192 | + for (int i = -1; i < iterations; i++) { |
| 193 | + t1 = System.nanoTime(); |
| 194 | + runnable.run(); |
| 195 | + t2 = System.nanoTime(); |
| 196 | + if (i >= 0) |
| 197 | + times[i] = t2 - t1; // Times stored in nanoseconds |
| 198 | + } |
| 199 | + return times; |
| 200 | + } |
| 201 | + |
| 202 | + protected void timedRun(Runnable runnable, int minIter, File file) { |
| 203 | + long[] data = timedRun(runnable, minIter); |
| 204 | + try (PrintWriter pw = new PrintWriter(file)) { |
| 205 | + for (long d : data) |
| 206 | + pw.println(d); |
| 207 | + } catch (FileNotFoundException e) { |
| 208 | + throw new RuntimeException(e); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + protected void timedRun(Runnable runnable, int minIter, PrintWriter pw) { |
| 213 | + long[] data = timedRun(runnable, minIter); |
| 214 | + StringBuilder builder = new StringBuilder(); |
| 215 | + for (int i = 0; i < data.length; i++) { |
| 216 | + if (i > 0) |
| 217 | + builder.append(','); |
| 218 | + builder.append(data[i]); |
| 219 | + } |
| 220 | + pw.println(builder); |
| 221 | + } |
| 222 | + |
| 223 | + protected Collection<SlicingCriterion> findSCs(SDG sdg) { |
| 224 | + return findReturnObjectSCs(sdg); |
| 225 | + } |
| 226 | + |
| 227 | + protected Collection<SlicingCriterion> findRealSCs(SDG sdg) { |
| 228 | + return sdg.vertexSet().stream() |
| 229 | + .filter(Predicate.not(SyntheticNode.class::isInstance)) |
| 230 | + .filter(Predicate.not(GraphNode::isImplicitInstruction)) |
| 231 | + .sorted() |
| 232 | + .map(n -> (SlicingCriterion) graph -> Set.of(n)) |
| 233 | + .collect(Collectors.toList()); |
| 234 | + } |
| 235 | + |
| 236 | + protected Collection<SlicingCriterion> findReturnObjectSCs(SDG sdg) { |
| 237 | + return sdg.vertexSet().stream() |
| 238 | + .filter(gn -> gn.getAstNode() instanceof ReturnStmt) |
| 239 | + .flatMap(gn -> sdg.outgoingEdgesOf(gn).stream() |
| 240 | + .filter(StructuralArc.class::isInstance) |
| 241 | + .map(sdg::getEdgeTarget)) |
| 242 | + .filter(gn -> sdg.outgoingEdgesOf(gn).stream().anyMatch(StructuralArc.class::isInstance)) |
| 243 | + .map(n -> new SlicingCriterion() { |
| 244 | + public Set<GraphNode<?>> findNode(SDG sdg) { return Set.of(n); } |
| 245 | + public String toString() { return n.getLongLabel(); } |
| 246 | + }) |
| 247 | + .collect(Collectors.toList()); |
| 248 | + } |
| 249 | + |
| 250 | + public static void main(String... args) { |
| 251 | + new BenchSC().benchmark(); |
| 252 | + } |
| 253 | +} |
0 commit comments