package benchmarking;


import converters.JsonConverter;
import datamodel.PerformanceSelfMonitoring;
import mongo.MongoManager;

public class Monitoring {
    CpuMonitoring cpuMonitor = new CpuMonitoring();
    long startTime = -1, endTime = -1, totalTime = -1;
    private PerformanceSelfMonitoring perfSelf = new PerformanceSelfMonitoring();

    public void startMonitoring() {
        cpuMonitor.startMonitoring();
        startTime = System.currentTimeMillis();
    }

    public void stopMonitoring() {
        cpuMonitor.stopMonitoring();
        endTime = System.currentTimeMillis();
        totalTime = endTime - startTime;
    }

    public PerformanceSelfMonitoring getResults(long pid) {
        perfSelf.setCpuUsageResults(cpuMonitor.parseForPid(pid));
        perfSelf.setExecutionTime(totalTime);

        return perfSelf;
    }

    public void saveResultsInDb (long pid, MongoManager mm) {
        Thread writeRestuls = new Thread(new Runnable() {
            @Override
            public void run() {
                perfSelf.setCpuUsageResults(cpuMonitor.parseForPid(pid));
                perfSelf.setExecutionTime(totalTime);
                mm.pushJson(JsonConverter.objectToJsonString(perfSelf), "selfPerformance");            }
        });
        writeRestuls.start();
    }

}

@@ -258,90 +258,8 @@
  -1;
-%0A    private PerformanceSelfMonitoring perfSelf = new PerformanceSelfMonitoring();
 %0A%0A  
@@ -607,24 +607,102 @@
 long pid) %7B%0A
+        PerformanceSelfMonitoring perfSelf = new PerformanceSelfMonitoring();%0A
         perf
@@ -976,16 +976,16 @@
 verride%0A
-
         
@@ -1004,24 +1004,110 @@
 oid run() %7B%0A
+                PerformanceSelfMonitoring perfSelf = new PerformanceSelfMonitoring();%0A
             

Resolve concurency on the same object.