const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));

async function trafficLight(timeRed, timeYellow, timeGreen){
    while (true){
        green.classList.remove('active');
        red.classList.add('active');
        await delay(timeRed);

        red.classList.remove('active');
        yellow.classList.add('active');
        await delay(timeYellow);

        yellow.classList.remove('active');
        green.classList.add('active');
        await delay(timeGreen);
    }
}
trafficLight(3000, 1000, 3000);

function domEventPromise(element, eventName) {
    return new Promise(function(resolve, reject) {
        element.addEventListener(eventName, (e) => resolve(e.target));
        
        element.removeEventListener(eventName, (e) => resolve(e.target));
    });
}

// domEventPromise(knopka, 'click')
//     .then( e => console.log('event click happens', e));

//для пешеходов
async function PedestrianTrafficLight(timeGreen, timeRed){
    while (true){
        red2.classList.remove('active');
        green2.classList.add('active');
        await delay(timeGreen);

        green2.classList.remove('active');
        red2.classList.add('active');
        await delay(timeRed);
    }
}

PedestrianTrafficLight(3500, 3500);

function clickBtn() {
    domEventPromise(knopka, 'click')
    .then( e => {
        document.querySelectorAll('div[class*=circle]').forEach((el) => {
            el.classList.remove('active');
        })
        green2.classList.add('active');
        red.classList.add('active');
    });
}

setTimeout(clickBtn(), 2000);

async function speedtest(getPromise, count,parallel = 1){
    let promiseArr = [];
    let date = new Date();
    for(let pr = 0; pr < count; pr++) {
        promiseArr[pr] = getPromise();
        await Promise.all(promiseArr);
    }
    console.log(promiseArr);

    let duration = new Date() - date;
    let querySpeed = count / duration;
    let queryDuration = duration / count;
    let parallelSpeed = parallel / duration;
    let parallelDuration = duration / parallel;

    return {
        duration: duration,
        querySpeed: querySpeed, //средняя скорость одного запроса
        queryDuration: queryDuration, //
        parallelSpeed: parallelSpeed,
        parallelDuration: parallelDuration
    }
}
    
speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result))
// {duration: 10000, 
// querySpeed: 0.001, //1 тысячная запроса за миллисекунду
// queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем 
// parallelSpeed: 0.01  // 100 запросов за 10000 миллисекунд
// parallelDuration: 100 // 100 запросов за 10000 миллисекунд
speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5)

