WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.046 --> 00:00:03.476 align:middle
Profiling a page looks like this.

00:00:03.516 --> 00:00:09.766 align:middle
First, something tells the Blackfire
PHP extension - the "Probe": Hey!

00:00:10.026 --> 00:00:11.506 align:middle
Start profiling!

00:00:11.776 --> 00:00:15.566 align:middle
Which basically means that it
starts collecting tons of data.

00:00:16.256 --> 00:00:20.176 align:middle
The process of collecting data
is called instrumentation...

00:00:20.416 --> 00:00:28.016 align:middle
because when a concept is too simple, sometimes
we tech people like to invent confusing words.

00:00:28.496 --> 00:00:33.306 align:middle
Instrumentation means that the
PHP extension is collecting data.

00:00:34.406 --> 00:00:40.506 align:middle
The second step is that - eventually -
something tells the PHP extension to stop

00:00:40.506 --> 00:00:43.206 align:middle
"instrumentation" and to send the data.

00:00:43.956 --> 00:00:47.616 align:middle
The collection of data is known as a "profile".

00:00:47.616 --> 00:00:54.656 align:middle
The PHP extension sends the profile to the
agent, which aggregates it, prune some stuff

00:00:54.656 --> 00:00:57.406 align:middle
and ultimately sends it to the Blackfire server.

00:00:57.506 --> 00:01:03.396 align:middle
So: what is the "thing" that tells
the PHP extension to activate?

00:01:04.316 --> 00:01:08.026 align:middle
We know that the PHP extension
doesn't profile every request...

00:01:08.186 --> 00:01:15.036 align:middle
so what is it that says: Hey PHP
extension "probe" thing: start profiling!

00:01:15.796 --> 00:01:22.456 align:middle
The answer - so far - is: the browser
extension: it sends special information

00:01:22.456 --> 00:01:25.176 align:middle
that tells the probe to do its thing.

00:01:25.706 --> 00:01:30.316 align:middle
Or, if you use the blackfire command
line utility, which we did earlier

00:01:30.316 --> 00:01:35.516 align:middle
to profile a command, then it is what
tells the PHP extension to activate.

00:01:36.366 --> 00:01:43.816 align:middle
In either situation, the extension is activated
before even the first line of code is executed.

00:01:44.396 --> 00:01:48.696 align:middle
That means that every single line
of PHP code is "instrumented":

00:01:49.496 --> 00:01:52.266 align:middle
our final profile contains everything.

00:01:52.966 --> 00:01:59.156 align:middle
This is called auto-instrumentation:
instrumentation starts automatically.

00:01:59.906 --> 00:02:03.856 align:middle
This naturally leads to three
interesting questions.

00:02:03.856 --> 00:02:07.176 align:middle
First, who is baby Yoda?

00:02:07.746 --> 00:02:08.616 align:middle
I mean, is he...

00:02:08.916 --> 00:02:10.656 align:middle
like, related to Yoda?

00:02:10.656 --> 00:02:12.626 align:middle
Or just the same species?

00:02:13.436 --> 00:02:19.936 align:middle
The second question is: could we trigger, or
create a Blackfire profile in a different way?

00:02:20.566 --> 00:02:26.926 align:middle
Could we, for example, dynamically tell
the PHP extension to create a profile

00:02:26.926 --> 00:02:30.956 align:middle
from inside our code under
some specific condition?

00:02:31.766 --> 00:02:37.296 align:middle
And third, regardless of who triggers
the profile, could we "zoom in"

00:02:37.496 --> 00:02:41.036 align:middle
and only collect profiling
data for part of our code?

00:02:41.666 --> 00:02:46.096 align:middle
Like, could we create a profile that
only collects data about the code

00:02:46.096 --> 00:02:49.436 align:middle
from our controller instead
of the entire request?

00:02:50.156 --> 00:02:54.786 align:middle
Let's actually start with that last
question: profiling a specific part

00:02:54.786 --> 00:02:57.016 align:middle
of our code, instead of the whole thing.

00:02:57.816 --> 00:03:03.156 align:middle
To be fully honest, I don't know if this
part has a ton of practical use-cases,

00:03:03.466 --> 00:03:08.536 align:middle
but it will give you an even better idea
of how Blackfire works behind the scenes.

00:03:09.546 --> 00:03:15.986 align:middle
To help with this crazy experiment, we're
going to install Blackfire's PHP SDK.

00:03:15.986 --> 00:03:21.136 align:middle
Find your terminal, dial up your
modem to the Internet, and run:

00:03:21.586 --> 00:03:30.456 align:middle
composer require blackfire/php-sdk This is a
normal PHP library that helps interact directly

00:03:30.456 --> 00:03:33.326 align:middle
with Blackfire from inside your code.

00:03:33.686 --> 00:03:34.706 align:middle
You'll see how.

00:03:35.986 --> 00:03:43.116 align:middle
When it finishes, move over and open
src/Controller/MainController.php.

00:03:43.116 --> 00:03:46.126 align:middle
Ok: this is the controller for our homepage.

00:03:46.996 --> 00:03:49.726 align:middle
Let's pretend that when we profile this page,

00:03:50.056 --> 00:03:53.476 align:middle
we don't want to collect
data about all of our code.

00:03:54.386 --> 00:04:00.816 align:middle
Nope, we want to, sort of, "zoom in" and see
only what's happening inside the controller.

00:04:01.756 --> 00:04:08.126 align:middle
We can do that by saying $probe =
\BlackfireProbe::getMainInstance().

00:04:08.966 --> 00:04:12.616 align:middle
Remember: the PHP extension
is called the "probe"...

00:04:12.616 --> 00:04:15.576 align:middle
that's important if you want this to make sense.

00:04:16.416 --> 00:04:18.816 align:middle
Then call $probe-&gt;enable().

00:04:20.026 --> 00:04:27.996 align:middle
At the bottom, I'll set the rendered template
to a $response variable, add $probe-&gt;disable()

00:04:27.996 --> 00:04:31.056 align:middle
and finish with return $response.

00:04:32.556 --> 00:04:33.596 align:middle
Okay, so...

00:04:33.646 --> 00:04:35.346 align:middle
what the heck does this do?

00:04:36.476 --> 00:04:42.266 align:middle
The first thing I want you to notice is that
if I refresh the homepage a bunch of times...

00:04:44.226 --> 00:04:52.606 align:middle
and then go to https://blackfire.io,
I do not have any new profiles.

00:04:53.356 --> 00:04:58.506 align:middle
Adding this code does not "trigger"
a new profile to be created:

00:04:58.916 --> 00:05:04.456 align:middle
it does not tell the PHP extension - the
"probe" - that it should to do its work.

00:05:05.566 --> 00:05:09.536 align:middle
Instead, if a profile is
currently being created,

00:05:09.976 --> 00:05:13.176 align:middle
this tells the probe when
to start collecting data.

00:05:14.226 --> 00:05:18.746 align:middle
Hmm, this isn't going to quite make
sense until we see it in action.

00:05:19.356 --> 00:05:21.576 align:middle
Trigger a new profile on the homepage.

00:05:22.836 --> 00:05:26.466 align:middle
I'll call this one: [Recording]
Only instrument some code.

00:05:28.356 --> 00:05:36.336 align:middle
Click to view the call graph:
https://bit.ly/sf-bf-partial-profile.

00:05:36.336 --> 00:05:36.906 align:middle
Fascinating.

00:05:36.906 --> 00:05:40.256 align:middle
This contains less information than normal.

00:05:40.556 --> 00:05:42.636 align:middle
It has a few things on top
- main() and handleRaw()...

00:05:42.636 --> 00:05:45.656 align:middle
but basically it jumps straight
to the homepage() method.

00:05:46.466 --> 00:05:51.146 align:middle
What's happening here is that the only
code that the probe "instrumented",

00:05:51.436 --> 00:05:55.076 align:middle
the only code that it collected information on,

00:05:55.206 --> 00:05:59.026 align:middle
is the code between the enable()
and disable() calls.

00:05:59.796 --> 00:06:04.146 align:middle
This... completely confused
me the first time I saw it.

00:06:04.796 --> 00:06:11.376 align:middle
What really happens is this: as soon as we
use the browser extension to tell the probe

00:06:11.376 --> 00:06:15.106 align:middle
to do its job, the PHP extension
starts instrumenting -

00:06:15.326 --> 00:06:17.856 align:middle
so, collection data - immediately.

00:06:18.646 --> 00:06:23.656 align:middle
Initially, it is collecting data
about every line of PHP code.

00:06:24.066 --> 00:06:29.676 align:middle
But as soon as it sees $probe-&gt;enable(),
it basically forgets

00:06:29.676 --> 00:06:32.016 align:middle
about all the data collected so far.

00:06:33.106 --> 00:06:36.056 align:middle
The $probe-&gt;enable() call says: Hey!

00:06:36.296 --> 00:06:38.396 align:middle
Start instrumenting here.

00:06:39.356 --> 00:06:45.796 align:middle
If you've already collected some data before
thanks to auto-instrumentation, get rid of it.

00:06:45.796 --> 00:06:51.286 align:middle
This effectively disables
auto-instrumentation: we're now controlling

00:06:51.506 --> 00:06:55.886 align:middle
which code is instrumented instead
of it happening automatically.

00:06:56.996 --> 00:07:00.686 align:middle
Once the code hits $probe-&gt;disable()
instrumentation stops.

00:07:00.686 --> 00:07:07.666 align:middle
You can actually use $probe-&gt;enable() and
$probe-&gt;disable() multiple times in your code

00:07:07.866 --> 00:07:09.946 align:middle
if you want to profile different pieces:

00:07:09.946 --> 00:07:16.336 align:middle
$probe-&gt;enable() only forgets data it's
already collected the first time you call it.

00:07:16.336 --> 00:07:20.786 align:middle
Oh, and you can also optionally
call $probe-&gt;close() -

00:07:21.096 --> 00:07:22.886 align:middle
you'll see this in their documentation.

00:07:23.446 --> 00:07:27.666 align:middle
That tells the PHP extension that
you're definitely done profiling

00:07:28.146 --> 00:07:30.706 align:middle
and it can send the data to the agent.

00:07:31.116 --> 00:07:35.716 align:middle
But, it's not strictly required,
because it'll be sent automatically

00:07:35.816 --> 00:07:37.456 align:middle
when the script ends anyways.

00:07:38.346 --> 00:07:41.726 align:middle
So... this feature is maybe useful...

00:07:41.826 --> 00:07:47.406 align:middle
but it's definitely a nice intro into taking
more control of the profiling process.

00:07:47.906 --> 00:07:49.216 align:middle
And.. fun fact!

00:07:49.506 --> 00:07:53.166 align:middle
We installed the blackfire/php-sdk library...

00:07:53.266 --> 00:07:56.106 align:middle
but we haven't actually used it yet!

00:07:56.846 --> 00:08:01.666 align:middle
This \BlackfireProbe class is
not from the php-sdk library:

00:08:02.116 --> 00:08:05.556 align:middle
it's from the Blackfire PHP extension.

00:08:06.446 --> 00:08:10.186 align:middle
As long as you have the extension
installed, that class will exist.

00:08:10.996 --> 00:08:13.756 align:middle
We're interacting directly with the extension.

00:08:14.426 --> 00:08:19.016 align:middle
So... why did we install the
SDK if we didn't need it?

00:08:19.806 --> 00:08:20.746 align:middle
Because...

00:08:20.966 --> 00:08:24.046 align:middle
it gave us auto-complete on that class.

00:08:24.406 --> 00:08:27.556 align:middle
And you all know that I freakin'
love auto-complete.

00:08:28.166 --> 00:08:32.416 align:middle
The SDK has a, sort of, "stub"
version of this class.

00:08:32.996 --> 00:08:37.846 align:middle
This is not the code that was actually
executed when we called those methods...

00:08:38.146 --> 00:08:43.286 align:middle
but having this at least shows us
what methods and arguments exist.

00:08:44.696 --> 00:08:50.626 align:middle
Next, let's actually use the PHP SDK
to do something a bit more interesting.

00:08:51.256 --> 00:08:55.576 align:middle
I want to create a profile
automatically in my code

00:08:55.786 --> 00:08:58.576 align:middle
without needing to use the browser extension.

00:08:59.266 --> 00:09:02.266 align:middle
This does have real-world use-cases.

