How to profile a PHP shell script app or worker using Blackfire
Asked Answered
C

2

3

I noticed that when I have an endless worker I cannot profile PHP shell scripts. Because when it's killed it doesn't send the probe.

What changes shall I do?

Consistent answered 4/6, 2015 at 13:49 Comment(0)
C
3

When you are trying to profile a worker which is running an endless loop. In this case you have to manually edit your code to either remove the endless loop or instrument your code to manually call the close() method of the probe (https://blackfire.io/doc/manual-instrumentation).

That's because the data is sent to the agent only when the close() method is called (it is called automatically at the end of the program unless you killed it).

You can manually instrument some code by using the BlackfireProbe class that comes bundled with the Blackfire's probe:

// Get the probe main instance
$probe = BlackfireProbe::getMainInstance();
// start profiling the code
$probe->enable();

// Calling close() instead of disable() stops the profiling and forces the  collected data to be sent to Blackfire:

// stop the profiling
// send the result to Blackfire
$probe->close();

As with auto-instrumentation, profiling is only active when the code is run through the Companion or the blackfire CLI utility. If not, all calls are converted to noops.

Consistent answered 4/6, 2015 at 13:49 Comment(1)
Let's hope that page always exists, otherwise you should probably include the code/text that's on that page, unless you're the site's "owner" ;-) You know how Stack feels about link only's.Pericycle
H
0

I don't know, maybe in 2015 following page did not exist, but now you can do profiling in following way: https://blackfire.io/docs/24-days/17-php-sdk

$blackfire = new LoopClient(new Client(), 10);
$blackfire->setSignal(SIGUSR1);
$blackfire->attachReference(7);
$blackfire->promoteReferenceSignal(SIGUSR2);

for (;;) {
    $blackfire->startLoop($profileConfig);

    consume();

    $blackfire->endLoop();

    usleep(400000);
}

Now you can send signal SIGUSR1 to process of this worker and LoopClient will start profiling. It'll listen 10 iterations of method consume and send last probe. After that it'll stop profiling.

Heterogony answered 7/6, 2018 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.