vladislavaSim преди 1 година
родител
ревизия
248f328a00
променени са 1 файла, в които са добавени 33 реда и са изтрити 1 реда
  1. 33 1
      HW14/main.js

+ 33 - 1
HW14/main.js

@@ -57,4 +57,36 @@ btn.addEventListener('click', async () => {
         changeLight(yellow)
         await delay(1000)
         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))
+