2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-10 22:38:15 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-09-19 17:30:59 -04:00
|
|
|
assertThrows,
|
2020-07-10 22:38:15 -04:00
|
|
|
createResolvable,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-07-10 22:38:15 -04:00
|
|
|
} from "./test_util.ts";
|
2019-02-02 01:27:42 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { hrtime: false } }, async function performanceNow(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-03-03 12:22:53 -05:00
|
|
|
const resolvable = createResolvable();
|
2019-02-02 01:27:42 -05:00
|
|
|
const start = performance.now();
|
2019-04-21 16:40:10 -04:00
|
|
|
setTimeout((): void => {
|
2019-02-02 01:27:42 -05:00
|
|
|
const end = performance.now();
|
|
|
|
assert(end - start >= 10);
|
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;
|
2019-02-02 01:27:42 -05:00
|
|
|
});
|
2020-07-10 22:38:15 -04:00
|
|
|
|
|
|
|
unitTest(function performanceMark() {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function performanceMeasure() {
|
2020-10-19 22:43:29 -04:00
|
|
|
const markName1 = "mark1";
|
|
|
|
const measureName1 = "measure1";
|
|
|
|
const measureName2 = "measure2";
|
|
|
|
const mark1 = performance.mark(markName1);
|
2020-07-10 22:38:15 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
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(
|
2020-10-19 22:43:29 -04:00
|
|
|
measure1.duration < 500,
|
|
|
|
`duration exceeds 500ms: ${measure1.duration}`,
|
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
|
|
|
|
|
|
|
unitTest(function performanceIllegalConstructor() {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function performanceEntryIllegalConstructor() {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function performanceMeasureIllegalConstructor() {
|
|
|
|
assertThrows(
|
|
|
|
() => new PerformanceMeasure(),
|
|
|
|
TypeError,
|
|
|
|
"Illegal constructor.",
|
|
|
|
);
|
|
|
|
});
|