|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +/*global define*/ |
| 9 | + |
| 10 | +// This module can be loaded in an amd and commonjs-context. |
| 11 | +// Because we want both instances to use the same perf-data |
| 12 | +// we store them globally |
| 13 | +global._performanceEntries = global._performanceEntries || []; |
| 14 | + |
| 15 | +if (typeof define !== "function" && typeof module === "object" && typeof module.exports === "object") { |
| 16 | + // this is commonjs, fake amd |
| 17 | + global.define = function (dep, callback) { |
| 18 | + module.exports = callback(); |
| 19 | + global.define = undefined; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +define([], function () { |
| 24 | + |
| 25 | + // const _now = global.performance && performance.now ? performance.now : Date.now |
| 26 | + const _now = Date.now; |
| 27 | + |
| 28 | + class PerformanceEntry { |
| 29 | + constructor(type, name, startTime, duration) { |
| 30 | + this.type = type; |
| 31 | + this.name = name; |
| 32 | + this.startTime = startTime; |
| 33 | + this.duration = duration; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + function _getEntry(type, name) { |
| 38 | + for (let i = global._performanceEntries.length - 1; i >= 0; i--) { |
| 39 | + if ( |
| 40 | + (type === undefined || global._performanceEntries[i].type === type) && |
| 41 | + (name === undefined || global._performanceEntries[i].name === name) |
| 42 | + ) { |
| 43 | + return global._performanceEntries[i]; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + function importEntries(entries) { |
| 49 | + global._performanceEntries.splice(0, 0, ...entries); |
| 50 | + } |
| 51 | + |
| 52 | + function getEntries(type, name) { |
| 53 | + return global._performanceEntries.filter(entry => { |
| 54 | + return (type === undefined || entry.type === type) && |
| 55 | + (name === undefined || entry.name === name); |
| 56 | + }).sort((a, b) => { |
| 57 | + return a.startTime - b.startTime; |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + function mark(name) { |
| 62 | + const entry = new PerformanceEntry('mark', name, _now(), 0); |
| 63 | + global._performanceEntries.push(entry); |
| 64 | + if (typeof console.timeStamp === 'function') { |
| 65 | + console.timeStamp(name); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function time(name) { |
| 70 | + let from = `${name}/start`; |
| 71 | + mark(from); |
| 72 | + return { stop() { measure(name, from); } }; |
| 73 | + } |
| 74 | + |
| 75 | + function measure(name, from, to) { |
| 76 | + |
| 77 | + let startTime; |
| 78 | + let duration; |
| 79 | + let now = _now(); |
| 80 | + |
| 81 | + if (!from) { |
| 82 | + startTime = now; |
| 83 | + } else { |
| 84 | + startTime = _getEntry(undefined, from).startTime; |
| 85 | + } |
| 86 | + |
| 87 | + if (!to) { |
| 88 | + duration = now - startTime; |
| 89 | + } else { |
| 90 | + duration = _getEntry(undefined, to).startTime - startTime; |
| 91 | + } |
| 92 | + |
| 93 | + const entry = new PerformanceEntry('measure', name, startTime, duration); |
| 94 | + global._performanceEntries.push(entry); |
| 95 | + } |
| 96 | + |
| 97 | + var exports = { |
| 98 | + mark: mark, |
| 99 | + measure: measure, |
| 100 | + time: time, |
| 101 | + getEntries: getEntries, |
| 102 | + importEntries: importEntries |
| 103 | + }; |
| 104 | + |
| 105 | + return exports; |
| 106 | +}); |
0 commit comments