Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running Benchmark in Browser #14

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions src/etc.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import hrtime from "browser-process-hrtime";
import { EventEmitter } from "events";
import * as mathjs from "mathjs";
import { FOOTER, HEADER } from "./etc";
import { JestReporter, KarmaReporter, MochaReporter } from "./reporters";

export { JestReporter, KarmaReporter, MochaReporter };
import { FOOTER, HEADER } from "./reporters/utils";

export declare interface BenchmarkEventEmitter {
emit(event: "record", description: Array<string>, measurement: Measurement): boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/jestReporter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { JestReporter } from "..";
import { JestReporter } from "../reporters/jest";

export = JestReporter;
2 changes: 1 addition & 1 deletion src/plugin/jestReporterSetup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { JestReporter } from "..";
import { JestReporter } from "../reporters/jest";

JestReporter.initializeKelonio();
2 changes: 1 addition & 1 deletion src/plugin/karmaReporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KarmaReporter } from "..";
import { KarmaReporter } from "../reporters/karma";

module.exports = {
"reporter:kelonio": ["type", KarmaReporter],
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/karmaReporterSetup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { KarmaReporter } from "..";
import { KarmaReporter } from "../reporters/karma";

KarmaReporter.initializeKelonio();
2 changes: 1 addition & 1 deletion src/plugin/mochaReporter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MochaReporter } from "..";
import { MochaReporter } from "../reporters/mocha";

export = MochaReporter;
149 changes: 0 additions & 149 deletions src/reporters.ts

This file was deleted.

111 changes: 111 additions & 0 deletions src/reporters/jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as fsModule from "fs";
import { Benchmark, benchmark } from '../index'
import { ExtensionLookup, handleExtraReports } from "./utils";

export const STATE_FILE = ".kelonio.state.json";

let fs: typeof fsModule;
let canUseFs = true;
try {
fs = require("fs");
} catch {
canUseFs = false;
}

export class BenchmarkFileState {
constructor() {
if (!canUseFs) {
throw new Error("Unable to access file system");
}
}

exists(): boolean {
return fs.existsSync(STATE_FILE);
}

read(): any {
return JSON.parse(fs.readFileSync(STATE_FILE, "utf-8"));
}

write(data: object): void {
fs.writeFileSync(STATE_FILE, JSON.stringify(data), "utf-8");
}

append(data: object): void {
let previousData;
try {
previousData = this.read();
} catch {
previousData = {};
}
this.write({ ...previousData, ...data });
}

delete(): void {
try {
fs.unlinkSync(STATE_FILE);
} catch {}
}
}

interface JestReporterOptions {
keepStateAtStart?: boolean;
keepStateAtEnd?: boolean;
printReportAtEnd?: boolean;
extensions?: Array<ExtensionLookup>;
}

export class JestReporter implements jest.Reporter {
options: JestReporterOptions = {
keepStateAtStart: false,
keepStateAtEnd: false,
printReportAtEnd: true,
};
constructor(testData?: any, options?: JestReporterOptions) {
if (options) {
this.options = { ...this.options, ...options };
}
}
static initializeKelonio(): void {
const state = new BenchmarkFileState();
benchmark.events.on("record", (description, measurement) => {
const b = new Benchmark();
if (state.exists()) {
b.data = state.read();
}
b.incorporate(description, measurement);
state.write(b.data);
});
}

onRunStart(): void {
const state = new BenchmarkFileState();
if (this.options.keepStateAtStart) {
state.append({});
} else {
state.write({});
}
}

onRunComplete(): void {
const state = new BenchmarkFileState();
if (!state.exists()) {
throw new Error(
"The Kelonio reporter for Jest requires benchmark serialization." +
" Make sure to call `JestReporter.initializeKelonio()`."
);
}

const b = new Benchmark();
b.data = state.read();

if (this.options.printReportAtEnd) {
console.log(`\n${b.report()}`);
handleExtraReports(this.options.extensions, b, console.log);
}

if (!this.options.keepStateAtEnd) {
state.delete();
}
}
}
Loading