List of usage examples for java.lang.management ThreadMXBean getCurrentThreadCpuTime
public long getCurrentThreadCpuTime();
From source file:eu.planets_project.tb.gui.backing.exp.ExpTypeMigrate.java
public static void main(String args[]) { Properties p = System.getProperties(); ByteArrayOutputStream byos = new ByteArrayOutputStream(); try {/*from w w w . ja v a2s. c o m*/ p.storeToXML(byos, "Automatically generated for PLANETS Service ", "UTF-8"); String res = byos.toString("UTF-8"); System.out.println(res); } catch (IOException e) { e.printStackTrace(); } // This. List<String> pl = new ArrayList<String>(); for (Object key : p.keySet()) { pl.add((String) key); } Collections.sort(pl); // for (String key : pl) { System.out.println(key + " = " + p.getProperty(key)); } /* * http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html#getCurrentThreadCpuTime() * * http://www.java-forums.org/new-java/5303-how-determine-cpu-usage-using-java.html * */ ThreadMXBean TMB = ManagementFactory.getThreadMXBean(); int mscale = 1000000; long time = 0, time2 = 0; long cput = 0, cput2 = 0; double cpuperc = -1; //Begin loop. for (int i = 0; i < 10; i++) { if (TMB.isThreadCpuTimeSupported()) { if (!TMB.isThreadCpuTimeEnabled()) { TMB.setThreadCpuTimeEnabled(true); } // if(new Date().getTime() * mscale - time > 1000000000) //Reset once per second // { System.out.println("Resetting..."); time = System.currentTimeMillis() * mscale; cput = TMB.getCurrentThreadCpuTime(); // cput = TMB.getCurrentThreadUserTime(); // } } //Do cpu intensive stuff for (int k = 0; k < 10; k++) { for (int j = 0; j < 100000; j++) { double a = Math.pow(i, j); double b = a / j + Math.random(); a = b * Math.random(); b = a * Math.random(); } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } if (TMB.isThreadCpuTimeSupported()) { // if(new Date().getTime() * mscale - time != 0) { cput2 = TMB.getCurrentThreadCpuTime(); System.out.println("cpu: " + (cput2 - cput) / (1000.0 * mscale)); // cput2 = TMB.getCurrentThreadUserTime(); time2 = System.currentTimeMillis() * mscale; System.out.println("time: " + (time2 - time) / (1000.0 * mscale)); cpuperc = 100.0 * (cput2 - cput) / (double) (time2 - time); System.out.println("cpu perc = " + cpuperc); // } } //End Loop } System.out.println("Done."); }
From source file:ThreadTimer.java
public static long getCurrentThreadTime() { ThreadMXBean tb = ManagementFactory.getThreadMXBean(); return tb.getCurrentThreadCpuTime(); }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeDecode.java
private static long decodeLikeABoss(final byte[] encoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexDecoder().decodeLikeABoss(encoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeDecode.java
private static long decodeLikeAnEngineer(final byte[] encoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexDecoder().decodeLikeAnEngineer(encoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeEncode.java
private static long encodeLikeABoss(final byte[] decoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexEncoder().encodeLikeABoss(decoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeEncode.java
private static long encodeLikeAnEngineer(final byte[] decoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexEncoder().encodeLikeAnEngineer(decoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
From source file:com.espertech.esper.regression.client.MyMetricFunctions.java
public static boolean takeCPUTime(long nanoSecTarget) { if (nanoSecTarget < 100) { throw new RuntimeException("CPU time wait nsec less then zero, was " + nanoSecTarget); }//from ww w . ja v a 2 s . co m ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); if (!mbean.isThreadCpuTimeEnabled()) { throw new RuntimeException("ThreadMXBean CPU time reporting not enabled"); } long before = mbean.getCurrentThreadCpuTime(); while (true) { long after = mbean.getCurrentThreadCpuTime(); long spent = after - before; if (spent > nanoSecTarget) { break; } } return true; }
From source file:com.rest4j.impl.UtilTest.java
@Test public void testClone() { Tree test = new Tree(); test.name = StringUtils.repeat("TEST", 10000); test.left = new Tree(); test.right = test.left;/* w w w .j a v a 2 s .c om*/ ThreadMXBean threadMx = ManagementFactory.getThreadMXBean(); long start = threadMx.getCurrentThreadCpuTime(); int N = 10000; for (int i = 0; i < N; i++) test = Util.deepClone(test); System.out.println("cloned in " + (threadMx.getCurrentThreadCpuTime() - start) / N + "ns"); assertSame(test.left, test.right); }
From source file:com.thoughtworks.go.server.service.builders.KillAllChildProcessTaskBuilderTest.java
public long getSystemTime() { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); return bean.isCurrentThreadCpuTimeSupported() ? (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;/*from w ww. j a va 2s.co m*/ }
From source file:com.twosigma.beakerx.kernel.magic.command.functionality.TimeMagicCommand.java
public MagicCommandOutput time(String codeToExecute, Message message, int executionCount, boolean showResult) { CompletableFuture<TimeMeasureData> compileTime = new CompletableFuture<>(); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); long currentThreadId = Thread.currentThread().getId(); Long startWallTime = System.nanoTime(); Long startCpuTotalTime = threadMXBean.getCurrentThreadCpuTime(); Long startUserTime = threadMXBean.getCurrentThreadUserTime(); SimpleEvaluationObject simpleEvaluationObject = createSimpleEvaluationObject(codeToExecute, kernel, message, executionCount);//from w w w . java2 s . co m if (!showResult) { simpleEvaluationObject.noResult(); } TryResult either = kernel.executeCode(codeToExecute, simpleEvaluationObject); Long endWallTime = System.nanoTime(); Long endCpuTotalTime = threadMXBean.getThreadCpuTime(currentThreadId); Long endUserTime = threadMXBean.getThreadUserTime(currentThreadId); compileTime.complete(new TimeMeasureData(endCpuTotalTime - startCpuTotalTime, endUserTime - startUserTime, endWallTime - startWallTime)); String messageInfo = "CPU times: user %s, sys: %s, total: %s \nWall Time: %s\n"; try { TimeMeasureData timeMeasuredData = compileTime.get(); return new MagicCommandOutput(MagicCommandOutput.Status.OK, String.format(messageInfo, format(timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime() - timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime()), format(timeMeasuredData.getWallTime())), either, simpleEvaluationObject); } catch (InterruptedException | ExecutionException e) { return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem during measuring time for your statement."); } }