Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

The Blackfire CLI Tool for AJAX Requests

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We know that the probe - that's the Blackfire PHP extension - doesn't run on every single request: it only runs when it detects that our browser extension is telling it to run.

There's actually a second way that you can tell the probe to do its work. It's with a super handy command-line tool.

Installing the Blackfire CLI Tool

Go back to the Blackfire site, click on their docs... and once again find the installation page. When we went through this earlier, we purposely skipped one step: installing the Blackfire CLI tool. Actually, Blackfire recently updated this page... and I like the newer version a lot better. In both versions of the docs - the new one and the old one you see here - if you followed the commands to install the "agent" then you've already also installed the CLI tool. Nice!

To make sure, find your terminal and try running:

blackfire version

Blackfire CLI Confiug: Client ID & Token

Got it! Before using this, we do need to add a little bit of configuration by running a blackfire config command. On the old version of the docs, I'll copy the "client ID": I'll need that in a second. On the newer version of the docs, you'll be able to copy a blackfire config command that already includes the client id and client token. For me, I'll run

blackfire config

If your version of the command has the --client-id and --client-token options already, you're done! If not, like me, paste in the Client Id... then also copy and paste in the token.

The client id and token work... almost like a username and password to your Blackfire account. When we use the browser extension, we're logged into Blackfire in the browser. When we click profile, the Blackfire API is able to give the extension some credentials that it passes to the probe to prove that we're allowed to profile this page.

When you use the Blackfire command line tool to profile something... the client id and client token are used to talk to the Blackfire API and get those same credentials that it then passes to the probe to prove we're authorized to profile. They basically identify & prove which user we are on Blackfire.

Profiling AJAX Requests

The Blackfire CLI tool has two superpowers. The first is that you can run blackfire curl and then pass a URL to any page on your site that you want to create a profile for. Now... that might seem totally worthless. After all... if we want to profile a page... isn't it easier just to go to that page in our browser and use the extension to profile it?

Yep! Unless... you can't easily "go" to that page - like if you want to profile an AJAX request or an API endpoint. Check this out: I'll open up the dev tools, go to the "Network" section and refresh. Notice I'm already filtered to XHR requests - so the /api/github-organization AJAX request pops up. Want to easily profile just that request? Right click on it and select "Copy as cURL".

Now head back to your terminal and paste. Cool, right? It creates a full curl command that you can use to make that same request... including any session cookies, which means this request will be authenticated as the same user you're logged in as in the browser. We can use this with Blackfire: say blackfire then paste!

Try it! It's profiling and using the same process as the browser: making 10 requests and profiling each one. This is my favorite way to profile AJAX requests. When it finishes, it gives us the URL to the call graph and some basic stats below. Go open that profile: http://bit.ly/sf-bf-curl!

It works! Use that to easily profile any AJAX requests you want to.

So what is the second superpower of the CLI tool? It's actually its main superpower: the ability to profile command-line scripts. Let's do that next.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial can be used to learn how to profile any app - including Symfony 5.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "blackfire/php-sdk": "^1.20", // v1.20.0
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/annotations": "^1.0", // v1.8.0
        "doctrine/doctrine-bundle": "^1.6.10|^2.0", // 1.11.2
        "doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
        "doctrine/orm": "^2.5.11", // v2.6.4
        "phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.2
        "sensio/framework-extra-bundle": "^5.4", // v5.5.1
        "symfony/console": "4.3.*", // v4.3.10
        "symfony/dotenv": "4.3.*", // v4.3.10
        "symfony/flex": "^1.9", // v1.18.7
        "symfony/form": "4.3.*", // v4.3.10
        "symfony/framework-bundle": "4.3.*", // v4.3.9
        "symfony/http-client": "4.3.*", // v4.3.10
        "symfony/property-access": "4.3.*", // v4.3.10
        "symfony/property-info": "4.3.*", // v4.3.10
        "symfony/security-bundle": "4.3.*", // v4.3.10
        "symfony/serializer": "4.3.*", // v4.3.10
        "symfony/twig-bundle": "4.3.*", // v4.3.10
        "symfony/validator": "4.3.*", // v4.3.10
        "symfony/webpack-encore-bundle": "^1.6", // v1.7.2
        "symfony/yaml": "4.3.*", // v4.3.10
        "twig/extensions": "^1.5" // v1.5.4
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.2", // 3.2.2
        "easycorp/easy-log-handler": "^1.0.7", // v1.0.9
        "fzaninotto/faker": "^1.8", // v1.8.0
        "symfony/browser-kit": "4.3.*", // v4.3.10
        "symfony/css-selector": "4.3.*", // v4.3.10
        "symfony/debug-bundle": "4.3.*", // v4.3.10
        "symfony/maker-bundle": "^1.13", // v1.14.3
        "symfony/monolog-bundle": "^3.0", // v3.5.0
        "symfony/phpunit-bridge": "^5.0", // v5.0.3
        "symfony/stopwatch": "4.3.*", // v4.3.10
        "symfony/var-dumper": "4.3.*", // v4.3.10
        "symfony/web-profiler-bundle": "4.3.*" // v4.3.10
    }
}
userVoice