Skip to content

Instantly share code, notes, and snippets.

@jweisman
Last active January 16, 2019 15:21
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jweisman/3913049336fba6dc98cf6d4024631d4c to your computer and use it in GitHub Desktop.
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
Copy link
Author

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