Add timing information

This commit is contained in:
Kuba Orlik 2022-05-25 19:38:30 +02:00
parent 3334abb9d8
commit 84665ba267
2 changed files with 28 additions and 2 deletions

View File

@ -62,6 +62,7 @@ router.get("/", async (ctx) => {
<input type="submit" /> <input type="submit" />
</form> </form>
<code><pre id="output"></pre></code> <code><pre id="output"></pre></code>
<code><pre id="stdout"></pre></code>
</body> </body>
<script> <script>
async function sleep(time) { async function sleep(time) {
@ -79,6 +80,8 @@ router.get("/", async (ctx) => {
response = await (await fetch(\`/api/requests/\${id}\`)).json(); response = await (await fetch(\`/api/requests/\${id}\`)).json();
console.log(response); console.log(response);
output.innerHTML = JSON.stringify(response, null, " "); output.innerHTML = JSON.stringify(response, null, " ");
console.log(response.output);
stdout.innerHTML = response.output;
await sleep(1000); await sleep(1000);
} while (response.status !== "finished"); } while (response.status !== "finished");
} }

View File

@ -13,6 +13,11 @@ module.exports = class ScreenshotRequest {
this.status = "waiting"; this.status = "waiting";
this.output = ""; this.output = "";
this.images = []; this.images = [];
this.request_time = Date.now();
this.started_time = null;
this.finished_time = null;
this.processing_took = null;
this.waiting_took = null;
q.push(async () => { q.push(async () => {
return this.exec(); return this.exec();
}); });
@ -34,12 +39,30 @@ module.exports = class ScreenshotRequest {
domains: this.domains, domains: this.domains,
id: this.id, id: this.id,
status: this.status, status: this.status,
/* output: this.output, */ output: this.output,
files: await this.getImages(), files: await this.getImages(),
request_time: this.request_time,
started_time: this.started_time,
finished_time: this.finished_time,
processing_took: this.processing_took,
waiting_took: this.waiting_took,
elapsed_time_s: Math.round(
((this.status === "finished" ? this.finished_time : Date.now()) -
this.request_time) /
1000
),
}; };
} }
setFinished() {
this.status = "finished";
this.finished_time = Date.now();
this.processing_took = this.finished_time - this.started_time;
this.waiting_took = this.started_time - this.request_time;
}
async exec() { async exec() {
this.started_time = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.status = "running"; this.status = "running";
this.process = spawn( this.process = spawn(
@ -55,7 +78,7 @@ module.exports = class ScreenshotRequest {
{ cwd: process.cwd() } { cwd: process.cwd() }
); );
this.process.on("close", (exitCode) => { this.process.on("close", (exitCode) => {
this.status = "finished"; this.setFinished();
if (exitCode === 0) { if (exitCode === 0) {
resolve(); resolve();
} else { } else {