2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-11 05:53:06 -04:00
|
|
|
import { now as opNow } from "../ops/timers.ts";
|
2020-07-10 22:38:15 -04:00
|
|
|
import { customInspect, inspect } from "./console.ts";
|
|
|
|
import { cloneValue, setFunctionName } from "./util.ts";
|
|
|
|
|
|
|
|
let performanceEntries: PerformanceEntryList = [];
|
|
|
|
|
|
|
|
function findMostRecent(
|
|
|
|
name: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
type: "mark" | "measure",
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceEntry | undefined {
|
|
|
|
return performanceEntries
|
|
|
|
.slice()
|
|
|
|
.reverse()
|
|
|
|
.find((entry) => entry.name === name && entry.entryType === type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertMarkToTimestamp(mark: string | number): number {
|
|
|
|
if (typeof mark === "string") {
|
|
|
|
const entry = findMostRecent(mark, "mark");
|
|
|
|
if (!entry) {
|
|
|
|
throw new SyntaxError(`Cannot find mark: "${mark}".`);
|
|
|
|
}
|
|
|
|
return entry.startTime;
|
|
|
|
}
|
|
|
|
if (mark < 0) {
|
|
|
|
throw new TypeError("Mark cannot be negative.");
|
|
|
|
}
|
|
|
|
return mark;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterByNameType(
|
|
|
|
name?: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
type?: "mark" | "measure",
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceEntryList {
|
|
|
|
return performanceEntries.filter(
|
|
|
|
(entry) =>
|
|
|
|
(name ? entry.name === name : true) &&
|
2020-07-14 15:24:17 -04:00
|
|
|
(type ? entry.entryType === type : true),
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function now(): number {
|
|
|
|
const res = opNow();
|
|
|
|
return res.seconds * 1e3 + res.subsecNanos / 1e6;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PerformanceEntryImpl implements PerformanceEntry {
|
|
|
|
#name: string;
|
|
|
|
#entryType: string;
|
|
|
|
#startTime: number;
|
|
|
|
#duration: number;
|
|
|
|
|
|
|
|
get name(): string {
|
|
|
|
return this.#name;
|
|
|
|
}
|
|
|
|
|
|
|
|
get entryType(): string {
|
|
|
|
return this.#entryType;
|
|
|
|
}
|
|
|
|
|
|
|
|
get startTime(): number {
|
|
|
|
return this.#startTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
get duration(): number {
|
|
|
|
return this.#duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
name: string,
|
|
|
|
entryType: string,
|
|
|
|
startTime: number,
|
2020-07-14 15:24:17 -04:00
|
|
|
duration: number,
|
2020-07-10 22:38:15 -04:00
|
|
|
) {
|
|
|
|
this.#name = name;
|
|
|
|
this.#entryType = entryType;
|
|
|
|
this.#startTime = startTime;
|
|
|
|
this.#duration = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
toJSON(): any {
|
|
|
|
return {
|
|
|
|
name: this.#name,
|
|
|
|
entryType: this.#entryType,
|
|
|
|
startTime: this.#startTime,
|
|
|
|
duration: this.#duration,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[customInspect](): string {
|
|
|
|
return `${this.constructor.name} { name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PerformanceMarkImpl extends PerformanceEntryImpl
|
|
|
|
implements PerformanceMark {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
#detail: any;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
get detail(): any {
|
|
|
|
return this.#detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
get entryType(): "mark" {
|
|
|
|
return "mark";
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
name: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
{ detail = null, startTime = now() }: PerformanceMarkOptions = {},
|
2020-07-10 22:38:15 -04:00
|
|
|
) {
|
|
|
|
super(name, "mark", startTime, 0);
|
|
|
|
if (startTime < 0) {
|
|
|
|
throw new TypeError("startTime cannot be negative");
|
|
|
|
}
|
|
|
|
this.#detail = cloneValue(detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
toJSON(): any {
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
entryType: this.entryType,
|
|
|
|
startTime: this.startTime,
|
|
|
|
duration: this.duration,
|
|
|
|
detail: this.detail,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[customInspect](): string {
|
|
|
|
return this.detail
|
2020-07-14 15:24:17 -04:00
|
|
|
? `${this.constructor.name} {\n detail: ${
|
|
|
|
inspect(this.detail, { depth: 3 })
|
|
|
|
},\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}`
|
2020-07-10 22:38:15 -04:00
|
|
|
: `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PerformanceMeasureImpl extends PerformanceEntryImpl
|
|
|
|
implements PerformanceMeasure {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
#detail: any;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
get detail(): any {
|
|
|
|
return this.#detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
get entryType(): "measure" {
|
|
|
|
return "measure";
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
name: string,
|
|
|
|
startTime: number,
|
|
|
|
duration: number,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-07-14 15:24:17 -04:00
|
|
|
detail: any = null,
|
2020-07-10 22:38:15 -04:00
|
|
|
) {
|
|
|
|
super(name, "measure", startTime, duration);
|
|
|
|
this.#detail = cloneValue(detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
toJSON(): any {
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
entryType: this.entryType,
|
|
|
|
startTime: this.startTime,
|
|
|
|
duration: this.duration,
|
|
|
|
detail: this.detail,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[customInspect](): string {
|
|
|
|
return this.detail
|
2020-07-14 15:24:17 -04:00
|
|
|
? `${this.constructor.name} {\n detail: ${
|
|
|
|
inspect(this.detail, { depth: 3 })
|
|
|
|
},\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}`
|
2020-07-10 22:38:15 -04:00
|
|
|
: `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PerformanceImpl implements Performance {
|
|
|
|
clearMarks(markName?: string): void {
|
|
|
|
if (markName == null) {
|
|
|
|
performanceEntries = performanceEntries.filter(
|
2020-07-14 15:24:17 -04:00
|
|
|
(entry) => entry.entryType !== "mark",
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
performanceEntries = performanceEntries.filter(
|
2020-07-14 15:24:17 -04:00
|
|
|
(entry) => !(entry.name === markName && entry.entryType === "mark"),
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearMeasures(measureName?: string): void {
|
|
|
|
if (measureName == null) {
|
|
|
|
performanceEntries = performanceEntries.filter(
|
2020-07-14 15:24:17 -04:00
|
|
|
(entry) => entry.entryType !== "measure",
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
performanceEntries = performanceEntries.filter(
|
|
|
|
(entry) =>
|
2020-07-14 15:24:17 -04:00
|
|
|
!(entry.name === measureName && entry.entryType === "measure"),
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getEntries(): PerformanceEntryList {
|
|
|
|
return filterByNameType();
|
|
|
|
}
|
|
|
|
getEntriesByName(
|
|
|
|
name: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
type?: "mark" | "measure",
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceEntryList {
|
|
|
|
return filterByNameType(name, type);
|
|
|
|
}
|
|
|
|
getEntriesByType(type: "mark" | "measure"): PerformanceEntryList {
|
|
|
|
return filterByNameType(undefined, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
mark(
|
|
|
|
markName: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
options: PerformanceMarkOptions = {},
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceMark {
|
|
|
|
// 3.1.1.1 If the global object is a Window object and markName uses the
|
|
|
|
// same name as a read only attribute in the PerformanceTiming interface,
|
|
|
|
// throw a SyntaxError. - not implemented
|
|
|
|
const entry = new PerformanceMarkImpl(markName, options);
|
|
|
|
// 3.1.1.7 Queue entry - not implemented
|
|
|
|
performanceEntries.push(entry);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
measure(
|
|
|
|
measureName: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
options?: PerformanceMeasureOptions,
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceMeasure;
|
|
|
|
measure(
|
|
|
|
measureName: string,
|
|
|
|
startMark?: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
endMark?: string,
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceMeasure;
|
|
|
|
measure(
|
|
|
|
measureName: string,
|
|
|
|
startOrMeasureOptions: string | PerformanceMeasureOptions = {},
|
2020-07-14 15:24:17 -04:00
|
|
|
endMark?: string,
|
2020-07-10 22:38:15 -04:00
|
|
|
): PerformanceMeasure {
|
|
|
|
if (startOrMeasureOptions && typeof startOrMeasureOptions === "object") {
|
|
|
|
if (endMark) {
|
|
|
|
throw new TypeError("Options cannot be passed with endMark.");
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!("start" in startOrMeasureOptions) &&
|
|
|
|
!("end" in startOrMeasureOptions)
|
|
|
|
) {
|
|
|
|
throw new TypeError("A start or end mark must be supplied in options.");
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
"start" in startOrMeasureOptions &&
|
|
|
|
"duration" in startOrMeasureOptions &&
|
|
|
|
"end" in startOrMeasureOptions
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
"Cannot specify start, end, and duration together in options.",
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let endTime: number;
|
|
|
|
if (endMark) {
|
|
|
|
endTime = convertMarkToTimestamp(endMark);
|
|
|
|
} else if (
|
|
|
|
typeof startOrMeasureOptions === "object" &&
|
|
|
|
"end" in startOrMeasureOptions
|
|
|
|
) {
|
|
|
|
endTime = convertMarkToTimestamp(startOrMeasureOptions.end!);
|
|
|
|
} else if (
|
|
|
|
typeof startOrMeasureOptions === "object" &&
|
|
|
|
"start" in startOrMeasureOptions &&
|
|
|
|
"duration" in startOrMeasureOptions
|
|
|
|
) {
|
|
|
|
const start = convertMarkToTimestamp(startOrMeasureOptions.start!);
|
|
|
|
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration!);
|
|
|
|
endTime = start + duration;
|
|
|
|
} else {
|
|
|
|
endTime = now();
|
|
|
|
}
|
|
|
|
let startTime: number;
|
|
|
|
if (
|
|
|
|
typeof startOrMeasureOptions === "object" &&
|
|
|
|
"start" in startOrMeasureOptions
|
|
|
|
) {
|
|
|
|
startTime = convertMarkToTimestamp(startOrMeasureOptions.start!);
|
|
|
|
} else if (
|
|
|
|
typeof startOrMeasureOptions === "object" &&
|
|
|
|
"end" in startOrMeasureOptions &&
|
|
|
|
"duration" in startOrMeasureOptions
|
|
|
|
) {
|
|
|
|
const end = convertMarkToTimestamp(startOrMeasureOptions.end!);
|
|
|
|
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration!);
|
|
|
|
startTime = end - duration;
|
|
|
|
} else if (typeof startOrMeasureOptions === "string") {
|
|
|
|
startTime = convertMarkToTimestamp(startOrMeasureOptions);
|
|
|
|
} else {
|
|
|
|
startTime = 0;
|
|
|
|
}
|
|
|
|
const entry = new PerformanceMeasureImpl(
|
|
|
|
measureName,
|
|
|
|
startTime,
|
|
|
|
endTime - startTime,
|
|
|
|
typeof startOrMeasureOptions === "object"
|
|
|
|
? startOrMeasureOptions.detail ?? null
|
2020-07-14 15:24:17 -04:00
|
|
|
: null,
|
2020-07-10 22:38:15 -04:00
|
|
|
);
|
|
|
|
performanceEntries.push(entry);
|
|
|
|
return entry;
|
|
|
|
}
|
2019-02-02 01:27:42 -05:00
|
|
|
|
|
|
|
now(): number {
|
2020-07-10 22:38:15 -04:00
|
|
|
return now();
|
2019-02-02 01:27:42 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 22:38:15 -04:00
|
|
|
|
|
|
|
setFunctionName(PerformanceEntryImpl, "PerformanceEntry");
|
|
|
|
setFunctionName(PerformanceMarkImpl, "PerformanceMark");
|
|
|
|
setFunctionName(PerformanceMeasureImpl, "PerformanceMeasure");
|
|
|
|
setFunctionName(PerformanceImpl, "Performance");
|