package benchmarking;

import datamodel.CpuUsageResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CpuMonitoring implements MonitoringInterface {
    private final static Logger logger = LoggerFactory.getLogger(CpuMonitoring.class);

    private Calendar startTimeOfMonitoring;
    private Calendar endTimeOfMonitoring;

    @Override
    public void startMonitoring() {
        startTimeOfMonitoring = Calendar.getInstance();
    }

    @Override
    public void stopMonitoring() {
        endTimeOfMonitoring = Calendar.getInstance();
        endTimeOfMonitoring.add(Calendar.SECOND, 1);
    }

    public CpuUsageResults parseForPid(long pid) {
        BufferedReader br = null;
        String line;
        StringBuilder nmapMonitoring = new StringBuilder();
        List<String> cpuUsageResults = new ArrayList<String>();
        try {
            br = new BufferedReader(new FileReader("/tmp/everySecondMonitoring.txt"));


            while (true) {
                if ((line = br.readLine()) == null) {
                    Thread.sleep(100);
                    continue;
                }
                String[] splitedLine = line.split("\\s+");
                if ((splitedLine.length > 23) && (splitedLine[0].trim().length() > 18)) {
                    String[] dateAndTime = splitedLine[0].split("_");
                    String date = dateAndTime[0];
                    String time = dateAndTime[1];

                    if (date.trim().length() == 0 || time.trim().length() == 0) {
                        continue;
                    }

                    String[] dateSplit = date.split("/");
                    String[] timeSplit = time.split(":");

                    Calendar timeFromLog = Calendar.getInstance();

                    timeFromLog.set(Calendar.SECOND, Integer.parseInt(timeSplit[2]));
                    timeFromLog.set(Calendar.MINUTE, Integer.parseInt(timeSplit[1]));
                    timeFromLog.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSplit[0]));
                    timeFromLog.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateSplit[2]));
                    timeFromLog.set(Calendar.MONTH, Integer.parseInt(dateSplit[1]) - 1);
                    timeFromLog.set(Calendar.YEAR, Integer.parseInt(dateSplit[0]));

                    if (timeFromLog.after(startTimeOfMonitoring) && timeFromLog.before(endTimeOfMonitoring)) {
                        if ((splitedLine.length > 1) && isNumeric(splitedLine[1].trim())) {
                            long resultsPid = Long.parseLong(splitedLine[1]);
                            if (resultsPid == pid) {
                                cpuUsageResults.add(splitedLine[2]);
                            }
                        }
                    }

                    // it is useless to look after the time the monitoring was stoped
                    if (timeFromLog.after(endTimeOfMonitoring)) {
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage(), e);
        } catch (ArrayIndexOutOfBoundsException e) {
            logger.error(e.getMessage(), e);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return new CpuUsageResults(pid, startTimeOfMonitoring, cpuUsageResults);
    }

    private boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
}

@@ -1066,16 +1066,149 @@
   try %7B%0A
+            // wait for 1 second before reading the file so that all data for PID is written in file%0A            Thread.sleep(1000);%0A
         
@@ -1287,17 +1287,16 @@
 xt%22));%0A%0A
-%0A
         
@@ -1309,36 +1309,8 @@
 ile 
-(true) %7B%0A                if 
 ((li
@@ -1344,95 +1344,8 @@
 ) %7B%0A
-                    Thread.sleep(100);%0A                    continue;%0A                %7D%0A
     
@@ -3646,26 +3646,38 @@
     
-e.printStackTrace(
+logger.error(e.getMessage(), e
 );%0A 

Add 1 second delay before reading the file, this allows atop to write all the CPU usage for the proecess in the log file. Remove the mechanism that was used before for reading all data related to CPU usage for a PID because it was CPU intensive.