UNPKG

gun-flint

Version:

Micro-framework for building Gun adapters

146 lines (117 loc) 5.28 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _baseMixin = require('./base-mixin'); var _baseMixin2 = _interopRequireDefault(_baseMixin); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var ProfilerMixin = function (_BaseMixin) { (0, _inherits3.default)(ProfilerMixin, _BaseMixin); function ProfilerMixin(context) { (0, _classCallCheck3.default)(this, ProfilerMixin); var _this = (0, _possibleConstructorReturn3.default)(this, (ProfilerMixin.__proto__ || (0, _getPrototypeOf2.default)(ProfilerMixin)).call(this, context, ['opt', 'afterRead', 'afterWrite', '__readAggregate', '__writeAggregate', '__readCount', '__readErrCount', '__writeCount', '__writeErrCount', '__readInterval', '__writeInterval', '__log'])); context.__readAggregate = []; context.__writeAggregate = []; context.__readCount = 0; context.__readErrCount = 0; context.__writeErrCount = 0; context.__writeCount = 0; context.__readInterval = null; context.__writeInterval = null; return _this; } (0, _createClass3.default)(ProfilerMixin, [{ key: '__log', value: function __log(message) { console.info('Profiler: ' + message); } /** * */ }, { key: 'opt', value: function opt(context, next) { var _this2 = this; console.warn('You are using the Gun-Flint profiler mixin. This is intended to profile your adapter during development and should not be deployed into production environments.'); // Update every minute setInterval(function () { var avgReads = _this2.__readAggregate.reduce(function (carry, val) { return carry + val; }, 0); _this2.__readAggregate = []; _this2.__log('GET AVG: ' + avgReads + ' total gets in last minute; ' + avgReads / 60 + ' reads/sec; .'); var avgWrites = _this2.__writeAggregate.reduce(function (carry, val) { return carry + val; }, 0); _this2.__writeAggregate = []; _this2.__log('PUT AVG: ' + avgWrites + ' total gets in last minute; ' + avgWrites / 60 + ' reads/sec.'); }, 60 * 1000); // Update every second this.__readInterval = setInterval(function () { if (_this2.__readCount) { var reads = _this2.__readCount; var readErr = _this2.__readErrCount; _this2.__readCount = 0; _this2.__readErrCount = 0; _this2.__log('GET: ' + writes + ' reads/sec; ' + reads / 10000 + ' ms/read; ' + readErr + ' errs/sec.'); _this2.__readAggregate.push(reads); } else { _this2.__log('GET: no reads in last 10 sec'); } }, 10000); // Update every second this.__writeInterval = setInterval(function () { if (_this2.__writeCount) { var _writes = _this2.__writeCount; var writeErr = _this2.__writeErrCount; _this2.__writeCount = 0; _this2.__writeErrCount = 0; _this2.__log('PUT: ' + _writes / 10 + ' writes/sec; ' + _writes / 10000 + ' ms/write; ' + writeErr + ' errs/sec.'); _this2.__writeAggregate.push(_writes); } else { _this2.__log('PUT: no writes in last 10 sec'); } }, 10000); next(context); } }, { key: 'afterRead', value: function afterRead(dedupId, err, data, next) { if (!data) { next = data; } if (!err) { this.__readCount++; } else { this.__readErrCount++; } next(dedupId, err, data); } }, { key: 'afterWrite', value: function afterWrite(dedupId, err, next) { if (!next) { next = err; err = null; } if (!err) { this.__writeCount++; } else { this.__writeErrCount++; } next(dedupId, err); } }]); return ProfilerMixin; }(_baseMixin2.default); exports.default = ProfilerMixin;