Fix require times.



  • I've been doing a lot of work to bring down my CPU usage, but I can't seem to catch a break from the time it takes to load up my script.  I've been getting a wide range of CPU limit notifications (anywhere from 0 to thousands of notifications per hour).

    After putting in a ton of time optimizing my script, and working with my profiler, I don't know that there is anything I can do.

    Below is my current main.js.

    require('perf');
    require('creep');
    require('source');
    require('spawns');
    require('structures');
    require('room-position');
    var profiler = require('profiler');

    profiler.enable();
    module.exports.loop = function() {
    profiler.wrap(function() {
    var start = Game.getUsedCpu();
    var index = 0;
    for (var roomName in Game.rooms) {
    index++;
    if (index === 1 || Game.cpuLimit > 60) {
    Game.rooms[roomName].work();
    }
    }
    console.log(Game.getUsedCpu() - start, start);
    });
    }
    Here is the output that I then see in the console (times on the left are the times that my main loop ran in, time on the right is the start variable).  There are a lot more lines than this, but that's all that fits in the console.
     
    [3:01:31 AM] 14.490700999999994 25.062711
    [3:01:33 AM] 8.401745000000002 17.992238
    [3:01:36 AM] 17.664528999999998 30.933001
    [3:01:38 AM] 18.328616000000004 33.14149
    [3:01:39 AM] 5.719020000000002 10.325317
    [3:01:43 AM] 15.669170000000001 47.227466
    [3:01:44 AM] 9.323974 17.043523
    [3:01:48 AM] 5.914033999999999 9.580664
    [3:01:54 AM] 5.575247000000001 17.763152
    [3:01:55 AM] 8.342134000000001 18.901647
    [3:01:59 AM] 6.6542780000000015 21.621283
    [3:02:01 AM] 8.344047 17.63203
    [3:02:04 AM] 13.660488 25.990941
    [3:02:07 AM] 8.154161000000002 18.013329
    [3:02:09 AM] 13.498067000000006 43.098558
    [3:02:10 AM] 25.970536000000003 17.839734
    [3:02:14 AM] 8.042030999999998 23.107959
    [3:02:16 AM] 7.167925 9.173901
    [3:02:21 AM] 17.976727999999994 27.388998
    [3:02:22 AM] 4.847477999999999 9.226024
    [3:02:25 AM] 13.988324000000002 31.106472
    [3:02:28 AM] 13.301356000000002 19.72084
    [3:02:29 AM] 13.865193999999999 17.980405
    [3:02:32 AM] 12.265623999999999 29.939694
    [3:02:35 AM] 9.779004999999998 55.085228
    [3:02:38 AM] 5.709491 26.645434
    [3:02:39 AM] 5.808555000000002 16.596426
    [3:02:43 AM] 8.617096 18.15661
    [3:02:46 AM] 13.436439 23.091488
    [3:02:48 AM] 20.112817 34.604358
    [3:02:54 AM] 10.348903 17.545147
    [3:02:56 AM] 12.349533999999998 19.725963
    [3:02:59 AM] 12.894641999999997 20.269176
    [3:03:01 AM] 8.228573 17.514257
    [3:03:03 AM] 6.326778999999998 16.346291
    [3:03:06 AM] 13.272701999999995 43.243217
    [3:03:08 AM] 10.555196000000002 17.013294
    [3:03:11 AM] 5.294263000000001 17.063165
    [3:03:12 AM] 4.918824000000001 16.808634
    [3:03:16 AM] 14.891358000000002 10.968451
    [3:03:17 AM] 7.155049000000002 17.082047
    [3:03:19 AM] 7.033011000000002 17.609693
    [3:03:23 AM] 22.316795 31.506726
    [3:03:27 AM] 10.088149999999999 34.022269
    [3:03:28 AM] 11.699035 10.553996
    [3:03:32 AM] 8.241211 17.3698
    [3:03:34 AM] 5.379438000000004 18.126723
    [3:03:36 AM] 8.234374999999996 26.21249
    [3:03:38 AM] 6.508486999999999 16.800408

  • SUN

    If you type `console.log('nocache')` right before the `module.exports.loop` line,
    you'll see, that cache is not permamemt.
    It means, that the code outside of `module.exports.loop` method (including all your `require()` calls) is executed occasionly.
    As far as each `require()` call is *CPU-heavy*, you do have that spikes at start sometimes.

    In the presence of cpu spikes, you may want to get know the real 'used cpu' value, that can be compared to your 'cpu limit'. So, you may calculate script's execution time average, let us say: for the last 30 ticks. At least it works fine for me.