88 */
99
1010var _ = require ( './ui/underscore.js' ) ;
11- var domain = require ( 'domain' ) ;
1211var fs = require ( 'fs' ) ;
1312var os = require ( 'os' ) ;
1413var path = require ( 'path' ) ;
@@ -22,10 +21,7 @@ _.templateSettings = {
2221// EXPORTS
2322exports . configure = configure ;
2423exports . instrument = addProfilingInstrumentation ;
25- exports . step = step ;
26- exports . timeQuery = timeQuery ;
2724exports . profile = middleware ;
28- exports . include = include ;
2925
3026// GLOBALS
3127var storage = function ( id , json ) {
@@ -149,32 +145,21 @@ function middleware(f) {
149145 static ( reqPath , res ) ;
150146 return ;
151147 }
152-
153- var reqDomain = domain . create ( ) ;
154- reqDomain . add ( req ) ;
155- reqDomain . add ( res ) ;
156148 if ( enabled ) {
157149 res . on ( 'header' , function ( ) {
158- stopProfiling ( ) ;
150+ stopProfiling ( req ) ;
159151 } ) ;
152+ res . setHeader ( "X-MiniProfiler-Ids" , '["' + id + '"]' ) ;
160153 }
161- reqDomain . run ( function ( ) {
162- var id = startProfiling ( req , enabled ) ;
163- if ( enabled ) {
164- res . setHeader ( "X-MiniProfiler-Ids" , '["' + id + '"]' ) ;
165- }
166- next ( ) ;
167- } ) ;
154+ var id = startProfiling ( req , enabled ) ;
155+ next ( ) ;
168156 } ;
169157}
170158
171159function include ( id ) {
172- var domain = getDomain ( '--include' ) ;
173160 // not profiling
174161 if ( ! id ) {
175- if ( ! domain || ! domain . miniprofiler_currentRequest ) return null ;
176- var extension = domain . miniprofiler_currentRequest . miniprofiler ;
177- id = extension . id ;
162+ return null ;
178163 }
179164 return includes . partial ( {
180165 path : resourcePath ,
@@ -197,7 +182,7 @@ function include(id) {
197182/*
198183 * Setup profiling. This function may only be called once, subsequent calls are ignored.
199184 *
200- * This must be called before the first call to startProfiling, but doesn't need to be called in the context of a domain .
185+ * This must be called before the first call to startProfiling.
201186 *
202187 * options is an optional object, which can have the following fields:
203188 * - storage: function(id[, json]) ; called to store (if json is present) or fetch (if json is omitted) a string JSON blob of profiling information
@@ -231,9 +216,6 @@ function getProfiling(id){
231216 * Descends recursively, but will terminate even if there are cycles in the object graph.
232217 *
233218 * Adds `miniprofiler_instrumented` to `toInstrument`, any contained objects, and any contained functions.
234- *
235- * Note that to use miniprofiler you *must* be creating a new domain per request, this is necessary for request tracking
236- * purposes.
237219 */
238220function addProfilingInstrumentation ( toInstrument ) {
239221 if ( ! toInstrument ) {
@@ -254,17 +236,13 @@ function addProfilingInstrumentation(toInstrument) {
254236
255237/*
256238 * Begins profiling the given request.
257- *
258- * Note that this call must occur in the context of the domain that will service this request.
259239 */
260240function startProfiling ( request , enabled ) {
261241 if ( ! configured ) throw new Error ( 'configure() must be called before the first call to startProfiling' ) ;
262242
263- var domain = getDomain ( '--startProfiling' ) ;
264- if ( ! domain ) return ;
265-
266- domain . miniprofiler_currentRequest = request ;
267- var currentRequestExtension = { } ;
243+ var currentRequestExtension = {
244+ enabled : enabled
245+ } ;
268246
269247 if ( enabled ) {
270248 currentRequestExtension . startDate = Date . now ( ) ;
@@ -278,11 +256,19 @@ function startProfiling(request, enabled) {
278256 var args = Array . prototype . slice . call ( arguments , enabled ? 0 : 3 ) ;
279257 if ( enabled ) {
280258 args . unshift ( currentRequestExtension ) ;
281- timeQueryExtension . apply ( this , args ) ;
259+ timeQuery . apply ( this , args ) ;
282260 } else {
283261 arguments [ 2 ] . apply ( this , args ) ;
284262 }
285263 } ;
264+ currentRequestExtension . step = function ( name , call ) {
265+ var args = Array . prototype . slice . call ( arguments , enabled ? 0 : 2 ) ;
266+ if ( enabled ) {
267+ step ( name , request , call ) ;
268+ } else {
269+ call ( ) ;
270+ }
271+ } ;
286272 currentRequestExtension . include = function ( ) {
287273 return enabled ? include ( currentRequestExtension . id ) : '' ;
288274 } ;
@@ -293,31 +279,23 @@ function startProfiling(request, enabled) {
293279
294280/*
295281 * Stops profiling the given request.
296- *
297- * Note that this call must occur in the context of the domain that services this request.
298282 */
299- function stopProfiling ( ) {
300- var domain = getDomain ( '--stopProfiling' ) ;
301-
283+ function stopProfiling ( request ) {
302284 // not profiling
303- if ( ! domain || ! domain . miniprofiler_currentRequest ) return null ;
304-
305- var extension = domain . miniprofiler_currentRequest . miniprofiler ;
285+ if ( ! request . miniprofiler . enabled ) return null ;
306286
287+ var extension = request . miniprofiler ;
307288 var time = process . hrtime ( ) ;
308-
309289 if ( extension . stepGraph . parent != null ) {
310290 throw new Error ( 'profiling ended while still in a function, was left in [' + extension . stepGraph . name + ']' ) ;
311291 }
312292
313293 extension . stopTime = time ;
314294 extension . stepGraph . stopTime = time ;
315295
316- var request = domain . miniprofiler_currentRequest ;
317-
318296 // get those references gone, we can't assume much about GC here
319- delete domain . miniprofiler_currentRequest . miniprofiler ;
320- delete domain . miniprofiler_currentRequest ;
297+ // (is the above comment still true? is this line needed? - mjibson)
298+ delete request . miniprofiler
321299
322300 var json = describePerformance ( extension , request ) ;
323301 var ret = extension . id ;
@@ -332,18 +310,10 @@ function stopProfiling(){
332310 *
333311 * You should only use this method directly in cases when calls to addProfiling won't suffice.
334312 */
335- function step ( name , call ) {
336- var domain = getDomain ( '--step: ' + name ) ;
337-
338- // Not profiling
339- if ( ! domain || ! domain . miniprofiler_currentRequest ) {
340- var ret = call ( ) ;
341- return ret ;
342- }
343-
313+ function step ( name , request , call ) {
344314 var time = process . hrtime ( ) ;
345315
346- var extension = domain . miniprofiler_currentRequest . miniprofiler ;
316+ var extension = request . miniprofiler ;
347317
348318 var newStep = makeStep ( name , time , extension . stepGraph ) ;
349319 extension . stepGraph . steps . push ( newStep ) ;
@@ -359,7 +329,7 @@ function step(name, call) {
359329 failed = true ;
360330 throw e ;
361331 } finally {
362- unstep ( name , failed ) ;
332+ unstep ( name , request , failed ) ;
363333 }
364334
365335 return result ;
@@ -377,19 +347,7 @@ function step(name, call) {
377347 * when the query has completed. Implicitly, any execution of a callback is considered
378348 * to have ended the query.
379349 */
380- function timeQuery ( type , query , executeFunction /*, params[] */ ) {
381- // Not profiling
382- if ( ! domain || ! domain . miniprofiler_currentRequest ) {
383- return executeFunction . apply ( this , params ) ;
384- }
385- var domain = getDomain ( '--timeQuery: ' + type ) ;
386- var extension = domain . miniprofiler_currentRequest . miniprofiler ;
387- var args = Array . prototype . slice . call ( arguments , 0 ) ;
388- args . unshift ( extension ) ;
389- timeQueryExtension . apply ( this , args ) ;
390- }
391-
392- function timeQueryExtension ( extension , type , query , executeFunction ) {
350+ function timeQuery ( extension , type , query , executeFunction ) {
393351 var time = process . hrtime ( ) ;
394352 var startDate = Date . now ( ) ;
395353
@@ -436,15 +394,10 @@ function htmlEscape(str) {
436394 . replace ( / > / g, '>' ) ;
437395}
438396
439- function unstep ( name , failed ) {
397+ function unstep ( name , request , failed ) {
440398 var time = process . hrtime ( ) ;
441399
442- var domain = getDomain ( '--unstep: ' + name ) ;
443-
444- // Not profiling
445- if ( ! domain || ! domain . miniprofiler_currentRequest ) return ;
446-
447- var extension = domain . miniprofiler_currentRequest . miniprofiler ;
400+ var extension = request . miniprofiler ;
448401
449402 if ( extension . stepGraph . name != name ) {
450403 throw new Error ( 'profiling stepped out of the wrong function, found [' + name + '] expected [' + extension . stepGraph . name + ']' ) ;
@@ -551,16 +504,6 @@ function describeCustomTimings(customTimings, root) {
551504 return ret ;
552505}
553506
554- function getDomain ( debugName ) {
555- if ( this instanceof domain . Domain ) {
556- return this ;
557- }
558-
559- if ( domain . active ) return domain . active ;
560-
561- return null ;
562- }
563-
564507function makeStep ( name , time , parent ) {
565508 return { name : name , startTime : time , stopTime : null , parent : parent , steps : [ ] , customTimings : { } } ;
566509}
0 commit comments