requests-simulator

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Executor.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Executor.java
new file mode 100644
index 0000000..d431c5c
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Executor.java
@@ -0,0 +1,34 @@
+/*
+ * 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 java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ *
+ * @author romulo
+ */
+public class Executor {
+
+    private static final Logger LOGGER = Logger.getLogger(Executor.class.getName());
+
+    public static final void execute(List<Profile> profiles) {
+        List<Thread> threads = profiles.stream().map(profile -> new Thread(() -> {
+            profile.execute();
+        })).collect(Collectors.toList());
+        threads.forEach(Thread::start);
+        threads.forEach(thread -> {
+            try {
+                thread.join();
+            } catch (InterruptedException 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 191f47e..92230ab 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Main.java
@@ -6,6 +6,7 @@
 package br.ufrgs.inf.prosoft.requestssimulator;
 
 import br.ufrgs.inf.prosoft.requestssimulator.requests.Request;
+import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -70,7 +71,8 @@ public class Main {
         Request.REQUESTS_LOG = throughputPath;
         String logPath = arguments.get("execute");
         if (logPath != null) {
-            LogExecutor.execute(profile, logPath);
+            List<Profile> profiles = LogReader.parseFile(logPath, profile);
+            Executor.execute(profiles);
             System.exit(0);
         }
 
diff --git a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
index 7ebea54..f080de2 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Profile.java
@@ -6,12 +6,12 @@
 package br.ufrgs.inf.prosoft.requestssimulator;
 
 import br.ufrgs.inf.prosoft.requestssimulator.requests.RequestPlan;
-import com.google.gson.JsonObject;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Stream;
 
 /**
  *
@@ -21,7 +21,7 @@ public class Profile {
 
     private final Collection<RequestPlan> roots;
     private final Map<String, RequestPlan> urlHasRequestPlan;
-    private List<List<JsonObject>> logs;
+    private final List<Session> sessions;
 
     public Profile(Map<String, RequestPlan> urlHasRequestPlan) {
         this.urlHasRequestPlan = urlHasRequestPlan;
@@ -40,28 +40,42 @@ public class Profile {
             });
         });
         this.roots = rootsReferences.values();
+        this.sessions = new ArrayList<>();
+    }
+
+    public Profile(List<Session> sessions) {
+        this.sessions = sessions;
+        this.roots = null;
+        this.urlHasRequestPlan = null;
+    }
+
+    public Stream<Session> sessions() {
+        return this.sessions.stream();
     }
 
     public Session newSession() {
-        return new Session(this.roots);
+        Session session = new Session(this.roots);
+        this.sessions.add(session);
+        return session;
     }
 
     public RequestPlan getRequestPlan(String requestPlanReference) {
         return this.urlHasRequestPlan.get(requestPlanReference);
     }
 
-    public List<List<JsonObject>> getLogs() {
-        return logs;
-    }
-
-    public void run(long time) {
-        this.logs = new ArrayList<>();
+    public void simulate(long time) {
         long end = System.currentTimeMillis() + time;
         while (System.currentTimeMillis() < end) {
-            Session session = new Session(this.roots);
-            session.run();
-            List<JsonObject> sessionLogs = session.getLogs();
-            this.logs.add(sessionLogs);
+            newSession().simulate();
         }
     }
+
+    public void execute() {
+        this.sessions.forEach(Session::execute);
+    }
+
+    @Override
+    protected Profile clone() {
+        return new Profile(this.urlHasRequestPlan);
+    }
 }
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 e4a5896..d4c161b 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Session.java
@@ -7,13 +7,13 @@ package br.ufrgs.inf.prosoft.requestssimulator;
 
 import br.ufrgs.inf.prosoft.requestssimulator.requests.Request;
 import br.ufrgs.inf.prosoft.requestssimulator.requests.RequestPlan;
-import com.google.gson.JsonObject;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.stream.Stream;
 
 /**
  *
@@ -23,11 +23,18 @@ public class Session {
 
     private final Collection<RequestPlan> roots;
     private final Map<String, String> storedValues;
-    private List<JsonObject> logs;
+    private final List<Request> requests;
 
     public Session(Collection<RequestPlan> roots) {
         this.roots = roots;
         this.storedValues = new HashMap<>();
+        this.requests = new ArrayList<>();
+    }
+
+    public Session(List<Request> requests) {
+        this.roots = null;
+        this.storedValues = new HashMap<>();
+        this.requests = requests;
     }
 
     public Session storeValue(String field, String value) {
@@ -39,12 +46,11 @@ public class Session {
         return this.storedValues.get(storedField);
     }
 
-    public List<JsonObject> getLogs() {
-        return logs;
+    public Stream<Request> requests() {
+        return this.requests.stream();
     }
 
-    public void run() {
-        this.logs = new ArrayList<>();
+    public void simulate() {
         Random random = new Random();
         Request request = null;
         int probability = 0;
@@ -55,12 +61,15 @@ public class Session {
                 break;
             }
             request.fire();
-            JsonObject logRequest = request.toJsonObject();
-            this.logs.add(logRequest);
+            this.requests.add(request);
             probability = random.nextInt(100);
         }
     }
 
+    public void execute() {
+        this.requests.forEach(Request::fire);
+    }
+
     public Request pickNextRequest(Request currentRequest) {
         if (currentRequest == null) {
             if (this.roots == null || this.roots.isEmpty()) {
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 f82b4cc..ea72f2c 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/requestssimulator/Simulator.java
@@ -5,6 +5,7 @@
  */
 package br.ufrgs.inf.prosoft.requestssimulator;
 
+import br.ufrgs.inf.prosoft.requestssimulator.requests.Request;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
@@ -16,6 +17,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.stream.Collectors;
 
 /**
  *
@@ -39,22 +41,22 @@ public class Simulator {
 
     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<>();
+        List<Profile> profiles = new ArrayList<>();
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
-                writeLog(logPath, logs);
+                writeLog(logPath, profiles);
             }
         });
         for (int i = 0; i < users; i++) {
+            Profile clone = profile.clone();
+            profiles.add(clone);
             Thread thread = new Thread(() -> {
-                profile.run(time);
-                List<List<JsonObject>> profileLogs = profile.getLogs();
-                logs.add(profileLogs);
+                clone.simulate(time);
             });
             threads.add(thread);
-            thread.start();
         }
+        threads.forEach(Thread::start);
         threads.forEach(thread -> {
             try {
                 thread.join();
@@ -62,12 +64,20 @@ public class Simulator {
                 LOGGER.log(Level.SEVERE, null, ex);
             }
         });
-        writeLog(logPath, logs);
     }
 
-    private static void writeLog(String logPath, List<List<List<JsonObject>>> logs) {
+    private static void writeLog(String logPath, List<Profile> profiles) {
         if (logPath != null) {
             LOGGER.log(Level.INFO, "writing log");
+            List<List<List<JsonObject>>> logs = profiles.stream()
+                    .map(profile
+                            -> profile.sessions()
+                            .map(session
+                                    -> session.requests()
+                                    .map(Request::toJsonObject)
+                                    .collect(Collectors.toList()))
+                            .collect(Collectors.toList()))
+                    .collect(Collectors.toList());
             Gson gson = new GsonBuilder().setPrettyPrinting().create();
             try (Writer writer = new FileWriter(logPath)) {
                 gson.toJson(logs, writer);