Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 59 additions & 57 deletions lib/miniprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
* Guilherme Oenning, 2016 @goenning
*/

var _ = require('./utils.js');
var qs = require('querystring');
var url = require('url');
var ui = require('./ui.js');
var clientParser = require('./client-parser.js');
var _ = require('./utils.js');
var qs = require('querystring');
var url = require('url');
var ui = require('./ui.js');
var clientParser = require('./client-parser.js');

const hostname = require('os').hostname;
const hostname = require('os').hostname;
var ignoredPaths = [];
var trivialDurationThresholdMilliseconds = 2.5;
var popupShowTimeWithChildren = false;
var popupRenderPosition = 'left';
var trivialDurationThresholdMilliseconds = 2.5;
var popupShowTimeWithChildren = false;
var popupRenderPosition = 'left';
var resourcePath = '/mini-profiler-resources/';

exports.storage = {
exports.storage = {
InMemoryStorage: require('./storages/inmemory.js'),
RedisStorage: require('./storages/redis.js')
};
Expand All @@ -36,7 +37,7 @@ for (let framework of ['koa', 'express', 'hapi']) {
let func = require(`./middlewares/${framework}.js`);

exports[framework] = function(options) {
options = options || { };
options = options || {};

if (!options.enable) options.enable = () => { return true; };
if (!options.authorize) options.authorize = () => { return true; };
Expand All @@ -47,7 +48,6 @@ for (let framework of ['koa', 'express', 'hapi']) {
exports[framework].for = func.buildMiddleware;
}

var resourcePath = '/mini-profiler-resources/';
var version = require('../package.json').version;

var contentTypes = {
Expand Down Expand Up @@ -100,10 +100,10 @@ function handleRequest(enable, authorize, req, res) {
var lastPathSegment = segments[segments.length - 1];
var handler = (lastPathSegment == 'results') ? results : assets;
handler(req, res, lastPathSegment, (result) => {
res.writeHead(result.status, { 'Content-Type': result.type });
res.end(result.body);
resolve(true);
});
res.writeHead(result.status, { 'Content-Type': result.type });
res.end(result.body);
resolve(true);
});
});
}

Expand Down Expand Up @@ -140,7 +140,7 @@ function results(req, res, lastPathSegment, done) {
if (!data) {
done({
type: 'text/plain; charset=utf-8',
status:404,
status: 404,
body: `Id '${id}' not found.`
});
return;
Expand Down Expand Up @@ -218,22 +218,24 @@ function include(id) {
* - trivialDurationThresholdMilliseconds: double ; any step lasting longer than this will be considered trivial, and hidden by default
* - popupShowTimeWithChildren: boolean ; whether or not to include the "time with children" column
* - popupRenderPosition: 'left', 'right', 'bottomLeft', 'bottomRight' ; which side of the screen to display timings on
* - resourcePath: string ; if your site root is in a subdirectory, specify here, e.g., /siteroot
*/
function configure(options){
options = options || {};

ignoredPaths = options.ignoredPaths || ignoredPaths;
trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds;
popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren;
popupRenderPosition = options.popupRenderPosition || popupRenderPosition;
storage = options.storage || storage;
}
function configure(options) {
options = options || {};

ignoredPaths = options.ignoredPaths || ignoredPaths;
trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds;
popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren;
popupRenderPosition = options.popupRenderPosition || popupRenderPosition;
storage = options.storage || storage;
resourcePath = `${options.resourcePath ? options.resourcePath.replace(/\/$/, '') : ''}${resourcePath}`;
}

/*
* Begins profiling the given request.
*/
function startProfiling(request, enabled, authorized) {
var currentRequestExtension = {
function startProfiling(request, enabled, authorized) {
var currentRequestExtension = {
enabled: enabled,
authorized: authorized
};
Expand Down Expand Up @@ -286,7 +288,7 @@ function include(id) {
/*
* Stops profiling the given request.
*/
function stopProfiling(extension, request){
function stopProfiling(extension, request) {
var time = process.hrtime();

extension.stopTime = time;
Expand All @@ -301,29 +303,29 @@ function include(id) {
*
* You should only use this method directly in cases when calls to addProfiling won't suffice.
*/
function step(name, request, call) {
var time = process.hrtime();
function step(name, request, call) {
var time = process.hrtime();

var extension = request.miniprofiler;
var extension = request.miniprofiler;

var newStep = makeStep(name, time, extension.stepGraph);
extension.stepGraph.steps.push(newStep);
extension.stepGraph = newStep;
var newStep = makeStep(name, time, extension.stepGraph);
extension.stepGraph.steps.push(newStep);
extension.stepGraph = newStep;

var result;
if (call.length) {
var result;
if (call.length) {
result = call(() => {
unstep(name, request);
});
unstep(name, request);
});
} else {
try {
result = call();
} finally {
unstep(name, request);
}
}
result = call();
} finally {
unstep(name, request);
}
}

return result;
return result;
}

/*
Expand All @@ -338,11 +340,11 @@ function include(id) {
* when the query has completed. Implicitly, any execution of a callback is considered
* to have ended the query.
*/
function timeQuery(extension, type, query, executeFunction) {
var timing = startTimeQuery(extension, type, query);
var params = Array.prototype.slice.call(arguments, 4);
function timeQuery(extension, type, query, executeFunction) {
var timing = startTimeQuery(extension, type, query);
var params = Array.prototype.slice.call(arguments, 4);

for(var i = 0; i < params.length; i++){
for (var i = 0; i < params.length; i++) {
if (_.isFunction(params[i])) {
var param = params[i];
params[i] = function() {
Expand Down Expand Up @@ -403,7 +405,7 @@ function describePerformance(root, request) {
return ret;
}

function diff(start, stop){
function diff(start, stop) {
var deltaSecs = stop[0] - start[0];
var deltaNanoSecs = stop[1] - start[1];

Expand All @@ -415,22 +417,22 @@ function diff(start, stop){
function callStack(stack) {
var sp = stack.split('\n');
var ret = [];
for(var i = 2; i < sp.length; i++) {
for (var i = 2; i < sp.length; i++) {
var st = sp[i].trim().split(' ');
ret.push(st[1]);
}
return ret.join(' ');
}

function describeTimings(timing, root){
function describeTimings(timing, root) {
var id = _.uuid();
var name = timing.name;
var elapsedMs = diff(timing.startTime, timing.stopTime);
var sinceRootMs = diff(root.startTime, timing.startTime);
var customTimings = describeCustomTimings(timing.customTimings, root);

var children = [];
for(var i = 0; i < timing.steps.length; i++){
for (var i = 0; i < timing.steps.length; i++) {
var step = timing.steps[i];
children.push(describeTimings(step, root));
}
Expand All @@ -447,12 +449,12 @@ function describeTimings(timing, root){

function describeCustomTimings(customTimings, root) {
var ret = {};
for(var prop in customTimings) {
for (var prop in customTimings) {

var arr = customTimings[prop];
var retArr = [];

for(var i = 0; i < arr.length; i++) {
for (var i = 0; i < arr.length; i++) {
var timing = {};
timing.Id = arr[i].id;
timing.ExecuteType = arr[i].executeType;
Expand All @@ -470,6 +472,6 @@ function describeCustomTimings(customTimings, root) {
return ret;
}

function makeStep(name, time, parent){
function makeStep(name, time, parent) {
return { name: name, startTime: time, stopTime: null, parent: parent, steps: [], customTimings: {} };
}
}