Thursday, August 28, 2014

profiling Catalyst applications with Devel::NYTProf

While attempting to profile a Catalyst application with Devel::NYTProf, I kept getting errors when processing the nytprof.out file because the file was not closed properly.

The solution from http://fkumro.blogspot.ro/2011/11/using-nytprof-with-catalyst.html did not work, kept getting "panic at nytprofcalls line 191" messages.

What worked was starting with

perl -d:NYTProf scripts/myapp_server.pl

adding a

sub stop :Path {
    exit(0);
}

in the Root.pm and loading that in  the browser to stop the server.

2 comments:

  1. That ends up giving you all sorts of profiling information about Catalyst itself rather than just your application. And, depending on your app, the Catalyst profiling can obscure the bits of your app that you really want to profile. What we ended up doing was starting the app with profiling turned off and then using special routes to start/stop profiling:

    NYTPROF="start=no" perl -d:NYTProf scripts/myapp_server.pl

    And our routes look like:

    sub start :Path('start_profiling') :Args(0) {
    my ( $self, $c ) = @_;
    DB::enable_profile('nytprof.out');
    my $stop_link = $c->uri_for('stop_profiling');
    $c->response->body("Profiling started. Profiling info will be written until you visit $stop_link");
    }

    sub stop :Path('stop_profiling') :Args(0) {
    my ( $self, $c ) = @_;
    DB::finish_profile();
    $c->response->body('Profiling stopped');
    }

    Then we could navigate within the app to the place that we want to profile, use curl (or another browser or whatever) to start profiling by going to http://localhost:3000/start_profiling, do whatever actions in the app that we want to profile (like maybe submit a POST request or click through several links or whatever), then go to http://localhost:3000/stop_profiling when we're done. At that point we'd have an nytprof.out file that we can run nytprofhtml against to view the profile.

    We thought about making a Catalyst plugin that would take care of all of these details (maybe put some links on every page to starting/stoping profiling) and automatically run nytprofhtml and present the results via special routes within the app, but we had too many other things to work on and the few manual steps we need to do aren't too much work.


    ReplyDelete