Like the title states I am trying to benchmark a cycle and obtain cpu load and memory consumed. I have tried getProcessCpuLoad() with not much success. Note that I have tried it out with a thread sleep of 3 seconds before and after the cycle. With the memory tried the getMemoryPoolMXBeans, but not much success. Is there a Java library that can help me achieve this or a tool.
What can I do?
I don't have the latest version of the code with me, here is the one of the previous versions it is missing the sleep thread, though probably the entire code is wrong:
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.List;
import com.sun.management.OperatingSystemMXBean;
class HelloWorld {
  public static void main(String[] args) {
    System.out.println("result of getMemoryPoolMXBeans ");
    List < MemoryPoolMXBean > mpmxList = ManagementFactory.getMemoryPoolMXBeans();
    long BeforememoryUse = 0;
    for (MemoryPoolMXBean pl: mpmxList) {
      MemoryUsage mu = pl.getPeakUsage();
      BeforememoryUse += mu.getUsed();
    }
    //before 
    //  System.out.println(memoryUse); 
    long acc = 0;
    OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
      OperatingSystemMXBean.class);
    // What % CPU load this current JVM is taking, from 0.0-1.0 
    double x = osBean.getProcessCpuLoad();
    for (int i = 0; i < 100000000; i++) {
      acc++;
    }
    System.out.println(x + "\n");
    System.out.println(osBean.getProcessCpuLoad() + "\n");
    x = osBean.getProcessCpuLoad() - x;
    System.out.println("Cpu load for first statement= " + x);
    //after 
    long AftermemoryUse = 0;
    for (MemoryPoolMXBean pl: mpmxList) {
      MemoryUsage mu = pl.getPeakUsage();
      AftermemoryUse += mu.getUsed();
    }
    System.out.println("Memory used by the first statement" + (AftermemoryUse - BeforememoryUse));
    //second statement 
    // What % CPU load this current JVM is taking, from 0.0-1.0 
    BeforememoryUse = 0;
    for (MemoryPoolMXBean pl: mpmxList) {
      MemoryUsage mu = pl.getPeakUsage();
      BeforememoryUse += mu.getUsed();
    }
    //before 
    //  System.out.println(memoryUse); 
    acc = 0;
    x = osBean.getProcessCpuLoad();
    for (int i = 0; i < 10000; i++) {
      acc++;
    }
    //after 
    x = osBean.getProcessCpuLoad() - x;
    AftermemoryUse = 0;
    for (MemoryPoolMXBean pl: mpmxList) {
      MemoryUsage mu = pl.getPeakUsage();
      AftermemoryUse += mu.getUsed();
    }
    System.out.println("Cpu load for second statement= " + x);
    System.out.println("Memory used by the second statement" + (AftermemoryUse - BeforememoryUse));
  }
 
    