Question: How to use it in the context of a webserver like Express.js
Opened this issue · 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
});
}
Hey @roundrobin, that should just work :/. Can you link me to the actual code?
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 In theory, yes. You'd have to modify the code, or use your own output printer though.
@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();
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.