Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active November 25, 2019 15:53
Show Gist options
  • Select an option

  • Save pcon/7452472 to your computer and use it in GitHub Desktop.

Select an option

Save pcon/7452472 to your computer and use it in GitHub Desktop.

Revisions

  1. pcon revised this gist Jun 10, 2014. 1 changed file with 31 additions and 29 deletions.
    60 changes: 31 additions & 29 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -5,33 +5,35 @@
    *
    */
    public static void waitAtLeastASecond() {
    Integer msPerS = 1000;
    Datetime start = Datetime.now();
    Datetime current = Datetime.now();

    // No sleep available so this ugliness
    Integer counter = 0;
    while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {
    // This code takes about 250ms or more on na3
    Long t1 = System.currentTimeMillis();
    String bigRandomString = '';
    for (Integer i = 0; i < 2000; i++) {
    bigRandomString += Crypto.getRandomLong();
    }
    for (Integer i = 0; i < 50; i++) {
    Blob cryptoKey = Crypto.generateAesKey(256);
    Blob data = Blob.valueOf(bigRandomString);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
    }
    Long t2 = System.currentTimeMillis();

    System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms' +
    ', Count ' + counter +
    ', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements() +
    ', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());

    counter++;
    current = Datetime.now();
    }
    Integer msPerS = 1000;
    Datetime start = Datetime.now();
    Datetime current = Datetime.now();

    // No sleep available so this ugliness
    Integer counter = 0;
    while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {
    // This code takes about 250ms or more on na3
    Long t1 = System.currentTimeMillis();
    String bigRandomString = '';

    for (Integer i = 0; i < 2000; i++) {
    bigRandomString += Crypto.getRandomLong();
    }

    for (Integer i = 0; i < 50; i++) {
    Blob cryptoKey = Crypto.generateAesKey(256);
    Blob data = Blob.valueOf(bigRandomString);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
    }
    Long t2 = System.currentTimeMillis();

    System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms' +
    ', Count ' + counter +
    ', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements() +
    ', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());

    counter++;
    current = Datetime.now();
    }
    }
  2. pcon revised this gist Jun 10, 2014. 1 changed file with 36 additions and 38 deletions.
    74 changes: 36 additions & 38 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,37 @@
    /**
    * Sleep at least a second
    *
    * Found at: http://boards.developerforce.com/t5/Apex-Code-Development/Best-way-to-delay-until-time-changes-in-a-test/td-p/389619
    *
    */
    public static void waitAtLeastASecond() {
    Integer msPerS = 1000;
    Datetime start = Datetime.now();
    Datetime current = Datetime.now();
    /**
    * Sleep at least a second
    *
    * Found at: http://boards.developerforce.com/t5/Apex-Code-Development/Best-way-to-delay-until-time-changes-in-a-test/td-p/389619
    *
    */
    public static void waitAtLeastASecond() {
    Integer msPerS = 1000;
    Datetime start = Datetime.now();
    Datetime current = Datetime.now();

    // No sleep available so this ugliness
    Integer counter = 0;
    while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {

    // This code takes about 250ms or more on na3
    Long t1 = System.currentTimeMillis();
    String bigRandomString = '';
    for (Integer i = 0; i < 2000; i++) {
    bigRandomString += Crypto.getRandomLong();
    }
    for (Integer i = 0; i < 50; i++) {
    Blob cryptoKey = Crypto.generateAesKey(256);
    Blob data = Blob.valueOf(bigRandomString);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
    }
    Long t2 = System.currentTimeMillis();

    System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms'
    + ', Count ' + counter
    + ', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements()
    + ', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime()
    );

    counter++;
    current = Datetime.now();
    }
    }
    // No sleep available so this ugliness
    Integer counter = 0;
    while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {
    // This code takes about 250ms or more on na3
    Long t1 = System.currentTimeMillis();
    String bigRandomString = '';
    for (Integer i = 0; i < 2000; i++) {
    bigRandomString += Crypto.getRandomLong();
    }
    for (Integer i = 0; i < 50; i++) {
    Blob cryptoKey = Crypto.generateAesKey(256);
    Blob data = Blob.valueOf(bigRandomString);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
    }
    Long t2 = System.currentTimeMillis();

    System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms' +
    ', Count ' + counter +
    ', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements() +
    ', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());

    counter++;
    current = Datetime.now();
    }
    }
  3. pcon created this gist Nov 13, 2013.
    39 changes: 39 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    /**
    * Sleep at least a second
    *
    * Found at: http://boards.developerforce.com/t5/Apex-Code-Development/Best-way-to-delay-until-time-changes-in-a-test/td-p/389619
    *
    */
    public static void waitAtLeastASecond() {
    Integer msPerS = 1000;
    Datetime start = Datetime.now();
    Datetime current = Datetime.now();

    // No sleep available so this ugliness
    Integer counter = 0;
    while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {

    // This code takes about 250ms or more on na3
    Long t1 = System.currentTimeMillis();
    String bigRandomString = '';
    for (Integer i = 0; i < 2000; i++) {
    bigRandomString += Crypto.getRandomLong();
    }
    for (Integer i = 0; i < 50; i++) {
    Blob cryptoKey = Crypto.generateAesKey(256);
    Blob data = Blob.valueOf(bigRandomString);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
    }
    Long t2 = System.currentTimeMillis();

    System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms'
    + ', Count ' + counter
    + ', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements()
    + ', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime()
    );

    counter++;
    current = Datetime.now();
    }
    }