heimdalljs-fs-monitor
Advanced tools
Comparing version 0.1.0 to 0.2.0
204
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var heimdall = require('heimdalljs'); | ||
var logger = require('heimdalljs-logger')('heimdalljs-fs-monitor'); | ||
module.exports = FSMonitor; | ||
const fs = require('fs'); | ||
const heimdall = require('heimdalljs'); | ||
const logger = require('heimdalljs-logger')('heimdalljs-fs-monitor'); | ||
// It is possible for this module to be evaluated more than once in the same | ||
// heimdall session. In that case, we need to guard against double-counting by | ||
// making other instances of FSMonitor inert. | ||
var isMonitorRegistrant = false; | ||
var isFirstInstance = true; | ||
let isMonitorRegistrant = false; | ||
let hasActiveInstance = false; | ||
function FSMonitor() { | ||
this.state = 'idle'; | ||
this.blacklist = ['createReadStream', 'createWriteStream', 'ReadStream', 'WriteStream']; | ||
this._isEnabled = isMonitorRegistrant && isFirstInstance; | ||
class FSMonitor { | ||
constructor() { | ||
this.state = 'idle'; | ||
this.blacklist = ['createReadStream', 'createWriteStream', 'ReadStream', 'WriteStream']; | ||
} | ||
// Flip this to false because we want other instances from the same module to | ||
// be inert. | ||
isFirstInstance = false; | ||
} | ||
start() { | ||
if (isMonitorRegistrant && !hasActiveInstance) { | ||
this.state = 'active'; | ||
this._attach(); | ||
hasActiveInstance = true; | ||
} else { | ||
logger.warn('Multiple instances of heimdalljs-fs-monitor have been created' | ||
+ ' in the same session. Since this can cause fs operations to be counted' | ||
+ ' multiple times, this instance has been disabled.'); | ||
} | ||
} | ||
FSMonitor.prototype.start = function() { | ||
if (this._isEnabled) { | ||
this.state = 'active'; | ||
this._attach(); | ||
} else { | ||
logger.warn('Multiple instances of heimdalljs-fs-monitor have been created' | ||
+ ' in the same session. Since this can cause fs operations to be counted' | ||
+ ' multiple times, this instance has been disabled.'); | ||
stop() { | ||
if (this.state === 'active') { | ||
this.state = 'idle'; | ||
this._detach(); | ||
hasActiveInstance = false; | ||
} | ||
} | ||
}; | ||
FSMonitor.prototype.stop = function() { | ||
if (this._isEnabled) { | ||
this.state = 'idle'; | ||
this._detach(); | ||
shouldMeasure() { | ||
return this.state === 'active'; | ||
} | ||
}; | ||
FSMonitor.prototype.shouldMeasure = function() { | ||
return this.state === 'active'; | ||
}; | ||
_measure(name, original, context, args) { | ||
if (this.state !== 'active') { | ||
throw new Error('Cannot measure if the monitor is not active'); | ||
} | ||
if (!heimdall.hasMonitor('fs')) { | ||
heimdall.registerMonitor('fs', function FSSchema() {}); | ||
isMonitorRegistrant = true; | ||
} | ||
let metrics = heimdall.statsFor('fs'); | ||
let m = metrics[name] = metrics[name] || new Metric(); | ||
function Metric() { | ||
this.count = 0; | ||
this.time = 0; | ||
this.startTime = undefined; | ||
} | ||
m.start(); | ||
Metric.prototype.start = function() { | ||
this.startTime = process.hrtime(); | ||
this.count++; | ||
}; | ||
// TODO: handle async | ||
try { | ||
return original.apply(context, args); | ||
} finally { | ||
m.stop(); | ||
} | ||
} | ||
Metric.prototype.stop = function() { | ||
var now = process.hrtime(); | ||
_attach() { | ||
let monitor = this; | ||
this.time += (now[0] - this.startTime[0]) * 1e9 + (now[1] - this.startTime[1]); | ||
this.startTime = undefined; | ||
}; | ||
for (let member in fs) { | ||
if (this.blacklist.indexOf(member) === -1) { | ||
let old = fs[member]; | ||
if (typeof old === 'function') { | ||
fs[member] = (function(old, member) { | ||
return function() { | ||
if (monitor.shouldMeasure()) { | ||
let args = new Array(arguments.length); | ||
for (let i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
Metric.prototype.toJSON = function() { | ||
return { | ||
count: this.count, | ||
time: this.time | ||
}; | ||
}; | ||
return monitor._measure(member, old, fs, args); | ||
} else { | ||
return old.apply(fs, arguments); | ||
} | ||
}; | ||
}(old, member)); | ||
FSMonitor.prototype._measure = function(name, original, context, args) { | ||
if (this.state !== 'active') { | ||
throw new Error('Cannot measure if the monitor is not active'); | ||
fs[member].__restore = function() { | ||
fs[member] = old; | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
var metrics = heimdall.statsFor('fs'); | ||
var m = metrics[name] = metrics[name] || new Metric(); | ||
_detach() { | ||
for (let member in fs) { | ||
let maybeFunction = fs[member]; | ||
if (typeof maybeFunction === 'function' && typeof maybeFunction.__restore === 'function') { | ||
maybeFunction.__restore(); | ||
} | ||
} | ||
} | ||
} | ||
m.start(); | ||
module.exports = FSMonitor; | ||
// TODO: handle async | ||
try { | ||
return original.apply(context, args); | ||
} finally { | ||
m.stop(); | ||
if (!heimdall.hasMonitor('fs')) { | ||
heimdall.registerMonitor('fs', function FSSchema() {}); | ||
isMonitorRegistrant = true; | ||
} | ||
class Metric { | ||
constructor() { | ||
this.count = 0; | ||
this.time = 0; | ||
this.startTime = undefined; | ||
} | ||
}; | ||
FSMonitor.prototype._attach = function() { | ||
var monitor = this; | ||
start() { | ||
this.startTime = process.hrtime(); | ||
this.count++; | ||
} | ||
for (var member in fs) { | ||
if (this.blacklist.indexOf(member) === -1) { | ||
var old = fs[member]; | ||
if (typeof old === 'function') { | ||
fs[member] = (function(old, member) { | ||
return function() { | ||
if (monitor.shouldMeasure()) { | ||
var args = new Array(arguments.length); | ||
for (var i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
stop() { | ||
let now = process.hrtime(); | ||
return monitor._measure(member, old, fs, args); | ||
} else { | ||
return old.apply(fs, arguments); | ||
} | ||
}; | ||
}(old, member)); | ||
this.time += (now[0] - this.startTime[0]) * 1e9 + (now[1] - this.startTime[1]); | ||
this.startTime = undefined; | ||
} | ||
fs[member].__restore = function() { | ||
fs[member] = old; | ||
}; | ||
} | ||
} | ||
toJSON() { | ||
return { | ||
count: this.count, | ||
time: this.time | ||
}; | ||
} | ||
}; | ||
} | ||
FSMonitor.prototype._detach = function() { | ||
for (var member in fs) { | ||
var maybeFunction = fs[member]; | ||
if (typeof maybeFunction === 'function' && typeof maybeFunction.__restore === 'function') { | ||
maybeFunction.__restore(); | ||
} | ||
} | ||
}; |
{ | ||
"name": "heimdalljs-fs-monitor", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "fs operation monitoring for heimdalljs", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha tests.js", | ||
"test:debug": "mocha debug tests.js" | ||
}, | ||
@@ -15,6 +16,11 @@ "repository": { | ||
"license": "ISC", | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"chai": "^3.5.0", | ||
"heimdalljs": "^0.2.0", | ||
"heimdalljs-logger": "^0.1.7" | ||
"heimdalljs-logger": "^0.1.7", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4897
111
2
0
45
4
+ Addedchai@^3.5.0
+ Addedmocha@^3.2.0
+ Addedassertion-error@1.1.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowser-stdout@1.3.0(transitive)
+ Addedchai@3.5.0(transitive)
+ Addedcommander@2.9.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddebug@2.6.8(transitive)
+ Addeddeep-eql@0.1.3(transitive)
+ Addeddiff@3.2.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.1.1(transitive)
+ Addedgraceful-readlink@1.0.1(transitive)
+ Addedgrowl@1.9.2(transitive)
+ Addedhas-flag@1.0.0(transitive)
+ Addedhe@1.1.1(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjson3@3.3.2(transitive)
+ Addedlodash._baseassign@3.2.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basecreate@3.0.3(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash.create@3.1.1(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@0.0.8(transitive)
+ Addedmkdirp@0.5.1(transitive)
+ Addedmocha@3.5.3(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedsupports-color@3.1.2(transitive)
+ Addedtype-detect@0.1.11.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)