UNPKG

performance-node

Version:

Performance (User Timing API) for Node.js

149 lines (129 loc) 4.75 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function hrMillis() { var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.hrtime(); var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var arr = time; if (!(arr instanceof Array) || arr.length !== 2) { arr = process.hrtime(); } var number = arr[0] * 1000 + arr[1] / 1e6; return number - offset; } function getOffset(timelineInstance) { var offset = timelineInstance.offset, constructionTimeMillis = timelineInstance.constructionTimeMillis; return typeof offset === 'number' ? offset : constructionTimeMillis; } function markData(timelineInstance) { var obj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _timelineInstance$dat = timelineInstance.data, data = _timelineInstance$dat === undefined ? [] : _timelineInstance$dat, useTimestamp = timelineInstance.useTimestamp; var defaultHrtime = process.hrtime(); // set defaults var name = obj.name, _obj$startTime = obj.startTime, startTime = _obj$startTime === undefined ? hrMillis(defaultHrtime, getOffset(timelineInstance)) : _obj$startTime, _obj$timestamp = obj.timestamp, timestamp = _obj$timestamp === undefined ? Date.now() : _obj$timestamp, _obj$duration = obj.duration, duration = _obj$duration === undefined ? 0 : _obj$duration, _obj$entryType = obj.entryType, entryType = _obj$entryType === undefined ? 'mark' : _obj$entryType; var item = { name, startTime, duration, entryType }; if (useTimestamp) { item.timestamp = timestamp; } return data.concat(item).sort(function (a, b) { return a.startTime - b.startTime; }); } module.exports = function () { function Timeline() { var kwargs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Timeline); this.constructionTimeMillis = hrMillis(); this.data = []; this.offset = kwargs.offset; this.useTimestamp = kwargs.timestamp || false; return this; } _createClass(Timeline, [{ key: 'mark', value: function mark(name) { this.data = markData(this, { name }); } }, { key: 'getEntries', value: function getEntries() { return this.data.concat(); } }, { key: 'getEntriesByName', value: function getEntriesByName(name) { return this.data.filter(function (obj) { return obj.name === name; }); } }, { key: 'getEntriesByType', value: function getEntriesByType(type) { return this.data.filter(function (obj) { return obj.entryType === type; }); } }, { key: 'measure', value: function measure(name, startString, endString) { var startArr = this.getEntriesByName(startString); var startMark = startArr[startArr.length - 1] || { startTime: this.constructionTimeMillis - getOffset(this) }; var endArr = this.getEntriesByName(endString); var fallbackEndMark = { startTime: this.now() }; var endMark = (startString ? endArr[endArr.length - 1] : fallbackEndMark) || fallbackEndMark; var duration = endMark.startTime - startMark.startTime; this.data = markData(this, { name, startTime: startMark.startTime, timestamp: startMark.timestamp, duration, entryType: 'measure' }); } }, { key: 'clearMarks', value: function clearMarks() { this.data = this.data.filter(function (item) { return item.entryType !== 'mark'; }); } }, { key: 'clearMeasures', value: function clearMeasures() { this.data = this.data.filter(function (item) { return item.entryType !== 'measure'; }); } }, { key: 'clear', value: function clear() { this.data = []; } }, { key: 'now', value: function now() { return hrMillis(process.hrtime(), getOffset(this)); } }]); return Timeline; }();