trace

added average execution time reducer

10/18/2020 5:12:37 PM

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/trace/tools/CalculateMetrics.java b/src/main/java/br/ufrgs/inf/prosoft/trace/tools/CalculateMetrics.java
new file mode 100644
index 0000000..24b6c84
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/trace/tools/CalculateMetrics.java
@@ -0,0 +1,45 @@
+package br.ufrgs.inf.prosoft.trace.tools;
+
+import br.ufrgs.inf.prosoft.trace.Trace;
+import br.ufrgs.inf.prosoft.trace.reader.TraceReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class CalculateMetrics {
+
+    private static final Logger LOGGER = Logger.getLogger(CalculateMetrics.class.getName());
+
+    public static void calculateAverageExecutionTime(String tracePath, String outputPath, String prefix) {
+        List<Trace> traces = TraceReader.parseFile(tracePath);
+        Map<String, List<Long>> methodHasExecutionTimes = new HashMap<>();
+        traces.forEach(trace -> {
+            methodHasExecutionTimes.compute(trace.getName(), (key, previousValue) -> {
+                if (previousValue == null) {
+                    previousValue = new ArrayList<>();
+                }
+                previousValue.add(trace.getExecutionTime());
+                return previousValue;
+            });
+        });
+        try (FileWriter fileWriter = new FileWriter(outputPath, true)) {
+            methodHasExecutionTimes.forEach((method, executionTimes) -> {
+                double sum = executionTimes.stream().reduce(Long::sum).orElse(0L);
+                double average = sum / executionTimes.size();
+                try {
+                    fileWriter.write(prefix + method + "," + average + "\n");
+                } catch (IOException ex) {
+                    LOGGER.log(Level.SEVERE, "IOException {0}", ex);
+                }
+            });
+        } catch (IOException ex) {
+            LOGGER.log(Level.SEVERE, "IOException {0}", ex);
+        }
+    }
+
+}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/trace/tools/Main.java b/src/main/java/br/ufrgs/inf/prosoft/trace/tools/Main.java
index c1f0b9a..cb7c808 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/trace/tools/Main.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/trace/tools/Main.java
@@ -20,6 +20,7 @@ public class Main {
             System.err.println("check <TracePath>");
             System.err.println("diff <FirstTracePath> <SecondTracePath>");
             System.err.println("hash <InputTracePath> <OutputTracePath>");
+            System.err.println("average <TracePath> <ReducePath> <Prefix>");
             System.exit(1);
         }
         tool = args[0];
@@ -28,16 +29,19 @@ public class Main {
             CheckTraces.check(firstPath);
         } else {
             if (args.length < 3) {
-                System.err.println("diff <FirstTracePath> <SecondTracePath>");
-                System.err.println("hash <InputTracePath> <OutputTracePath>");
+                System.err.println("wrong input");
                 System.exit(1);
             }
             secondPath = args[2];
             if (tool.equals("diff")) {
                 DiffTraces.diff(firstPath, secondPath);
-            } else {
+            } else if (tool.equals("hash")) {
                 HashFile.convert(firstPath, secondPath);
+            } else {
+                String prefix = args[3] != null ? args[3] : "";
+                CalculateMetrics.calculateAverageExecutionTime(firstPath, secondPath, prefix);
             }
         }
     }
+
 }