async-profile icon indicating copy to clipboard operation
async-profile copied to clipboard

Question: How to use it in the context of a webserver like Express.js

Open roundrobin opened this issue 11 years ago • 5 comments

I tried using async-profile within an endpoint of an express app, but I can't get it to work. Its immediately returning of making a instance an within the endpoint. Here a stripped down demo of my code:

//controller.js

var AsyncProfile = require('async-profile');

function someEndpoint(req, res){

    var profiler = new AsyncProfile({
        callback: function(results){
            console.log("error", "CPU profiling", results);
        }
    });

   //Its return immediately after the instantiation 

   dosomething(function returnCallback(){

    profiler.stop();
    //Expected it to stop here
 });



}

roundrobin avatar Oct 24 '14 23:10 roundrobin

Hey @roundrobin, that should just work :/. Can you link me to the actual code?

ConradIrwin avatar Oct 27 '14 01:10 ConradIrwin

is it able to print all the stacks of code, for example if i use MVC in express, the stack print should be "Controller","Service","DB" ?

clevertension avatar Nov 27 '14 00:11 clevertension

@clevertension In theory, yes. You'd have to modify the code, or use your own output printer though.

ConradIrwin avatar Dec 03 '14 18:12 ConradIrwin

@roundrobin's code works correctly for me, however if I use request, the profile terminates prematurely.

You can run this and see:

var AsyncProfile = require('async-profile'),
    request = require("request");

function someEndpoint() {

    var profiler = new AsyncProfile({
        callback: function(results) {
            results.print();
        }
    });

    request("http://google.com", function() {
        // profiler stops right before this callback.
        console.log("loaded google");
    });

    // wait 5 seconds and then stop
    setTimeout(function returnCallback() {
        console.log("really stop");
        profiler.stop();
    }, 5000);

}
someEndpoint();

giannif avatar Jan 24 '15 05:01 giannif

That's probably because request() isn't preserving domains across async calls. This can probably be patched in a similar manner to redis/mongo etc. in the current code.

ConradIrwin avatar Jan 24 '15 06:01 ConradIrwin