2021-10-14 20:02:54 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-14 22:35:38 +05:30
|
|
|
import { Bench, BenchConfig, Submission, SubmissionProof } from "./types";
|
2021-10-14 20:39:39 +05:30
|
|
|
import ROUTES from "../api/v1/routes";
|
2021-10-14 21:25:19 +05:30
|
|
|
import genJsonPaylod from "../utils/genJsonPayload";
|
|
|
|
import isBlankString from "../utils/isBlankString";
|
2021-10-14 22:35:38 +05:30
|
|
|
import createError from "../components/error/";
|
2023-02-01 18:18:50 +05:30
|
|
|
import { get_bench_type } from "./prove";
|
2021-10-14 20:02:54 +05:30
|
|
|
|
|
|
|
export const index = () => {
|
2021-10-27 11:39:37 +05:30
|
|
|
const ADV = <HTMLButtonElement>document.getElementById("advance");
|
|
|
|
|
|
|
|
const toggleAdv = (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (ADV.innerText.includes("Show")) {
|
|
|
|
ADV.innerText = "Hide Advance Log";
|
|
|
|
document
|
|
|
|
.querySelectorAll(".advance-log")
|
|
|
|
.forEach((e: HTMLElement) => (e.style.display = "block"));
|
|
|
|
} else {
|
|
|
|
ADV.innerText = "Show Advance Log";
|
|
|
|
document
|
|
|
|
.querySelectorAll(".advance-log")
|
|
|
|
.forEach((e: HTMLElement) => (e.style.display = "none"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ADV.addEventListener("click", (e) => toggleAdv(e));
|
|
|
|
|
2021-10-14 20:39:39 +05:30
|
|
|
const initSession = async () => {
|
|
|
|
fetch(ROUTES.register);
|
|
|
|
};
|
|
|
|
|
2021-10-14 20:02:54 +05:30
|
|
|
const worker = new Worker("/bench.js");
|
2021-10-14 21:25:19 +05:30
|
|
|
const res: Array<Bench> = [];
|
2021-10-14 20:02:54 +05:30
|
|
|
const stats = document.getElementById("stats");
|
2021-10-14 21:25:19 +05:30
|
|
|
const CAMPAIGN_ID = window.location.pathname.split("/")[3];
|
|
|
|
let deviceName = "";
|
2021-10-14 20:02:54 +05:30
|
|
|
|
2021-10-14 21:25:19 +05:30
|
|
|
const addResult = (perf: Bench) => {
|
2021-10-14 20:02:54 +05:30
|
|
|
const row = document.createElement("tr");
|
|
|
|
row.className = "data";
|
|
|
|
const diff = document.createElement("td");
|
|
|
|
diff.innerHTML = perf.difficulty.toString();
|
|
|
|
const duration = document.createElement("td");
|
2021-10-14 21:25:19 +05:30
|
|
|
duration.innerHTML = perf.duration.toString();
|
2021-10-14 20:02:54 +05:30
|
|
|
|
|
|
|
row.appendChild(diff);
|
|
|
|
row.appendChild(duration);
|
|
|
|
|
|
|
|
stats.appendChild(row);
|
|
|
|
|
|
|
|
res.push(perf);
|
|
|
|
};
|
|
|
|
|
2021-10-14 21:25:19 +05:30
|
|
|
const submitBench = async () => {
|
2023-02-01 18:18:50 +05:30
|
|
|
const submission_type = await get_bench_type();
|
2021-10-14 21:25:19 +05:30
|
|
|
const payload: Submission = {
|
|
|
|
device_user_provided: deviceName,
|
|
|
|
threads: window.navigator.hardwareConcurrency,
|
|
|
|
device_software_recognised: window.navigator.userAgent,
|
|
|
|
benches: res,
|
2023-02-01 18:18:50 +05:30
|
|
|
submission_type,
|
2021-10-14 21:25:19 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const resp = await fetch(
|
|
|
|
ROUTES.submitBench(CAMPAIGN_ID),
|
|
|
|
genJsonPaylod(payload)
|
|
|
|
);
|
|
|
|
if (resp.status == 200) {
|
|
|
|
const data: SubmissionProof = await resp.json();
|
|
|
|
const element = document.createElement("div");
|
|
|
|
const token = document.createElement("b");
|
2021-10-27 11:39:37 +05:30
|
|
|
token.innerText = "Submissinon ID: ";
|
2021-10-14 21:25:19 +05:30
|
|
|
const tokenText = document.createTextNode(`${data.token}`);
|
|
|
|
|
|
|
|
const proof = document.createElement("b");
|
|
|
|
proof.innerText = "Proof: ";
|
|
|
|
const proofText = document.createTextNode(`${data.proof}`);
|
|
|
|
|
|
|
|
element.appendChild(token);
|
|
|
|
element.appendChild(tokenText);
|
|
|
|
element.appendChild(document.createElement("br"));
|
|
|
|
element.appendChild(proof);
|
|
|
|
element.appendChild(proofText);
|
|
|
|
document.getElementById("submission-proof").appendChild(element);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-14 20:02:54 +05:30
|
|
|
const addDeviceInfo = () => {
|
|
|
|
const INFO = {
|
|
|
|
threads: window.navigator.hardwareConcurrency,
|
|
|
|
oscup: window.navigator.userAgent,
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(res);
|
|
|
|
console.log(INFO);
|
|
|
|
|
|
|
|
const element = document.createElement("div");
|
2021-10-27 11:39:37 +05:30
|
|
|
const ua = document.createElement("span");
|
2021-10-14 20:02:54 +05:30
|
|
|
ua.innerText = "User Agent: ";
|
|
|
|
const os = document.createTextNode(`${INFO.oscup}`);
|
|
|
|
|
2021-10-27 11:39:37 +05:30
|
|
|
const threads = document.createElement("span");
|
2021-10-14 20:02:54 +05:30
|
|
|
threads.innerText = "Hardware concurrency: ";
|
|
|
|
const threadsText = document.createTextNode(`${INFO.threads}`);
|
|
|
|
|
|
|
|
element.appendChild(ua);
|
|
|
|
element.appendChild(os);
|
|
|
|
element.appendChild(document.createElement("br"));
|
|
|
|
element.appendChild(threads);
|
|
|
|
element.appendChild(threadsText);
|
|
|
|
|
|
|
|
document.getElementById("device-info").appendChild(element);
|
|
|
|
};
|
|
|
|
|
2021-10-14 21:25:19 +05:30
|
|
|
const finished = async () => {
|
|
|
|
await submitBench();
|
2021-10-14 20:02:54 +05:30
|
|
|
const s = document.getElementById("status");
|
|
|
|
s.innerHTML = "Benchmark finished";
|
|
|
|
};
|
|
|
|
|
2021-10-14 22:35:38 +05:30
|
|
|
const getConfig = async (): Promise<BenchConfig> => {
|
|
|
|
const resp = await fetch(ROUTES.fetchConfig(CAMPAIGN_ID));
|
|
|
|
if (resp.status != 200) {
|
|
|
|
const msg = "Something went wrong while fetching survey";
|
|
|
|
createError(msg);
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const config: BenchConfig = await resp.json();
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
2021-10-14 20:39:39 +05:30
|
|
|
const run = async (e: Event) => {
|
2021-10-14 20:02:54 +05:30
|
|
|
e.preventDefault();
|
2021-10-14 21:25:19 +05:30
|
|
|
const deveceNameElement = <HTMLInputElement>document.getElementById("name");
|
|
|
|
if (isBlankString(deveceNameElement.value, "Device Name", e)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceName = deveceNameElement.value;
|
|
|
|
|
2021-10-14 20:39:39 +05:30
|
|
|
await initSession();
|
2021-10-14 21:25:19 +05:30
|
|
|
|
2021-10-14 20:02:54 +05:30
|
|
|
document.getElementById("pre-bench").style.display = "none";
|
|
|
|
document.getElementById("bench").style.display = "flex";
|
|
|
|
|
2021-10-14 22:35:38 +05:30
|
|
|
const config = await getConfig();
|
|
|
|
|
|
|
|
const iterations = config.difficulties.length;
|
2021-10-14 20:02:54 +05:30
|
|
|
|
|
|
|
const counterElement = document.getElementById("counter");
|
|
|
|
counterElement.innerText = `${iterations} more to go`;
|
|
|
|
|
2021-10-14 21:25:19 +05:30
|
|
|
worker.onmessage = async (event: MessageEvent) => {
|
|
|
|
const data: Bench = event.data;
|
2021-10-14 20:02:54 +05:30
|
|
|
addResult(data);
|
|
|
|
if (res.length == iterations) {
|
2021-10-14 21:25:19 +05:30
|
|
|
await finished();
|
2021-10-14 20:02:54 +05:30
|
|
|
counterElement.innerText = "All Done!";
|
|
|
|
} else {
|
|
|
|
counterElement.innerText = `${iterations - res.length} more to go`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-14 22:35:38 +05:30
|
|
|
config.difficulties.forEach((difficulty_factor) =>
|
|
|
|
worker.postMessage(difficulty_factor)
|
|
|
|
);
|
2021-10-14 20:02:54 +05:30
|
|
|
|
|
|
|
addDeviceInfo();
|
|
|
|
};
|
|
|
|
|
2021-10-14 20:39:39 +05:30
|
|
|
document
|
|
|
|
.getElementById("start")
|
|
|
|
.addEventListener("click", async (e) => await run(e));
|
2021-10-14 20:02:54 +05:30
|
|
|
};
|