Skip to content

Commit ccecf72

Browse files
committed
adding state
1 parent 27622ec commit ccecf72

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

JetStreamDriver.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class Driver {
341341

342342
if (isInBrowser) {
343343
globalThis.dispatchEvent(new CustomEvent("JetStreamDone", {
344-
detail: this.resultsObjectNew()
344+
detail: this.resultsObject()
345345
}));
346346
}
347347
}
@@ -528,28 +528,30 @@ class Driver {
528528
}
529529
}
530530

531-
resultsObject(newStyle = false) {
532-
if (newStyle)
533-
return this.resultsObjectNew();
534-
return this.resultsObjectOld();
535-
}
536-
537-
resultsObjectNew() {
531+
resultsObject(format = "run-benchmark") {
532+
if (format == "run-benchmark")
533+
return this.runBenchmarkResultsObject();
534+
if (format != "simple")
535+
throw Error(`Unknown result format '${format}'`);
538536
const results = {__proto__: null};
539537
for (const benchmark of this.benchmarks) {
540538
if (!benchmark.isDone)
541539
continue;
542-
results[benchmark.name] = {
543-
Score: benchmark.score,
544-
...benchmark.subScores(),
540+
if (!benchmark.isSuccess) {
541+
results[benchmark.name] = "FAILED";
542+
} else {
543+
results[benchmark.name] = {
544+
Score: benchmark.score,
545+
...benchmark.subScores(),
545546

546-
};
547+
};
548+
}
547549
}
548550
return results;
549551
}
550552

551553

552-
resultsObjectOld()
554+
runBenchmarkResultsObject()
553555
{
554556
let results = {};
555557
for (const benchmark of this.benchmarks) {
@@ -571,9 +573,9 @@ class Driver {
571573
return results;
572574
}
573575

574-
resultsJSON()
576+
resultsJSON(format = "run-benchmark")
575577
{
576-
return JSON.stringify(this.resultsObject());
578+
return JSON.stringify(this.resultsObject(format));
577579
}
578580

579581
dumpJSONResultsIfNeeded()
@@ -606,6 +608,15 @@ class Driver {
606608
}
607609
};
608610

611+
const BenchmarkState = Object.freeze({
612+
READY: "READY",
613+
SETUP: "SETUP",
614+
RUNNING: "RUNNING",
615+
FINALIZE: "FINALIZE",
616+
ERROR: "ERROR",
617+
DONE: "DONE"
618+
})
619+
609620
class Benchmark {
610621
constructor(plan)
611622
{
@@ -618,12 +629,15 @@ class Benchmark {
618629
this.scripts = null;
619630

620631
this._resourcesPromise = null;
621-
this._isDone = false;
632+
this._state = BenchmarkState.READY;
622633
}
623634

624635
get name() { return this.plan.name; }
625636

626-
get isDone() { return this._isDone; }
637+
get isDone() {
638+
return this._state == BenchmarkState.DONE || this._state == BenchmarkState.ERROR;
639+
}
640+
get isSuccess() { return this._state = BenchmarkState.DONE; }
627641

628642
get runnerCode() {
629643
return `
@@ -688,6 +702,7 @@ class Benchmark {
688702
async run() {
689703
if (this.isDone)
690704
throw new Error(`Cannot run Benchmark ${this.name} twice`);
705+
this._state = BenchmarkState.PREPARE;
691706
let code;
692707
if (isInBrowser)
693708
code = "";
@@ -796,16 +811,19 @@ class Benchmark {
796811

797812
let magicFrame;
798813
try {
814+
this._state = BenchmarkState.RUNNING;
799815
magicFrame = JetStream.runCode(code);
800816
} catch(e) {
817+
this._state = BenchmarkState.ERROR;
801818
console.log("Error in runCode: ", e);
802819
console.log(e.stack)
803820
throw e;
821+
} finally {
822+
this._state = BenchmarkState.FINALIZE;
804823
}
805824
const results = await promise;
806825

807826
this.endTime = performance.now();
808-
this._isDone = true;
809827

810828
if (RAMification) {
811829
const memoryFootprint = MemoryFootprint();
@@ -814,6 +832,8 @@ class Benchmark {
814832
}
815833

816834
this.processResults(results);
835+
this._state = BenchmarkState.DONE;
836+
817837
if (isInBrowser)
818838
magicFrame.contentDocument.close();
819839
else if (isD8)

tests/run.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async function pollResultsUntilDone(driver, resolve, reject) {
136136
return {
137137
done: globalThis.JetStream.isDone,
138138
errors: globalThis.JetStream.errors,
139-
resultsJSON: JSON.stringify(globalThis.JetStream.resultsObjectNew()),
139+
resultsJSON: JSON.stringify(globalThis.JetStream.resultsObject("simple")),
140140
};
141141
});
142142
if (errors.length) {

0 commit comments

Comments
 (0)