|
@@ -57,4 +57,36 @@ btn.addEventListener('click', async () => {
|
|
changeLight(yellow)
|
|
changeLight(yellow)
|
|
await delay(1000)
|
|
await delay(1000)
|
|
changeLight(red)
|
|
changeLight(red)
|
|
-});
|
|
|
|
|
|
+})
|
|
|
|
+
|
|
|
|
+async function speedtest(getPromise, count, parallel = 1) {
|
|
|
|
+ let time = performance.now();
|
|
|
|
+ let promisesArr = [];
|
|
|
|
+ let initParallel = parallel;
|
|
|
|
+ for (let i = 0; i < count; i++) {
|
|
|
|
+ promisesArr.push(getPromise);
|
|
|
|
+ parallel--;
|
|
|
|
+ await Promise.all(promisesArr);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ time = performance.now() - time;
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ duration: time,
|
|
|
|
+ querySpeed: count / time,
|
|
|
|
+ queryDuration: time / count,
|
|
|
|
+ parallelSpeed: (count / time) * initParallel,
|
|
|
|
+ parallelDuration: time / (count * initParallel),
|
|
|
|
+ };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+speedtest(() => delay(1000), 10, 10).then((res) =>console.log(res));
|
|
|
|
+speedtest(
|
|
|
|
+ () =>
|
|
|
|
+ fetch("http://swapi.dev/api/people/1").then((res) =>
|
|
|
|
+ res.json()
|
|
|
|
+ ),
|
|
|
|
+ 2,
|
|
|
|
+ 2
|
|
|
|
+).then((v)=>console.log(v))
|
|
|
|
+
|