prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
100 lines (81 loc) • 2.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PerformanceTracker = exports.RealmStatistics = void 0;
var _invariant = _interopRequireDefault(require("./invariant.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
class RealmStatistics {
constructor(getTime, getMemory) {
this.simplifications = 0;
this.simplificationAttempts = 0;
this.evaluatedNodes = 0;
this.parsing = new PerformanceTracker(getTime, getMemory);
this.fixupSourceLocations = new PerformanceTracker(getTime, getMemory);
this.fixupFilenames = new PerformanceTracker(getTime, getMemory);
this.evaluation = new PerformanceTracker(getTime, getMemory);
}
// legacy projection
getRealmStatistics() {
return {
simplifications: this.simplifications,
simplificationAttempts: this.simplificationAttempts,
evaluatedNodes: this.evaluatedNodes
};
}
projectPerformanceTrackers(suffix, projection) {
let res = {};
for (let key of Object.keys(this)) {
let value = this[key];
if (value instanceof PerformanceTracker) res[key + suffix] = projection(value);
}
return res;
}
log() {
console.log(`=== realm statistics`);
console.log(`${this.evaluatedNodes} AST nodes evaluated.`);
console.log(`${this.simplifications} abstract values simplified after ${this.simplificationAttempts} attempts.`);
}
logPerformanceTrackers(format) {
console.log(`${format(this.parsing)} parsing, ${format(this.fixupSourceLocations)} fixing source locations, ${format(this.fixupFilenames)} fixing filenames, ${format(this.evaluation)} evaluating global code`);
}
}
exports.RealmStatistics = RealmStatistics;
class PerformanceTracker {
constructor(getTime, getMemory) {
this.time = this.memory = 0;
this._getTime = getTime;
this._getMemory = getMemory;
this._running = false;
}
start() {
(0, _invariant.default)(this._running === false);
if (this._getTime !== undefined) this.time = this._getTime() - this.time;
if (this._getMemory !== undefined) this.memory = this._getMemory() - this.memory;
this._running = true;
}
stop() {
(0, _invariant.default)(this._running === true);
if (this._getTime !== undefined) this.time = this._getTime() - this.time;
if (this._getMemory !== undefined) this.memory = this._getMemory() - this.memory;
this._running = false;
}
measure(action) {
this.start();
try {
return action();
} finally {
this.stop();
}
}
}
exports.PerformanceTracker = PerformanceTracker;
//# sourceMappingURL=statistics.js.map