2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-10 22:38:15 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2023-01-29 09:15:01 -05:00
|
|
|
assertNotStrictEquals,
|
2021-07-08 09:43:36 -04:00
|
|
|
assertStringIncludes,
|
2020-09-19 17:30:59 -04:00
|
|
|
assertThrows,
|
2020-11-26 11:22:36 -05:00
|
|
|
deferred,
|
2020-07-10 22:38:15 -04:00
|
|
|
} from "./test_util.ts";
|
2019-02-02 01:27:42 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { hrtime: false } }, async function performanceNow() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2019-02-02 01:27:42 -05:00
|
|
|
const start = performance.now();
|
2021-08-25 16:04:14 -04:00
|
|
|
let totalTime = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
setTimeout(() => {
|
2019-02-02 01:27:42 -05:00
|
|
|
const end = performance.now();
|
2021-08-25 16:04:14 -04:00
|
|
|
totalTime = end - start;
|
2020-03-03 12:22:53 -05:00
|
|
|
resolvable.resolve();
|
2019-02-02 01:27:42 -05:00
|
|
|
}, 10);
|
2020-03-03 12:22:53 -05:00
|
|
|
await resolvable;
|
2021-08-25 16:04:14 -04:00
|
|
|
assert(totalTime >= 10);
|
2019-02-02 01:27:42 -05:00
|
|
|
});
|
2020-07-10 22:38:15 -04:00
|
|
|
|
2022-05-06 13:37:18 -04:00
|
|
|
Deno.test(function timeOrigin() {
|
|
|
|
const origin = performance.timeOrigin;
|
|
|
|
|
|
|
|
assert(origin > 0);
|
|
|
|
assert(Date.now() >= origin);
|
|
|
|
});
|
|
|
|
|
2022-05-13 12:36:00 -04:00
|
|
|
Deno.test(function performanceToJSON() {
|
|
|
|
const json = performance.toJSON();
|
|
|
|
|
|
|
|
assert("timeOrigin" in json);
|
|
|
|
assert(json.timeOrigin === performance.timeOrigin);
|
|
|
|
// check there are no other keys
|
|
|
|
assertEquals(Object.keys(json).length, 1);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceMark() {
|
2020-07-10 22:38:15 -04:00
|
|
|
const mark = performance.mark("test");
|
|
|
|
assert(mark instanceof PerformanceMark);
|
|
|
|
assertEquals(mark.detail, null);
|
|
|
|
assertEquals(mark.name, "test");
|
|
|
|
assertEquals(mark.entryType, "mark");
|
|
|
|
assert(mark.startTime > 0);
|
|
|
|
assertEquals(mark.duration, 0);
|
|
|
|
const entries = performance.getEntries();
|
|
|
|
assert(entries[entries.length - 1] === mark);
|
|
|
|
const markEntries = performance.getEntriesByName("test", "mark");
|
|
|
|
assert(markEntries[markEntries.length - 1] === mark);
|
|
|
|
});
|
|
|
|
|
2023-01-29 09:15:01 -05:00
|
|
|
Deno.test(function performanceMarkDetail() {
|
|
|
|
const detail = { foo: "foo" };
|
|
|
|
const mark = performance.mark("test", { detail });
|
|
|
|
assert(mark instanceof PerformanceMark);
|
|
|
|
assertEquals(mark.detail, { foo: "foo" });
|
|
|
|
assertNotStrictEquals(mark.detail, detail);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function performanceMarkDetailArrayBuffer() {
|
|
|
|
const detail = new ArrayBuffer(10);
|
|
|
|
const mark = performance.mark("test", { detail });
|
|
|
|
assert(mark instanceof PerformanceMark);
|
|
|
|
assertEquals(mark.detail, new ArrayBuffer(10));
|
|
|
|
assertNotStrictEquals(mark.detail, detail);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function performanceMarkDetailSubTypedArray() {
|
|
|
|
class SubUint8Array extends Uint8Array {}
|
|
|
|
const detail = new SubUint8Array([1, 2]);
|
|
|
|
const mark = performance.mark("test", { detail });
|
|
|
|
assert(mark instanceof PerformanceMark);
|
|
|
|
assertEquals(mark.detail, new Uint8Array([1, 2]));
|
|
|
|
assertNotStrictEquals(mark.detail, detail);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceMeasure() {
|
2020-10-19 22:43:29 -04:00
|
|
|
const markName1 = "mark1";
|
|
|
|
const measureName1 = "measure1";
|
|
|
|
const measureName2 = "measure2";
|
|
|
|
const mark1 = performance.mark(markName1);
|
2023-03-22 14:06:29 -04:00
|
|
|
// Measure against the inaccurate-but-known-good wall clock
|
|
|
|
const now = new Date().valueOf();
|
2020-07-10 22:38:15 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
2023-03-22 14:06:29 -04:00
|
|
|
const later = new Date().valueOf();
|
2020-10-19 22:43:29 -04:00
|
|
|
const measure1 = performance.measure(measureName1, markName1);
|
|
|
|
const measure2 = performance.measure(
|
|
|
|
measureName2,
|
|
|
|
undefined,
|
|
|
|
markName1,
|
|
|
|
);
|
|
|
|
assert(measure1 instanceof PerformanceMeasure);
|
|
|
|
assertEquals(measure1.detail, null);
|
|
|
|
assertEquals(measure1.name, measureName1);
|
|
|
|
assertEquals(measure1.entryType, "measure");
|
|
|
|
assert(measure1.startTime > 0);
|
|
|
|
assertEquals(measure2.startTime, 0);
|
|
|
|
assertEquals(mark1.startTime, measure1.startTime);
|
|
|
|
assertEquals(mark1.startTime, measure2.duration);
|
2020-07-13 00:50:57 -04:00
|
|
|
assert(
|
2020-10-19 22:43:29 -04:00
|
|
|
measure1.duration >= 100,
|
|
|
|
`duration below 100ms: ${measure1.duration}`,
|
2020-07-13 00:50:57 -04:00
|
|
|
);
|
|
|
|
assert(
|
2023-03-22 14:06:29 -04:00
|
|
|
measure1.duration < (later - now) * 1.50,
|
|
|
|
`duration exceeds 150% of wallclock time: ${measure1.duration}ms vs ${
|
|
|
|
later - now
|
|
|
|
}ms`,
|
2020-07-13 00:50:57 -04:00
|
|
|
);
|
2020-07-10 22:38:15 -04:00
|
|
|
const entries = performance.getEntries();
|
2020-10-19 22:43:29 -04:00
|
|
|
assert(entries[entries.length - 1] === measure2);
|
|
|
|
const entriesByName = performance.getEntriesByName(
|
|
|
|
measureName1,
|
|
|
|
"measure",
|
|
|
|
);
|
|
|
|
assert(entriesByName[entriesByName.length - 1] === measure1);
|
|
|
|
const measureEntries = performance.getEntriesByType("measure");
|
|
|
|
assert(measureEntries[measureEntries.length - 1] === measure2);
|
2020-07-10 22:38:15 -04:00
|
|
|
} catch (e) {
|
|
|
|
return reject(e);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
});
|
2020-09-19 17:30:59 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceCustomInspectFunction() {
|
2021-07-08 09:43:36 -04:00
|
|
|
assertStringIncludes(Deno.inspect(performance), "Performance");
|
|
|
|
assertStringIncludes(
|
|
|
|
Deno.inspect(Performance.prototype),
|
|
|
|
"Performance",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceMarkCustomInspectFunction() {
|
2021-07-08 09:43:36 -04:00
|
|
|
const mark1 = performance.mark("mark1");
|
|
|
|
assertStringIncludes(Deno.inspect(mark1), "PerformanceMark");
|
|
|
|
assertStringIncludes(
|
|
|
|
Deno.inspect(PerformanceMark.prototype),
|
|
|
|
"PerformanceMark",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceMeasureCustomInspectFunction() {
|
2021-07-08 09:43:36 -04:00
|
|
|
const measure1 = performance.measure("measure1");
|
|
|
|
assertStringIncludes(Deno.inspect(measure1), "PerformanceMeasure");
|
|
|
|
assertStringIncludes(
|
|
|
|
Deno.inspect(PerformanceMeasure.prototype),
|
|
|
|
"PerformanceMeasure",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceIllegalConstructor() {
|
2021-07-05 07:17:11 -04:00
|
|
|
assertThrows(() => new Performance(), TypeError, "Illegal constructor");
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals(Performance.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceEntryIllegalConstructor() {
|
2021-07-05 07:17:11 -04:00
|
|
|
assertThrows(() => new PerformanceEntry(), TypeError, "Illegal constructor");
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals(PerformanceEntry.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function performanceMeasureIllegalConstructor() {
|
2020-09-19 17:30:59 -04:00
|
|
|
assertThrows(
|
|
|
|
() => new PerformanceMeasure(),
|
|
|
|
TypeError,
|
2021-07-05 07:17:11 -04:00
|
|
|
"Illegal constructor",
|
2020-09-19 17:30:59 -04:00
|
|
|
);
|
|
|
|
});
|
2022-05-11 21:15:54 -04:00
|
|
|
|
|
|
|
Deno.test(function performanceIsEventTarget() {
|
|
|
|
assert(performance instanceof EventTarget);
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const handler = () => {
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
performance.addEventListener("test", handler, { once: true });
|
|
|
|
performance.dispatchEvent(new Event("test"));
|
|
|
|
});
|
|
|
|
});
|