Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

[WIP] Convert to TypeScript #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

export { Metric } from "./metric";
export { Metrics } from "./metrics";
export { measure } from "./measure";
Expand Down
54 changes: 28 additions & 26 deletions packages/zapshot/src/measure.js → packages/zapshot/src/measure.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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++) {
Expand All @@ -63,7 +68,6 @@ export const measure = (
}

observer.disconnect();

metrics.push(
new Metric({
name,
Expand All @@ -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);
};
39 changes: 23 additions & 16 deletions packages/zapshot/src/metric.js → packages/zapshot/src/metric.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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]);
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
83 changes: 0 additions & 83 deletions packages/zapshot/src/report.js

This file was deleted.

Loading