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 cc96d4c..c174b60 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
@@ -5,6 +5,11 @@
  */
 package br.ufrgs.inf.prosoft.requestssimulator;
 
+import br.ufrgs.inf.prosoft.requestssimulator.requests.Request;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 /**
  *
  * @author romulo
@@ -14,47 +19,55 @@ public class Main {
     public static void main(String[] args) {
         System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT+%1$tL] [%4$-7s] [RequestsSimulator] %5$s %n");
 
-        String profilePath = null;
-        String tracePath = null;
-        String logPath = null;
-        long time = 0;
-        int users = 1;
         if (args.length < 2) {
-            System.err.println("<ProfilePath> <time> [<users>] [<LogPath>]");
-            System.err.println("<ProfilePath> <TracePath>");
+            System.err.println("--profile=<ProfilePath> --time=<time> [--users=<users>] [--log=<LogPath>] [--throughput=<ThroughputPath>]");
+            System.err.println("--profile=<ProfilePath> --execute=<LogPath> [--throughput=<ThroughputPath>]");
             System.exit(1);
         }
-        profilePath = args[0];
-        try {
-            time = Long.valueOf(args[1]);
-        } catch (NumberFormatException ex) {
-            if (args.length > 2) {
-                System.err.println("<time> must be a number");
-                System.exit(1);
-            }
-            tracePath = args[1];
-        }
-        if (args.length == 3) {
-            try {
-                users = Integer.valueOf(args[2]);
-            } catch (NumberFormatException ex) {
-                logPath = args[2];
-                System.out.println("<users> not informed. Using default " + users);
-            }
-        } else if (args.length == 4) {
-            try {
-                users = Integer.valueOf(args[2]);
-            } catch (NumberFormatException ex) {
-                System.err.println("<users> must be a number");
-                System.exit(1);
+
+        Map<String, String> arguments = Stream.of(args).map(arg -> {
+            arg = arg.replaceFirst("--", "");
+            String[] split = arg.split("=");
+            if (split.length < 2) {
+                return new String[]{"", ""};
             }
-            logPath = args[3];
+            return split;
+        }).collect(Collectors.toMap(array -> {
+            return array[0];
+        }, array -> {
+            return array[1];
+        }));
+
+        String profilePath = arguments.get("profile");
+        if (profilePath == null) {
+            System.err.println("<profile> is required");
+            System.exit(1);
         }
         Profile profile = ProfileReader.parseFile(profilePath);
-        if (tracePath == null) {
-            Simulator.simulate(profile, time, users, logPath);
-        } else {
-            LogExecutor.execute(profile, tracePath);
+
+        String throughputPath = arguments.get("throughput");
+
+        Request.REQUESTS_LOG = throughputPath;
+        String logPath = arguments.get("execute");
+        if (logPath != null) {
+            LogExecutor.execute(profile, logPath);
+            System.exit(0);
+        }
+
+        long time = 0;
+        try {
+            time = Long.valueOf(arguments.get("time"));
+        } catch (NumberFormatException ex) {
+            System.err.println("<time> must be a number");
+            System.exit(1);
+        }
+        int users = 1;
+        try {
+            users = Integer.valueOf(arguments.get("users"));
+        } catch (NumberFormatException ex) {
+            System.out.println("<users> not informed. Using default " + users);
         }
+        logPath = arguments.get("log");
+        Simulator.simulate(profile, time, users, logPath);
     }
 }
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java
index f1cfc8e..56afb8f 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/DeleteRequest.java
@@ -61,6 +61,7 @@ public class DeleteRequest extends Request {
             } else {
                 Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
             }
+            log(responseCode);
         } catch (MalformedURLException ex) {
             Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
         } catch (IOException ex) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
index 819fb63..bb58de2 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/GetRequest.java
@@ -61,6 +61,7 @@ public class GetRequest extends Request {
             } else {
                 Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
             }
+            log(responseCode);
         } catch (MalformedURLException ex) {
             Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
         } catch (IOException ex) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
index e1901f1..8500f2e 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/MultipartRequest.java
@@ -100,6 +100,7 @@ public class MultipartRequest extends Request {
                 } else {
                     Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
                 }
+                log(responseCode);
             }
         } catch (IOException e) {
             Logger.getGlobal().log(Level.SEVERE, "IOException");
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
index f55bbed..c04593d 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/PostRequest.java
@@ -76,6 +76,7 @@ public class PostRequest extends Request {
             } else {
                 Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), stringURL});
             }
+            log(responseCode);
         } catch (MalformedURLException ex) {
             Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
         } catch (IOException ex) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
index 20b8896..fc804a4 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/requests/Request.java
@@ -10,6 +10,8 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -33,6 +35,7 @@ public abstract class Request {
     private final String headers;
     private final Collection<String> storeFields;
     private final Session session;
+    public static String REQUESTS_LOG = null;
 
     protected Request(RequestPlan requestPlan, Session session, String URL) {
         this.requestPlan = requestPlan;
@@ -283,6 +286,28 @@ public abstract class Request {
 
     protected abstract void fireRequest();
 
+    protected void log(int statusCode) {
+        if (REQUESTS_LOG == null) {
+            return;
+        }
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("time", System.currentTimeMillis());
+        jsonObject.addProperty("code", statusCode);
+        jsonObject.addProperty("method", getMethod());
+        jsonObject.addProperty("url", getURL());
+        log(jsonObject.toString());
+    }
+
+    private void log(String message) {
+        if (REQUESTS_LOG == null) {
+            return;
+        }
+        try (FileWriter fileWriter = new FileWriter(REQUESTS_LOG, true)) {
+            fileWriter.write(message + "\n");
+        } catch (IOException ex) {
+        }
+    }
+
     public final JsonObject toJsonObject() {
         JsonObject jsonObject = new JsonObject();
         jsonObject.addProperty("reference", this.requestPlan.getReference());
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
index eab37e3..e4a5896 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
@@ -56,7 +56,6 @@ public class Session {
             }
             request.fire();
             JsonObject logRequest = request.toJsonObject();
-            logRequest.addProperty("time", System.currentTimeMillis());
             this.logs.add(logRequest);
             probability = random.nextInt(100);
         }