| 1 |
// Gathers statistical data, and provides them in convinient form |
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
var partial = require("es5-ext/function/#/partial") |
| 6 |
, forEach = require("es5-ext/object/for-each") |
| 7 |
, pad = require("es5-ext/string/#/pad") |
| 8 |
, compact = require("es5-ext/array/#/compact") |
| 9 |
, d = require("d") |
| 10 |
, memoize = require("./plain"); |
| 11 |
|
| 12 |
var max = Math.max, stats = (exports.statistics = {}); |
| 13 |
|
| 14 |
Object.defineProperty( |
| 15 |
memoize, "__profiler__", |
| 16 |
d(function (conf) { |
| 17 |
var id, source, data, stack; |
| 18 |
stack = new Error().stack; |
| 19 |
if ( |
| 20 |
!stack || |
| 21 |
!stack |
| 22 |
.split("\n") |
| 23 |
.slice(3) |
| 24 |
.some(function (line) { |
| 25 |
if (line.indexOf("/memoizee/") === -1 && line.indexOf(" (native)") === -1) { |
| 26 |
source = line.replace(/\n/g, "\\n").trim(); |
| 27 |
return true; |
| 28 |
} |
| 29 |
return false; |
| 30 |
}) |
| 31 |
) { |
| 32 |
source = "unknown"; |
| 33 |
} |
| 34 |
id = compact.call([conf.profileName, source]).join(", "); |
| 35 |
|
| 36 |
if (!stats[id]) stats[id] = { initial: 0, cached: 0 }; |
| 37 |
data = stats[id]; |
| 38 |
|
| 39 |
conf.on("set", function () { ++data.initial; }); |
| 40 |
conf.on("get", function () { ++data.cached; }); |
| 41 |
}) |
| 42 |
); |
| 43 |
|
| 44 |
exports.log = function () { |
| 45 |
var initial, cached, ordered, ipad, cpad, ppad, toPrc, log; |
| 46 |
|
| 47 |
initial = cached = 0; |
| 48 |
ordered = []; |
| 49 |
|
| 50 |
toPrc = function (initialCount, cachedCount) { |
| 51 |
if (!initialCount && !cachedCount) { |
| 52 |
return "0.00"; |
| 53 |
} |
| 54 |
return ((cachedCount / (initialCount + cachedCount)) * 100).toFixed(2); |
| 55 |
}; |
| 56 |
|
| 57 |
log = "------------------------------------------------------------\n"; |
| 58 |
log += "Memoize statistics:\n\n"; |
| 59 |
|
| 60 |
forEach( |
| 61 |
stats, |
| 62 |
function (data, name) { |
| 63 |
initial += data.initial; |
| 64 |
cached += data.cached; |
| 65 |
ordered.push([name, data]); |
| 66 |
}, |
| 67 |
null, |
| 68 |
function (nameA, nameB) { |
| 69 |
return ( |
| 70 |
this[nameB].initial + |
| 71 |
this[nameB].cached - |
| 72 |
(this[nameA].initial + this[nameA].cached) |
| 73 |
); |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
ipad = partial.call(pad, " ", max(String(initial).length, "Init".length)); |
| 78 |
cpad = partial.call(pad, " ", max(String(cached).length, "Cache".length)); |
| 79 |
ppad = partial.call(pad, " ", "%Cache".length); |
| 80 |
log += |
| 81 |
ipad.call("Init") + |
| 82 |
" " + |
| 83 |
cpad.call("Cache") + |
| 84 |
" " + |
| 85 |
ppad.call("%Cache") + |
| 86 |
" Source location\n"; |
| 87 |
log += |
| 88 |
ipad.call(initial) + |
| 89 |
" " + |
| 90 |
cpad.call(cached) + |
| 91 |
" " + |
| 92 |
ppad.call(toPrc(initial, cached)) + |
| 93 |
" (all)\n"; |
| 94 |
ordered.forEach(function (data) { |
| 95 |
var name = data[0]; |
| 96 |
data = data[1]; |
| 97 |
log += |
| 98 |
ipad.call(data.initial) + |
| 99 |
" " + |
| 100 |
cpad.call(data.cached) + |
| 101 |
" " + |
| 102 |
ppad.call(toPrc(data.initial, data.cached)) + |
| 103 |
" " + |
| 104 |
name + |
| 105 |
"\n"; |
| 106 |
}); |
| 107 |
log += "------------------------------------------------------------\n"; |
| 108 |
return log; |
| 109 |
}; |