debugging long running PHP script

后端 未结 11 2396
我寻月下人不归
我寻月下人不归 2021-02-19 06:46

I have php script running as a cron job, extensively using third party code. Script itself has a few thousands LOC. Basically it\'s the data import / treatment script. (JSON to

11条回答
  •  没有蜡笔的小新
    2021-02-19 07:10

    I've run into strange slowdowns when doing network heavy efforts in the past. Basically, what I found was that during manual testing the system was very fast but when left to run unattended it would not get as much done as I had hoped.

    In my case the issue I found was that I had default network timeouts in place and many web requests would simply time out.

    In general, though not an external tool, you can use the difference between two microtime(TRUE) requests to time sections of code. To keep the logging small set a flag limit and only test the time if the flag has not been decremented down to zero after reducing for each such event. You can have individual flags for individual code segments or even for different time limits within a code segment.

    $flag['name'] = 10;  // How many times to fire
    $slow['name'] = 0.5; // How long in seconds before it's a problem?
    
    $start = microtime(TRUE);
    do_something($parameters);
    $used  = microtime(TRUE) - $start;
    if ( $flag['name'] && used >= $slow['name'] )
    {
      logit($parameters);
      $flag['name']--;
    }
    

    If you output what URL, or other data/event took to long to process, then you can dig into that particular item later to see if you can find out how it is causing trouble in your code.

    Of course, this assumes that individual items are causing your problem and not simply a general slowdown over time.

    EDIT:

    I (now) see it's a production server. This makes editing the code less enjoyable. You'd probably want to make integrating with the code a minimal process having the testing logic and possibly supported tags/flags and quantities in an external file.

    setStart('flagname');
    // Do stuff to be checked for speed here
    setStop('flagname',$moredata);
    

    For maximum robustness the methods/functions would have to ensure they handled unknown tags, missing parameters, and so forth.

提交回复
热议问题