Not for the first time a Perl POE daemon in our development system seemed to be taking a long time on certain requests. It is a large amount of code and although we had our suspicions we've learnt the lesson now - Devel::NYTProf is so simple to use we had no hesitation in running it on this daemon. Instead of:
you simply run:
then a nytprof.out file is created and then you can generate the HTML report and view it with:
Unsuprisingly, our suspicions were confirmed. The daemon issues an HTTP POST request retrieving a large amount of JSON encoded data. In this case the first element in the JSON needs to be extracted and compared with an argument passed in the client request to ensure the client can have the JSON data (your probably saying why not do it further down the line in the code handling the HTTP POST but just take my word we cannot right now) and then that element needs to be removed. The JSON is decoded into a Perl arrayref, the first element is examined, checked and finally deleted from the array before encoding it again and passing it back to the client. As the JSON data was large the decoding into a Perl array and back into JSON was consuming a lot of time. The JSON encoding is fairly simple, especially for an array of strings and numbers and looks like this:
Since JSON encoding is straight forward and we know the first element is a string and it is that one we want to extract it was far more simple to do:
The time dropped by over 90%.
Surprisingly (for us) although the above was a wasteful time consumer it was not the main one. Most of the time is lost in HTTP::Message::add_content:
I can't for the life of me see why a simple ref call on a hash would take 320s even for 7585 calls. We are not using LWP::UserAgent::POE just plain LWP::UserAgent - in fact the former is not even installed. I've not got to the bottom of this yet. Perhaps you can see something I've missed?
I've posted this to the Devel::NYTProf list as well if you are interested in following it.
Comments
where the blame currently lies
After a good deal of work trying to narrow this down it appears it is something to do with taint mode and I'll blog about it separately.