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

[WIP] Convert to TypeScript #58

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,5 +1,3 @@
// @flow

export { Metric } from "./metric";
export { Metrics } from "./metrics";
export { measure } from "./measure";
@@ -1,31 +1,28 @@
// @flow
import path from "path";
// $FlowFixMe(perf_hooks-exists)
import { performance, PerformanceObserver } from "perf_hooks";
import { Progress, type Logger } from "./progress";
import { Metric, type Call } from "./metric";
import path from "path"; // $FlowFixMe(perf_hooks-exists)

// FIXME: Move to lib
type Option = {|
+step: number,
+progressLogger: Logger
|};
import { performance, PerformanceObserver } from "perf_hooks";
import { Progress, Logger } from "./progress";
import { Metric, Call } from "./metric"; // FIXME: Move to lib

type Option = {
readonly step: number;
readonly progressLogger: Logger;
};
type SetupFunction = (
timerify: typeof performance.timerify
) => any | Promise<any>;
type TestFunction = (arg: any) => void | Promise<void>;
type Queue = Array<{|
name: string,
setup: SetupFunction,
fn: TestFunction
|}>;

type Queue = Array<{
name: string;
setup: SetupFunction;
fn: TestFunction;
}>;
export const measure = (
target: string,
option: Option
): Promise<Array<Metric>> => {
const queue: Queue = [];

global.bench = (name: string, setup: SetupFunction, fn: TestFunction) => {
queue.push({
name,
@@ -36,21 +33,29 @@ export const measure = (

const run = async (queue: Queue): Promise<Array<Metric>> => {
const metrics: Array<Metric> = [];

for (let { name, fn, setup } of queue) {
const calls: Array<Call> = [];
const markedCalls: { [string]: Array<Call> } = {};

const markedCalls: {
[x: string]: Array<Call>;
} = {};
const observer = new PerformanceObserver(list => {
list.getEntries().forEach(entry => {
if (entry.entryType === "measure") {
calls.push({ duration: entry.duration });
calls.push({
duration: entry.duration
});
} else {
markedCalls[entry.name] = markedCalls[entry.name] || [];
markedCalls[entry.name].push({ duration: entry.duration });
markedCalls[entry.name].push({
duration: entry.duration
});
}
});
});
observer.observe({ entryTypes: ["function", "measure"] });
observer.observe({
entryTypes: ["function", "measure"]
});
const arg = setup(performance.timerify);

for (let i = 0; i < option.step; i++) {
@@ -63,7 +68,6 @@ export const measure = (
}

observer.disconnect();

metrics.push(
new Metric({
name,
@@ -74,15 +78,13 @@ export const measure = (
}

return metrics;
};
}; // $FlowFixMe(allow-dynamic-require)

// $FlowFixMe(allow-dynamic-require)
require(target);

const progress = new Progress(option.progressLogger);
progress.setTotal(queue.length * option.step);
progress.setTitle(path.relative(process.cwd(), target));
progress.tick(0);

return run(queue);
};
@@ -1,22 +1,22 @@
// @flow
import sumBy from "lodash/sumBy";
import meanBy from "lodash/meanBy";
import sortBy from "lodash/sortBy";

export type Call = {|
+duration: number
|};

export type MetricProps = {|
+name: string,
+calls: Array<Call>,
+markedCalls: { [name: string]: Array<Call> }
|};

export type Call = {
readonly duration: number;
};
export type MetricProps = {
readonly name: string;
readonly calls: Array<Call>;
readonly markedCalls: {
[name: string]: Array<Call>;
};
};
export class Metric {
+name: string;
+calls: Array<Call>;
+markedCalls: { [marker: string]: Array<Call> };
name: string;
calls: Array<Call>;
markedCalls: {
[marker: string]: Array<Call>;
};

constructor({ name, calls, markedCalls }: MetricProps) {
this.name = name;
@@ -39,34 +39,41 @@ export class Metric {
);
}

getMarked(mark: string): ?Array<Call> {
getMarked(mark: string): Array<Call> | undefined | null {
if (!this.markedCalls[mark]) {
return null;
}

return this.markedCalls[mark];
}

getMarkedCount(mark: string): number {
const marked = this.getMarked(mark);

if (!marked) {
return -1;
}

return marked.length;
}

getMarkedTotal(mark: string): number {
const marked = this.getMarked(mark);

if (!marked) {
return -1;
}

return sumBy(marked, "duration");
}

getMarkedMean(mark: string): number {
const marked = this.getMarked(mark);

if (!marked) {
return -1;
}

return meanBy(marked, "duration");
}
}
@@ -1,20 +1,30 @@
// @flow
import { Metric, type MetricProps } from "./metric";

import { Metric, MetricProps } from "./metric";
export class Metrics {
metrics: { [name: string]: Metric };
metrics: {
[name: string]: Metric;
};

static fromArray(metrics: Array<Metric>) {
const metricsMap = {};

for (let metric of metrics) {
metricsMap[metric.name] = JSON.parse(JSON.stringify(metric));
}

return new Metrics({ metrics: metricsMap });
return new Metrics({
metrics: metricsMap
});
}

constructor({ metrics }: { metrics: { [name: string]: MetricProps } }) {
constructor({
metrics
}: {
metrics: {
[name: string]: MetricProps;
};
}) {
this.metrics = {};

for (let name in metrics) {
this.metrics[name] = new Metric(metrics[name]);
}
@@ -29,7 +39,7 @@ export class Metrics {
return Object.values(this.metrics);
}

getByName(name: string): ?Metric {
getByName(name: string): Metric | undefined | null {
return this.metrics[name] || null;
}
}
@@ -1,10 +1,7 @@
// @flow

export interface Logger {
log: (title: string, tick: number, total: number, eta: number) => void;
tearDown: (spent: number) => void;
}

export class Progress {
logger: Logger;
current: number;

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.