# Line profiler for PHP

- Thread: https://externals.io/message/79895
- Original archive: https://news-web.php.net/php.internals/79895
- Started by Jacob Bednarz on 2014-12-23T21:28:41+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #79895 — Jacob Bednarz — 2014-12-23T21:28:41+00:00

URL: https://externals.io/message/79895#79895

Hey,
I have been working on some profiling of an application recently and whilst xhprof and xdebug are good to get an overall picture to further break into, I haven’t been able to find a tool for line by line profiling in PHP for a particular file. I have done a bit of research and found rblineprof (https://github.com/tmm1/rblineprof) which is a line profiler for Ruby and I would like to achieve this in PHP. 

The questions I have are:
- Is this even possible?
- Are there any projects out there or being worked on that achieve this?
- If nothing exists, I am happy to look into this however I will need some guidance on best practices, caveats with profiling PHP and ways of parsing the file correctly.

Thanks!

---

## #79896 — Stanislav Malyshev — 2014-12-23T21:59:00+00:00

URL: https://externals.io/message/79895#79896 · In reply to: #79895

Hi!

> The questions I have are: - Is this even possible?

Yes, should be possible but:
1. If you want precision, it would slow down your code a lot, as
basically you need to record timing of each opcode, which can be very
expensive.
2. If you're ok with less precision, you can do sampling, but in that
case you probably need your script to spend a lot of time in the same
areas of the code to have any meaningful results, otherwise it'd be just
random.

> - Are there any projects out there or being worked on that achieve
> this?

Most of the profilers afaik go by functions. Most probably because
line-level profiling would be so intrusive as to not produce really
valid results, but maybe it can be done in a way that is not that
intrusive. Stackoverflow question
http://stackoverflow.com/q/1113034/214196 has the link to some profiling
tool that seems to do line-level, but from the fact they mention PHP 4
support I'm not sure how up-to-date it is.

> - If nothing exists, I am happy to look into this however I will need
> some guidance on best practices, caveats with profiling PHP and ways
> of parsing the file correctly.

Not sure what you mean by "parsing the file correctly" - PHP engine does
the parsing, profiler shouldn't do any parsing.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #79897 — Nikita Popov — 2014-12-23T22:13:51+00:00

URL: https://externals.io/message/79895#79897 · In reply to: #79895

On Tue, Dec 23, 2014 at 10:28 PM, Jacob Bednarz <jacob.bednarz@gmail.com>
wrote:

> Hey,
> I have been working on some profiling of an application recently and
> whilst xhprof and xdebug are good to get an overall picture to further
> break into, I haven’t been able to find a tool for line by line profiling
> in PHP for a particular file. I have done a bit of research and found
> rblineprof (https://github.com/tmm1/rblineprof) which is a line profiler
> for Ruby and I would like to achieve this in PHP.
>
> The questions I have are:
> - Is this even possible?
> - Are there any projects out there or being worked on that achieve this?
> - If nothing exists, I am happy to look into this however I will need some
> guidance on best practices, caveats with profiling PHP and ways of parsing
> the file correctly.
>
> Thanks!


I've implemented a basic sampling profiler with line-level resolution some
time ago: https://github.com/nikic/sample_prof

Didn't use it since then, but maybe it still works.

Nikita

---

## #79898 — Jacob Bednarz — 2014-12-23T22:34:02+00:00

URL: https://externals.io/message/79895#79898 · In reply to: #79897

>
> 1. If you want precision, it would slow down your code a lot, as
> basically you need to record timing of each opcode, which can be very
> expensive.


I'm not too concerned about the cost as this would only be running locally
and the for the purpose of profiling. The proviso with this would be that
the costs associated didn't reflect in the line profile time and skew the
results.

Stackoverflow question http://stackoverflow.com/q/1113034/214196 has
> the link to some profiling tool that seems to do line-level, but from the
> fact
> they mention PHP 4 support I'm not sure how up-to-date it is.


I will be sure to check it out - thanks.


> Not sure what you mean by "parsing the file correctly" - PHP engine does
> the parsing, profiler shouldn't do any parsing.


By this I mean I have looked at implementing it a couple of different ways
and each time I get caught up on timing code that spans multiple lines such
as:

$array = array(
    'my_key' => 'my_value',
);

Where I want to measure the variable assignment however because it spans
multiple lines I can't work out the best way to include it all. 'Parsing'
is probably the incorrect term here but it's all that came to mind when
writing this :p

I've implemented a basic sampling profiler with line-level resolution some
> time ago: https://github.com/nikic/sample_prof


Thanks a bunch! I will check it out and see if I can get it working for
what I am after.

---

## #79899 — Stanislav Malyshev — 2014-12-23T22:40:40+00:00

URL: https://externals.io/message/79895#79899 · In reply to: #79898

Hi!

> By this I mean I have looked at implementing it a couple of different ways
> and each time I get caught up on timing code that spans multiple lines such
> as:
> 
> $array = array(
>     'my_key' => 'my_value',
> );

I would ignore that if I were you and use whatever lines the engine
assigns to specific opcodes. There's nothing you can optimize between
array( and 'my_key', so for all practical purposes it does not really
matter to which line of these the code is assigned.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #79919 — Derick Rethans — 2014-12-24T11:16:53+00:00

URL: https://externals.io/message/79895#79919 · In reply to: #79896

> > […]
> 
> Yes, should be possible but:
> 1. If you want precision, it would slow down your code a lot, as
> basically you need to record timing of each opcode, which can be very
> expensive.
> 2. If you're ok with less precision, you can do sampling, but in that
> case you probably need your script to spend a lot of time in the same
> areas of the code to have any meaningful results, otherwise it'd be just
> random.
> 
> > […]
> 
> Most of the profilers afaik go by functions. Most probably because
> line-level profiling would be so intrusive as to not produce really
> valid results, but maybe it can be done in a way that is not that
> intrusive.

I have played with it for Xdebug, but it was too much of a slow down to 
be of any use...

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

---

## #79963 — Crocodile — 2014-12-27T11:41:06+00:00

URL: https://externals.io/message/79895#79963 · In reply to: #79919

Hello Jacob

Take a look at http://pinba.org/ . While this is probably not exactly (or
may be even not at all) the thing you asked for, it is definitely
interesting. It allows for some profiling *right on production. *There are
certain things that you only experience under heavy load, and this tool may
let you know what's happening.

Cheers,
Victor


2014-12-24 14:16 GMT+03:00 Derick Rethans <derick@php.net>:

> 
>
> > […]
>
> I have played with it for Xdebug, but it was too much of a slow down to
> be of any use...
>
> cheers,
> Derick
>
> --
> http://derickrethans.nl | http://xdebug.org
> Like Xdebug? Consider a donation: http://xdebug.org/donate.php
> twitter: @derickr and @xdebug
> Posted with an email client that doesn't mangle email: alpine
