ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

          So now you have:

          • The elapsed wall clock time (this period's time, minus last period's time) Call this X
          • The elapsed process cpu time (this period's time, minus last period's time) Call this Y
          • The number of CPUs. Call this C

          The percent utilization will be Y / (X x C) * 100

          import java.lang.management.*;
          import java.util.concurrent.*;

          osx = ManagementFactory.getOperatingSystemMXBean();
          cores = osx.getAvailableProcessors(); // Factorial to keep the process busy so we can see some actual activity
          factorial = { n -> int fact = 1; int i = 1; while(i <= n) { i++; fact *= i; } return fact; }
          long elapsedTime = -1, startTime = -1; long elapsedCpu = -1, startCpu = -1;;
          for(i in 0..20) { startTime = System.nanoTime(); startCpu = osx.getProcessCpuTime(); CountDownLatch latch = new CountDownLatch(cores); for(x in 1..cores) { Thread.startDaemon() { factorial(1000000); latch.countDown(); } } latch.await(); elapsedTime = System.nanoTime()-startTime; elapsedCpu = osx.getProcessCpuTime()-startCpu; percUsage = (elapsedCpu / (elapsedTime* cores)) *100; println "Percent Usage:$percUsage %"; }
          posted on 2015-04-06 15:44 ivaneeo 閱讀(1390) 評(píng)論(0)  編輯  收藏 所屬分類: java魔力
          主站蜘蛛池模板: 汤阴县| 赤城县| 大宁县| 江北区| 任丘市| 肇庆市| 开鲁县| 敦煌市| 武强县| 天峻县| 定南县| 大化| 金乡县| 张家口市| 马边| 吕梁市| 营口市| 滦平县| 葵青区| 汉中市| 大渡口区| 比如县| 汨罗市| 马鞍山市| 双鸭山市| 离岛区| 蒙山县| 彰武县| 安龙县| 淮南市| 天柱县| 松阳县| 普定县| 休宁县| 三穗县| 股票| 长海县| 宝丰县| 全州县| 南充市| 都兰县|