|
@@ -2,17 +2,19 @@ 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 <= parallel; i++) {
|
|
|
- for (let i = 1; i <= count; i++) {
|
|
|
- await getPromise();
|
|
|
+ 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();
|
|
|
- let parallelDuration = parallel * count;
|
|
|
- let duration = t1 - t0;
|
|
|
- let parallelSpeed = parallelDuration / duration;
|
|
|
- let queryDuration = duration / count;
|
|
|
- let querySpeed = count / duration;
|
|
|
+ const parallelDuration = parallel * count;
|
|
|
+ const duration = t1 - t0;
|
|
|
+ const parallelSpeed = parallelDuration / duration;
|
|
|
+ const queryDuration = duration / count;
|
|
|
+ const querySpeed = count / duration;
|
|
|
return {
|
|
|
duration,
|
|
|
querySpeed,
|
|
@@ -22,4 +24,5 @@ const speedtest = async (getPromise, count, parallel= 1) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result));
|
|
|
+speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result));
|
|
|
+speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 2, 2);
|