-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): use correct URI when defining bench in separate files
- Loading branch information
1 parent
ae68e9b
commit 8dd5c09
Showing
11 changed files
with
257 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/benchmark.js-plugin/src/__tests__/buildSuiteAdd.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Suite } from "benchmark"; | ||
import buildSuiteAdd from "../buildSuiteAdd"; | ||
|
||
const emptyBench = () => { | ||
return; | ||
}; | ||
|
||
describe("buildSuiteAdd", () => { | ||
let suite: Suite; | ||
let uriMap: Map<string, string>; | ||
|
||
beforeEach(() => { | ||
suite = new Suite(); | ||
uriMap = new Map<string, string>(); | ||
}); | ||
|
||
it("should register benchmark name with uriMap", () => { | ||
const add = buildSuiteAdd(suite, uriMap); | ||
add("test", emptyBench); | ||
expect(uriMap.get("test")).toBeDefined(); | ||
}); | ||
|
||
it("should register benchmark name with uriMap when suite name is defined", () => { | ||
suite.name = "suite"; | ||
const add = buildSuiteAdd(suite, uriMap); | ||
add("test", emptyBench); | ||
expect(uriMap.get("test")).toContain("suite"); | ||
}); | ||
|
||
it("should register benchmark name with uriMap when options.name is defined", () => { | ||
const add = buildSuiteAdd(suite, uriMap); | ||
add(emptyBench, { name: "test" }); | ||
expect(uriMap.get("test")).toBeDefined(); | ||
}); | ||
|
||
it("should call rawAdd with options object", () => { | ||
const rawAdd = jest.fn(); | ||
suite.add = rawAdd; | ||
const add = buildSuiteAdd(suite, uriMap); | ||
const options = { name: "test", delay: 100 }; | ||
add(options); | ||
expect(rawAdd).toHaveBeenCalledWith(options); | ||
}); | ||
|
||
it("should call rawAdd with function and options object", () => { | ||
const rawAdd = jest.fn(); | ||
suite.add = rawAdd; | ||
const add = buildSuiteAdd(suite, uriMap); | ||
const fn = emptyBench; | ||
const options = { name: "test", delay: 100 }; | ||
add("test", fn, options); | ||
expect(rawAdd).toHaveBeenCalledWith("test", fn, options); | ||
}); | ||
|
||
it("should call rawAdd with name and options object", () => { | ||
const rawAdd = jest.fn(); | ||
suite.add = rawAdd; | ||
const add = buildSuiteAdd(suite, uriMap); | ||
const options = { name: "test", delay: 100 }; | ||
add("test", options); | ||
expect(rawAdd).toHaveBeenCalledWith("test", options); | ||
}); | ||
|
||
it("should call rawAdd with function and undefined options", () => { | ||
const rawAdd = jest.fn(); | ||
suite.add = rawAdd; | ||
const add = buildSuiteAdd(suite, uriMap); | ||
const fn = emptyBench; | ||
add("test", fn); | ||
expect(rawAdd).toHaveBeenCalledWith("test", fn, undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Options, Suite } from "benchmark"; | ||
import { isFunction, isPlainObject } from "lodash"; | ||
import getCallingFile from "./getCallingFile"; | ||
|
||
function isOptions(options: unknown): options is Options { | ||
return isPlainObject(options); | ||
} | ||
|
||
export default function buildSuiteAdd( | ||
suite: Suite, | ||
uriMap: Map<string, string> | ||
) { | ||
const rawAdd = suite.add; | ||
const suiteName = suite.name; | ||
|
||
function registerBenchmarkName(name: string) { | ||
const callingFile = getCallingFile(3); | ||
let uri = callingFile; | ||
if (suiteName !== undefined) { | ||
uri += `::${suiteName}`; | ||
} | ||
uri += `::${name}`; | ||
uriMap.set(name, uri); | ||
} | ||
|
||
function add(options: Options): Suite; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function add(fn: Function, options?: Options): Suite; | ||
function add(name: string, options?: Options): Suite; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function add(name: string, fn: Function, options?: Options): Suite; | ||
function add(name: unknown, fn?: unknown, opts?: unknown) { | ||
// 1 argument: (options: Options) | ||
if (isOptions(name)) { | ||
if (name.name !== undefined) { | ||
registerBenchmarkName(name.name); | ||
} | ||
return rawAdd.bind(suite)(name); | ||
} | ||
|
||
// 2 arguments: (fn: Function, options?: Options) | ||
if (isFunction(name) && (isOptions(fn) || fn === undefined)) { | ||
if (fn !== undefined) { | ||
if (fn.name !== undefined) { | ||
registerBenchmarkName(fn.name); | ||
} | ||
} | ||
return rawAdd.bind(suite)(name, fn); | ||
} | ||
|
||
// 2 arguments: (name: string, options?: Options) | ||
if (typeof name === "string" && (isOptions(fn) || fn === undefined)) { | ||
registerBenchmarkName(name); | ||
return rawAdd.bind(suite)(name, fn); | ||
} | ||
|
||
// 3 arguments: (name: string, fn: Function, options?: Options) | ||
if ( | ||
typeof name === "string" && | ||
isFunction(fn) && | ||
(isOptions(opts) || opts === undefined) | ||
) { | ||
registerBenchmarkName(name); | ||
return rawAdd.bind(suite)(name, fn, opts); | ||
} | ||
} | ||
|
||
return add; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { findUpSync, Options as FindupOptions } from "find-up"; | ||
import path, { dirname } from "path"; | ||
import { get as getStackTrace } from "stack-trace"; | ||
|
||
function getGitDir(path: string): string | undefined { | ||
const dotGitPath = findUpSync(".git", { | ||
cwd: path, | ||
type: "directory", | ||
} as FindupOptions); | ||
return dotGitPath ? dirname(dotGitPath) : undefined; | ||
} | ||
|
||
export default function getCallingFile(depth: number): string { | ||
const stack = getStackTrace(); | ||
const callingFile = stack[depth].getFileName(); | ||
const gitDir = getGitDir(callingFile); | ||
if (gitDir === undefined) { | ||
throw new Error("Could not find a git repository"); | ||
} | ||
return path.relative(gitDir, callingFile); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { WithCodSpeedSuite } from ".."; | ||
|
||
export function registerBenchmarks(suite: WithCodSpeedSuite) { | ||
suite.add( | ||
"RegExp", | ||
function () { | ||
/o/.test("Hello World!"); | ||
}, | ||
{ maxTime: 0.1 } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Bench } from "tinybench"; | ||
|
||
export function registerBenchmarks(bench: Bench) { | ||
bench.add("RegExp", function () { | ||
/o/.test("Hello World!"); | ||
}); | ||
} |
Oops, something went wrong.