Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1004536 // Test limitCPU (CPU limiting per thread)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (278L/3K/8K).

!752

p {
  thread "Burn 10%" {
    while licensed {
      limitCPU(10);
    }
  }
  
  while licensed {
    sleepSeconds(1);
    print(doze_sleepTime + " / " + doze_calcTime
      + " = " + formatDouble(((double) doze_calcTime)/doze_sleepTime,
        2));
  }
}

static int doze_granularity = 10;
static volatile long doze_calcingSince;
static volatile long doze_sleepTime, doze_calcTime;

svoid limitCPU(double percentage) {
  ping();
  if (doze_calcingSince == 0) {  // first time
    doze_calcingSince = now();
    ret;
  }
  long newCalcTime = now() - doze_calcingSince;
  if (newCalcTime >= doze_granularity) { // don't try to sleep too often
    doze_calcTime += newCalcTime;
    double ratio = percentage/100;
    double allowedCalcTime = doze_sleepTime*ratio/(1-ratio);
    long sleep = toLong(max(0, doze_calcTime-allowedCalcTime));
    if (sleep > 0) {
      sleep(sleep);
      doze_sleepTime += sleep;
    }
    doze_calcingSince = now();
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004536
Snippet name: Test limitCPU (CPU limiting per thread)
Eternal ID of this version: #1004536/1
Text MD5: 3c24b7f174a9d97b18cdb99b060015cd
Transpilation MD5: 4df863a29e4eef61edc9c63ec583500e
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-20 14:41:37
Source code size: 997 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 488 / 538
Referenced in: [show references]