requests-simulator

added log parameter

4/29/2019 4:40:58 AM

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/LogExecutor.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/LogExecutor.java
index dfde57c..e7e54b1 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/LogExecutor.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/LogExecutor.java
@@ -32,12 +32,14 @@ import java.util.Collection;
  */
 public class LogExecutor {
 
+    private static final Logger LOGGER = Logger.getLogger(LogExecutor.class.getName());
+
     public static void execute(Profile profile, String tracePath) {
         String fileContent = null;
         try (Stream<String> lines = Files.lines(Paths.get(tracePath))) {
             fileContent = lines.collect(Collectors.joining());
         } catch (IOException ex) {
-            Logger.getLogger(ProfileReader.class.getName()).log(Level.SEVERE, null, ex);
+            LOGGER.log(Level.SEVERE, null, ex);
         }
         JsonParser jsonParser = new JsonParser();
         Collection<Thread> threads = new ArrayList<>();
@@ -83,7 +85,7 @@ public class LogExecutor {
             try {
                 thread.join();
             } catch (InterruptedException ex) {
-                Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
+                LOGGER.log(Level.SEVERE, null, ex);
             }
         });
     }
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 298f780..cc96d4c 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
@@ -5,51 +5,54 @@
  */
 package br.ufrgs.inf.prosoft.requestssimulator;
 
-import java.util.logging.Logger;
-
 /**
  *
  * @author romulo
  */
 public class Main {
 
-    private static final Logger logger = Logger.getLogger(Main.class.getName());
-
     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>]");
+            System.err.println("<ProfilePath> <time> [<users>] [<LogPath>]");
             System.err.println("<ProfilePath> <TracePath>");
             System.exit(1);
-        } else {
-            profilePath = args[0];
+        }
+        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 {
-                time = Long.valueOf(args[1]);
+                users = Integer.valueOf(args[2]);
             } catch (NumberFormatException ex) {
-                if (args.length == 3) {
-                    System.err.println("<time> must be a number");
-                    System.exit(1);
-                } else {
-                    tracePath = args[1];
-                }
+                logPath = args[2];
+                System.out.println("<users> not informed. Using default " + users);
             }
-            if (args.length == 3) {
-                try {
-                    users = Integer.valueOf(args[2]);
-                } catch (NumberFormatException ex) {
-                    System.err.println("<users> must be a number");
-                    System.exit(1);
-                }
+        } 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);
             }
+            logPath = args[3];
         }
         Profile profile = ProfileReader.parseFile(profilePath);
         if (tracePath == null) {
-            Simulator.simulate(profile, time, users);
+            Simulator.simulate(profile, time, users, logPath);
         } else {
             LogExecutor.execute(profile, tracePath);
         }
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java
index e8f1d33..4792b02 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java
@@ -23,13 +23,21 @@ import java.util.logging.Logger;
  */
 public class Simulator {
 
-    private static final Logger logger = Logger.getLogger(Simulator.class.getName());
+    private static final Logger LOGGER = Logger.getLogger(Simulator.class.getName());
 
     public static final void simulate(Profile profile, long time) {
         simulate(profile, time, 1);
     }
 
     public static final void simulate(Profile profile, long time, int users) {
+        simulate(profile, time, users, null);
+    }
+
+    public static final void simulate(Profile profile, long time, String logPath) {
+        simulate(profile, time, 1, logPath);
+    }
+
+    public static final void simulate(Profile profile, long time, int users, String logPath) {
         Collection<Thread> threads = new ArrayList<>();
         List<List<List<JsonObject>>> logs = new ArrayList<>();
         for (int i = 0; i < users; i++) {
@@ -45,16 +53,15 @@ public class Simulator {
             try {
                 thread.join();
             } catch (InterruptedException ex) {
-                Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
+                LOGGER.log(Level.SEVERE, null, ex);
             }
         });
-        String logPath = System.getenv("SIMULATOR_LOG_PATH");
         if (logPath != null) {
             Gson gson = new GsonBuilder().setPrettyPrinting().create();
             try (Writer writer = new FileWriter(logPath)) {
                 gson.toJson(logs, writer);
             } catch (IOException ex) {
-                Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, "invalid log path");
+                LOGGER.log(Level.SEVERE, "invalid log path");
             }
         }
     }