#!/usr/bin/env node

let rootFolder = process.argv[2];

const execSync = require('child_process').execSync;
const { exec } = require('child_process');
let url = "https://www.youtube.com";
let runLimit = 21;
var tests = [
     'tc qdisc replace dev enp0s31f6 root netem delay 0ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 50ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 100ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 150ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 200ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 250ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 300ms',
     'tc qdisc replace dev enp0s31f6 root netem delay 400ms',
     'tc qdisc replace dev enp0s31f6 root netem loss 1%',
     'tc qdisc replace dev enp0s31f6 root netem loss 2%',
     'tc qdisc replace dev enp0s31f6 root netem loss 3%',
     'tc qdisc replace dev enp0s31f6 root netem loss 4%',
     'tc qdisc replace dev enp0s31f6 root netem loss 5%',
     'tc qdisc replace dev enp0s31f6 root netem loss 7%',
     'tc qdisc replace dev enp0s31f6 root netem loss 10%',
];
var testsFolders = [
    '0-loss-0-delay',
    '50-ms',
    '100-ms',
    '150-ms',
    '200-ms',
    '250-ms',
    '300-ms',
    '400-ms',
    '1-loss',
    '2-loss',
    '3-loss',
    '4-loss',
    '5-loss',
    '7-loss',
    '10-loss',
];

function doTest(command, folder, quic = true) {
    let runs = 0;

    console.log(`Reset network parameters ${runs + 1}`);
    execSync('sudo tc qdisc replace dev enp0s31f6 root netem delay 0ms');
    execSync('sudo tc qdisc replace dev enp0s31f6 root netem loss 0%');
    console.log(`Set `+ command);
    execSync(`sudo `+command);
    if (quic == true) {
        execSync(`sudo mkdir ./${rootFolder}-quic-${folder}`);
        execSync(`sudo chmod 777 ./${rootFolder}-quic-${folder}`);
    } else {
        execSync(`sudo mkdir ./${rootFolder}-https-${folder}`);
        execSync(`sudo chmod 777 ./${rootFolder}-https-${folder}`);
    }

    do {
        console.log(`Starting performance test ${runs + 1}`);
        const file = "${folder}-${runs + 1}";
        try {
            if(quic == true) {
                execSync(`lighthouse ${url} --disable-setuid-sandbox --no-sandbox --only-categories=performance --emulated-form-factor=none --throttling-method=provided --output=json --output-path=./${rootFolder}-quic-${folder}/${runs + 1}-report.json --chrome-flags="--headless --enable-quic --origin-to-force-quic-on=${url}:433"`);
            } else {
                execSync(`lighthouse ${url} --disable-setuid-sandbox --no-sandbox --only-categories=performance --emulated-form-factor=none --throttling-method=provided --output=json --output-path=./${rootFolder}-https-${folder}/${runs + 1}-report.json --chrome-flags="--headless"`);
            }
        } catch
            (err) {
            console.log(`Performance test ${runs + 1} failed`);
            exit;
        }
        console.log(`Finished running performance test ${runs + 1}`);
        runs++;
    }
    while (runs < runLimit);

}

for(let i = 0; i < tests.length; i++) {
    // run quic tests
    doTest(tests[i], testsFolders[i]);
}

for(let i = 0; i < tests.length; i++) {
    // run https tests
    doTest(tests[i], testsFolders[i], false);
}
