package benchmarking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Date;

public class CpuMonitor {
    private boolean continueMonitoring = true;
    private StringBuilder commandOutput;
    private Date date;

    public void startMonitoring(){
        boolean firstAppearance = true;

        Thread cpuMonit = new Thread(new Runnable() {
            @Override
            public void run() {
                String line;
                Process process;
                commandOutput = new StringBuilder();

                String processPid = getPid();
                String[] cmd = {
                        "/bin/sh",
                        "-c",
                        " atop 1 -a | unbuffer -p awk '/" + processPid + "|nmap/ {print $1 \" \" $11}'"
                };

                try {
                    process = Runtime.getRuntime().exec(cmd);
                    Field pid = process.getClass().getDeclaredField("pid");
                    pid.setAccessible(true);
                    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

                    while ( continueMonitoring && (line = br.readLine()) != null) {
                        if (firstAppearance  && line.contains(processPid)) {
                            date = new Date();
                        }
                        commandOutput.append(line + "\n");
                    }
                    process.destroy();
                } catch (IOException e) {
                    System.err.println(e.getStackTrace());
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }

                //System.out.println("Command output: " + commandOutput.toString());
            }
        });
        cpuMonit.start();
    }

    public String parseForPid(long pid) {
        stopMonitoring();
        String cmdOut = commandOutput.toString();
        StringBuilder parsedResultsAfterPid = new StringBuilder();

        String[] outputLines = cmdOut.split(System.getProperty("line.separator"));
        for (String line : outputLines) {
            String[] processData = line.split(" ");
            try {
                long resultsPid = Long.parseLong(processData[0]);
                String cpuUsage = processData[1];
                if (resultsPid == pid) {
                    parsedResultsAfterPid.append(resultsPid + "," + cpuUsage + "\n");
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        return parsedResultsAfterPid.toString();
    }

    private static String getPid() {
        String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
        return processName.split("@")[0];
    }

    public void stopMonitoring() {
        continueMonitoring = false;
    }
}

@@ -16,16 +16,51 @@
 rking;%0A%0A
+import datamodel.CpuUsageResults;%0A%0A
 import j
@@ -193,20 +193,71 @@
 va.util.
-Date
+ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List
 ;%0A%0Apubli
@@ -1993,22 +1993,31 @@
  public 
-String
+CpuUsageResults
  parseFo
@@ -2168,32 +2168,103 @@
 tringBuilder();%0A
+        List%3CString%3E cpuPercentEverySecond = new ArrayList%3CString%3E();%0A%0A
 %0A        String%5B
@@ -2620,16 +2620,75 @@
         
+cpuPercentEverySecond.add(cpuUsage);%0A                    //
 parsedRe
@@ -2892,39 +2892,60 @@
 urn 
-parsedResultsAfterPid.toString(
+new CpuUsageResults(pid, date, cpuPercentEverySecond
 );%0A 

Use the datamodel for cpu usage.