Last active
January 16, 2019 15:21
-
-
Save jweisman/3913049336fba6dc98cf6d4024631d4c to your computer and use it in GitHub Desktop.
Ex Libris Alma - Parallel API Calls
This file contains 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
const { PerformanceObserver, performance } = require('perf_hooks'); | |
const alma = require('almarestapi-lib'); | |
const records = 10000; | |
const limit = 25; | |
const url = '/users?view=brief'; | |
const arrayName = 'user'; | |
var offset = 0, total = 0, processed = 0; | |
async function main() { | |
console.log('Processed,Completed,Errors,Time'); | |
do { | |
var data = await alma.getp(url+`&limit=${limit}&offset=${offset}`); | |
total = data.total_record_count; | |
var i=0, e=0; | |
var t1 = performance.now(); | |
var actions = data[arrayName].map(async item => { | |
if (item.link) | |
return alma.getp(item.link+'?&view=brief').then(data => i++).catch(err => e++); | |
}); | |
await Promise.all(actions); | |
offset += actions.length; | |
processed += actions.length; | |
var t2 = performance.now(); | |
console.log([processed, i, e, t2-t1].join(',')); | |
if (offset >= total) offset = 0; // loop multiple times if necessary | |
} while (processed < records); | |
} | |
main() | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After downloading the parallel.js file, install the Alma Node library:
To run add your API key to the environment, or on the command line:
The output can be opened in Excel to create a graph.