Quellcode durchsuchen

Improve match writing performance, add match writing progress reporting, and some minor clean-up.

Timur Sağlam vor 5 Jahren
Ursprung
Commit
9c751fe2da
1 geänderte Dateien mit 44 neuen und 35 gelöschten Zeilen
  1. 44 35
      jplag/src/main/java/jplag/reporting/Report.java

+ 44 - 35
jplag/src/main/java/jplag/reporting/Report.java

@@ -30,16 +30,18 @@ import jplag.Token;
 public class Report {
 public class Report {
 
 
     private static final String CSV_FILE = "matches_avg.csv";
     private static final String CSV_FILE = "matches_avg.csv";
+    private static final String[] PICS = {"forward.gif", "back.gif"};
+
     private final Map<JPlagComparison, Integer> comparisonToIndex = new HashMap<>();
     private final Map<JPlagComparison, Integer> comparisonToIndex = new HashMap<>();
-    private int currentComparisonIndex = 0;
     private final Messages msg;
     private final Messages msg;
-
-    // SUBMISSION - here it comes...
-    private final String[] pics = {"forward.gif", "back.gif"};
     private final File reportDir;
     private final File reportDir;
 
 
     private JPlagResult result;
     private JPlagResult result;
 
 
+    private int currentComparisonIndex = 0;
+    private int matchWritingProgess = 0;
+    private int matchesWritten = 0;
+
     public Report(File reportDir) throws ExitException {
     public Report(File reportDir) throws ExitException {
         this.reportDir = reportDir;
         this.reportDir = reportDir;
         this.msg = new Messages("en");
         this.msg = new Messages("en");
@@ -49,13 +51,11 @@ public class Report {
 
 
     public void writeResult(JPlagResult result) throws ExitException {
     public void writeResult(JPlagResult result) throws ExitException {
         this.result = result;
         this.result = result;
-        
-        System.out.print("Writing report...");
+        System.out.println("Writing report...");
         writeIndex();
         writeIndex();
-
         copyStaticFiles();
         copyStaticFiles();
-
         writeMatches(result.getComparisons());
         writeMatches(result.getComparisons());
+        System.out.println("Report exported to " + reportDir.getAbsolutePath());
     }
     }
 
 
     /*
     /*
@@ -65,11 +65,11 @@ public class Report {
         int redValue = (int) (Rl + (Rh - Rl) * percent / 100);
         int redValue = (int) (Rl + (Rh - Rl) * percent / 100);
         int greenValue = (int) (Gl + (Gh - Gl) * percent / 100);
         int greenValue = (int) (Gl + (Gh - Gl) * percent / 100);
         int blueValue = (int) (Bl + (Bh - Bl) * percent / 100);
         int blueValue = (int) (Bl + (Bh - Bl) * percent / 100);
-    
+
         String red = (redValue < 16 ? "0" : "") + Integer.toHexString(redValue);
         String red = (redValue < 16 ? "0" : "") + Integer.toHexString(redValue);
         String green = (greenValue < 16 ? "0" : "") + Integer.toHexString(greenValue);
         String green = (greenValue < 16 ? "0" : "") + Integer.toHexString(greenValue);
         String blue = (blueValue < 16 ? "0" : "") + Integer.toHexString(blueValue);
         String blue = (blueValue < 16 ? "0" : "") + Integer.toHexString(blueValue);
-    
+
         return "#" + red + green + blue;
         return "#" + red + green + blue;
     }
     }
 
 
@@ -78,15 +78,15 @@ public class Report {
      */
      */
     private void copyStaticFiles() {
     private void copyStaticFiles() {
         final String[] fileList = {"back.gif", "forward.gif", "help-en.html", "help-sim-en.html", "logo.gif", "fields.js"};
         final String[] fileList = {"back.gif", "forward.gif", "help-en.html", "help-sim-en.html", "logo.gif", "fields.js"};
-    
+
         for (int i = fileList.length - 1; i >= 0; i--) {
         for (int i = fileList.length - 1; i >= 0; i--) {
             try {
             try {
                 URL url = Report.class.getResource("data/" + fileList[i]);
                 URL url = Report.class.getResource("data/" + fileList[i]);
                 DataInputStream dis = new DataInputStream(url.openStream());
                 DataInputStream dis = new DataInputStream(url.openStream());
-    
+
                 File dest = new File(reportDir, fileList[i]);
                 File dest = new File(reportDir, fileList[i]);
                 DataOutputStream dos = new DataOutputStream(new FileOutputStream(dest));
                 DataOutputStream dos = new DataOutputStream(new FileOutputStream(dest));
-    
+
                 byte[] buffer = new byte[1024];
                 byte[] buffer = new byte[1024];
                 int count;
                 int count;
                 do {
                 do {
@@ -116,18 +116,6 @@ public class Report {
         }
         }
     }
     }
 
 
-    private int getComparisonIndex(JPlagComparison comparison) {
-        Integer index = comparisonToIndex.get(comparison);
-    
-        if (index != null) {
-            return index;
-        }
-    
-        comparisonToIndex.put(comparison, currentComparisonIndex++);
-    
-        return currentComparisonIndex - 1;
-    }
-
     /**
     /**
      * This method generates an table entry in the list of all comparisons.
      * This method generates an table entry in the list of all comparisons.
      */
      */
@@ -184,6 +172,15 @@ public class Report {
         htmlFile.println("</TABLE>");
         htmlFile.println("</TABLE>");
     }
     }
 
 
+    private synchronized void reportMatchWritingProgress(List<JPlagComparison> comparisons) {
+        matchesWritten++;
+        int currentProgress = 20 * matchesWritten / comparisons.size() * 5; // report every 5% step
+        if (currentProgress > matchWritingProgess) {
+            System.out.println("Writing matches: " + currentProgress + "%");
+            matchWritingProgess = currentProgress;
+        }
+    }
+
     /**
     /**
      * Make sure the report directory exists, is a directory and has write access.
      * Make sure the report directory exists, is a directory and has write access.
      */
      */
@@ -191,11 +188,11 @@ public class Report {
         if (!reportDir.exists() && !reportDir.mkdirs()) {
         if (!reportDir.exists() && !reportDir.mkdirs()) {
             throw new ExitException("Cannot create report directory!");
             throw new ExitException("Cannot create report directory!");
         }
         }
-    
+
         if (!reportDir.isDirectory()) {
         if (!reportDir.isDirectory()) {
             throw new ExitException(reportDir + " is not a directory!");
             throw new ExitException(reportDir + " is not a directory!");
         }
         }
-    
+
         if (!reportDir.canWrite()) {
         if (!reportDir.canWrite()) {
             throw new ExitException("Cannot write directory: " + reportDir);
             throw new ExitException("Cannot write directory: " + reportDir);
         }
         }
@@ -238,6 +235,18 @@ public class Report {
         htmlFile.println("</TABLE></CENTER>\n<P>\n<HR>");
         htmlFile.println("</TABLE></CENTER>\n<P>\n<HR>");
     }
     }
 
 
+    private int getComparisonIndex(JPlagComparison comparison) {
+        Integer index = comparisonToIndex.get(comparison);
+
+        if (index != null) {
+            return index;
+        }
+
+        comparisonToIndex.put(comparison, currentComparisonIndex++);
+
+        return currentComparisonIndex - 1;
+    }
+
     private void writeHTMLHeader(HTMLFile file, String title) {
     private void writeHTMLHeader(HTMLFile file, String title) {
         file.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
         file.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
         file.println("<HTML><HEAD><TITLE>" + title + "</TITLE>");
         file.println("<HTML><HEAD><TITLE>" + title + "</TITLE>");
@@ -277,7 +286,7 @@ public class Report {
                 if (start.file.equals(files[fileIndex]) && text[fileIndex] != null) {
                 if (start.file.equals(files[fileIndex]) && text[fileIndex] != null) {
                     String tmp = "<FONT color=\"" + Color.getHexadecimalValue(x) + "\">" + (j == 1 ? "<div style=\"position:absolute;left:0\">" : "")
                     String tmp = "<FONT color=\"" + Color.getHexadecimalValue(x) + "\">" + (j == 1 ? "<div style=\"position:absolute;left:0\">" : "")
                             + "<A HREF=\"javascript:ZweiFrames('match" + i + "-" + (1 - j) + ".html#" + x + "'," + (3 - j) + ",'match" + i
                             + "<A HREF=\"javascript:ZweiFrames('match" + i + "-" + (1 - j) + ".html#" + x + "'," + (3 - j) + ",'match" + i
-                            + "-top.html#" + x + "',1)\"><IMG SRC=\"" + pics[j] + "\" ALT=\"other\" " + "BORDER=\"0\" ALIGN=\""
+                            + "-top.html#" + x + "',1)\"><IMG SRC=\"" + PICS[j] + "\" ALT=\"other\" " + "BORDER=\"0\" ALIGN=\""
                             + (j == 0 ? "right" : "left") + "\"></A>" + (j == 1 ? "</div>" : "") + "<B>";
                             + (j == 0 ? "right" : "left") + "\"></A>" + (j == 1 ? "</div>" : "") + "<B>";
                     // position the icon and the beginning of the colorblock
                     // position the icon and the beginning of the colorblock
                     markupList.put(new MarkupText(fileIndex, start.getLine() - 1, start.getColumn() - 1, tmp, true), null);
                     markupList.put(new MarkupText(fileIndex, start.getLine() - 1, start.getColumn() - 1, tmp, true), null);
@@ -622,14 +631,14 @@ public class Report {
         htmlFile.close();
         htmlFile.close();
     }
     }
 
 
-    private void writeMatches(List<JPlagComparison> comparisons) { // TODO TS: We should report the progress if there are many comparisons.
-        comparisons.forEach(comparison -> {
+    private void writeMatches(List<JPlagComparison> comparisons) {
+        comparisons.parallelStream().forEach(comparison -> {
             try {
             try {
-                int i = getComparisonIndex(comparison);
-                writeMatch(comparison, i);
-            } catch (ExitException e) {
-                e.printStackTrace();
+                writeMatch(comparison, getComparisonIndex(comparison));
+            } catch (ExitException exception) {
+                exception.printStackTrace();
             }
             }
+            reportMatchWritingProgress(comparisons);
         });
         });
     }
     }
 
 
@@ -687,7 +696,7 @@ public class Report {
                 if (start.file.equals(files[y]) && text[y] != null) {
                 if (start.file.equals(files[y]) && text[y] != null) {
                     hilf = "<FONT color=\"" + Color.getHexadecimalValue(x) + "\">" + (j == 1 ? "<div style=\"position:absolute;left:0\">" : "")
                     hilf = "<FONT color=\"" + Color.getHexadecimalValue(x) + "\">" + (j == 1 ? "<div style=\"position:absolute;left:0\">" : "")
                             + "<A HREF=\"javascript:ZweiFrames('match" + i + "-" + (1 - j) + ".html#" + x + "'," + (3 - j) + ",'match" + i
                             + "<A HREF=\"javascript:ZweiFrames('match" + i + "-" + (1 - j) + ".html#" + x + "'," + (3 - j) + ",'match" + i
-                            + "-top.html#" + x + "',1)\"><IMG SRC=\"" + pics[j] + "\" ALT=\"other\" " + "BORDER=\"0\" ALIGN=\""
+                            + "-top.html#" + x + "',1)\"><IMG SRC=\"" + PICS[j] + "\" ALT=\"other\" " + "BORDER=\"0\" ALIGN=\""
                             + (j == 0 ? "right" : "left") + "\"></A>" + (j == 1 ? "</div>" : "") + "<B>";
                             + (j == 0 ? "right" : "left") + "\"></A>" + (j == 1 ? "</div>" : "") + "<B>";
                     // position the icon and the beginning of the colorblock
                     // position the icon and the beginning of the colorblock
                     if (text[y][start.getLine() - 1].endsWith("</FONT>")) {
                     if (text[y][start.getLine() - 1].endsWith("</FONT>")) {