Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active April 4, 2018 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/a5fec05743cc3fc85f7870d0c8a14b1e to your computer and use it in GitHub Desktop.
Save brainysmurf/a5fec05743cc3fc85f7870d0c8a14b1e to your computer and use it in GitHub Desktop.
UrlFetchApp.fetchAll is definitely async.
/* The following code produces the following output in stackdriver log (newer logs higher):
Time for concurrent: 5.848 seconds.
Time for sleeping 10: 5.003 seconds.
Time for sleeping 2: 5.002 seconds.
Time for sleeping 9: 5.004 seconds.
Time for sleeping 8: 5.002 seconds.
Time for sleeping 5: 5.003 seconds.
Time for sleeping 3: 5.003 seconds.
Time for sleeping 1: 5.003 seconds.
Time for sleeping 7: 5.003 seconds.
Time for sleeping 6: 5.003 seconds.
Time for sleeping 4: 5.003 seconds.
Note that the sequence is not predictable, and if it were sync then it should have taken 50 seconds to complete.
*/
function timeit (name, ctx, func /*, args */) {
var now, then, args, ret;
now = Date.now();
args = Array.prototype.slice.call(arguments, 3);
var ret = func.apply(ctx, args);
then = Date.now();
console.log('Time for ' + name + ': ' + ((then - now) / 1000).toString() + ' seconds.');
return ret;
}
function sleep(seconds) {
timeit('sleeping', Utilities, Utilities.sleep, seconds * 1000);
}
// this code needs the Requests.gs modular library: https://github.com/classroomtechtools/modularLibraries.gs/blob/master/Requests/Requests.md
function concurrentProcessingTest() {
var response = timeit(
'concurrent',
Import.Requests,
Import.Requests.concurrentlyInDevMode,
function (c) {
c.add('sleep', '1');
c.add('sleep', '2');
c.add('sleep', '3');
c.add('sleep', '4');
c.add('sleep', '5');
c.add('sleep', '6');
c.add('sleep', '7');
c.add('sleep', '8');
c.add('sleep', '9');
c.add('sleep', '10');
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment