Skip to content

Commit

Permalink
Refine
Browse files Browse the repository at this point in the history
  • Loading branch information
haven2world committed Dec 4, 2023
1 parent 49225bb commit d718c90
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 209 deletions.
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

0 comments on commit d718c90

Please sign in to comment.