Last active
April 4, 2018 23:48
-
-
Save brainysmurf/a5fec05743cc3fc85f7870d0c8a14b1e to your computer and use it in GitHub Desktop.
UrlFetchApp.fetchAll is definitely async.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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