requests-simulator

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
index c174b60..6decb4c 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
@@ -22,6 +22,7 @@ public class Main {
         if (args.length < 2) {
             System.err.println("--profile=<ProfilePath> --time=<time> [--users=<users>] [--log=<LogPath>] [--throughput=<ThroughputPath>]");
             System.err.println("--profile=<ProfilePath> --execute=<LogPath> [--throughput=<ThroughputPath>]");
+            System.err.println("--throughput=<ThroughputPath> --reduce=<ReducePath> --prefix=<prefix>");
             System.exit(1);
         }
 
@@ -38,6 +39,22 @@ public class Main {
             return array[1];
         }));
 
+        String reducePath = arguments.get("reduce");
+        String throughputPath = arguments.get("throughput");
+        String prefix = arguments.get("prefix");
+        if (reducePath != null) {
+            if (throughputPath == null) {
+                System.err.println("<ThroughputPath> is required");
+                System.exit(1);
+            }
+            if (prefix == null) {
+                System.err.println("<prefix> is required");
+                System.exit(1);
+            }
+            Reducer.reduce(throughputPath, reducePath, prefix);
+            System.exit(0);
+        }
+
         String profilePath = arguments.get("profile");
         if (profilePath == null) {
             System.err.println("<profile> is required");
@@ -45,8 +62,6 @@ public class Main {
         }
         Profile profile = ProfileReader.parseFile(profilePath);
 
-        String throughputPath = arguments.get("throughput");
-
         Request.REQUESTS_LOG = throughputPath;
         String logPath = arguments.get("execute");
         if (logPath != null) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Reducer.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Reducer.java
new file mode 100644
index 0000000..7943254
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Reducer.java
@@ -0,0 +1,60 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package br.ufrgs.inf.prosoft.requestssimulator;
+
+import com.google.gson.JsonParser;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ *
+ * @author romulo
+ */
+public class Reducer {
+
+    private static final Logger LOGGER = Logger.getLogger(Reducer.class.getName());
+
+    public static void reduce(String throughputPath, String reducePath, String prefix) {
+        try (Stream<String> lines = Files.lines(Paths.get(throughputPath))) {
+            JsonParser jsonParser = new JsonParser();
+            List<Long> seconds
+                    = lines.map(line -> jsonParser.parse(line).getAsJsonObject().get("time").getAsLong() / 1000)
+                            .collect(Collectors.toList());
+
+            Long min = seconds.stream().min(Long::compare).get();
+            Long max = seconds.stream().max(Long::compare).get();
+
+            Map<Long, Long> secondHasRequests = seconds.stream().map(time -> time - min)
+                    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
+
+            max -= min;
+
+            try (FileWriter fileWriter = new FileWriter(reducePath, true)) {
+                Long last = 0L;
+                for (long i = 0; i <= max; i++) {
+                    Long get = secondHasRequests.get(i);
+                    if (get == null) {
+                        get = 0L;
+                    }
+                    get += last;
+                    fileWriter.write(prefix + i + "," + get + "\n");
+                    last = get;
+                }
+            }
+        } catch (IOException ex) {
+            LOGGER.log(Level.SEVERE, "file not found {0}", throughputPath);
+        }
+    }
+}