12345678910111213141516171819202122232425262728 |
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
- const speedtest = async (getPromise, count, parallel= 1) => {
- const t0 = performance.now();
- for (let i = 1; i <= count; i++) {
- let arr = [];
- for (let j = 1; j <= parallel; j++) {
- arr.push(getPromise());
- }
- await Promise.all(arr);
- }
- const t1 = performance.now();
- const parallelDuration = parallel * count;
- const duration = t1 - t0;
- const parallelSpeed = parallelDuration / duration;
- const queryDuration = duration / count;
- const querySpeed = count / duration;
- return {
- duration,
- querySpeed,
- queryDuration,
- parallelSpeed,
- parallelDuration
- }
- }
- speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result));
- speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 2, 2);
|