Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ex Libris Alma - Parallel API Calls
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);
@jweisman

This comment has been minimized.

Copy link
Owner

jweisman commented Dec 4, 2018

After downloading the parallel.js file, install the Alma Node library:

npm i almarestapi-lib

To run add your API key to the environment, or on the command line:

ALMA_API_KEY=l7xxxxxxxxxxxxx node parallel.js 2> error.txt 1> output.txt

The output can be opened in Excel to create a graph.

results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment