arnaud-lb / php-memory-profiler

Memory profiler for PHP. Helps finding memory leaks in PHP scripts.
MIT License
870 stars 51 forks source link

Add Stub File For IDE Autocomplete #30

Open mjourard opened 5 years ago

mjourard commented 5 years ago

It would be great if there was a stubfile such as https://github.com/JetBrains/phpstorm-stubs/blob/master/redis/Redis.php

This would make using the extension much more user-friendly

mjourard commented 5 years ago

Something like this:

<?php
/**
 * Helper autocomplete for php memprof extension
 */

/**
 * Returns whether memprof is enabled.
 * @return bool
 */
function memprof_enabled() {}

/**
 * Enables memprof and start tracking memory allocations. Note: any memory allocation made before this call is ignored.
 *
 */
function memprof_enable() {}

/**
 * Disables memprof and forget previous allocations.
 *
 */
function memprof_disable() {}

/**
 * The memprof_dump_callgrind function dumps the current memory usage to a stream in callgrind format. The file can then be read with tools such as KCacheGrind or QCacheGrind.
 *
 * Usage: memprof_dump_callgrind(fopen("output", "w"));
 */
function memprof_dump_callgrind(resource $stream) {}

/**
 * The memprof_dump_pprof function dumps the current memory usage to a stream in pprof format.
 * Usage: memprof_dump_pprof(fopen("profile.heap", "w"));
 * The file can be visualized using google-perftools's pprof tool.
 *
 * Display annotated call-graph in web browser or in gv:
 *
 * $ pprof --web profile.heap
 * $ # or:
 * $ pprof --gv profile.heap
 * pprof call-graph screenshot
 *
 * Output one line per function, sorted by own memory usage:
 *
 * $ pprof --text profile.heap
 */
function memprof_dump_pprof(resource $stream) {}

/**
 * Usage: $dump = memprof_dump_array();
 * The dump exposes the following information:
 *
 * Inclusive and exclusive memory usage of functions (counting only the memory that has is still in use when memprof_dump_array is called)
 * Inclusive and exclusive blocks count of functions (number of allocated; counting only the blocks that are still in use when memprof_dump_array is called)
 * The data is presented in call stacks. This way, if a function is called from multiple places, it is possible to see which call path caused it to leak the most memory
 * Example output:
 *
 * Array
 * (
 *   [memory_size] => 11578
 *   [blocks_count] => 236
 *   [memory_size_inclusive] => 10497691
 *   [blocks_count_inclusive] => 244
 *   [calls] => 1
 *   [called_functions] => Array
 *     (
 *       [main] => Array
 *         (
 *           [memory_size] => 288
 *           [blocks_count] => 3
 *           [memory_size_inclusive] => 10486113
 *           [blocks_count_inclusive] => 8
 *           [calls] => 1
 *           [called_functions] => Array
 *             (
 *               [a] => Array
 *                 (
 *                   [memory_size] => 4
 *                   [blocks_count] => 1
 *                   [memory_size_inclusive] => 10485825
 *                   [blocks_count_inclusive] => 5
 *                   [calls] => 1
 *                   [called_functions] => Array
 *                     (
 *                       [b] => Array
 *                         (
 *                           [memory_size] => 10485821
 *                           [blocks_count] => 4
 *                           [memory_size_inclusive] => 10485821
 *                           [blocks_count_inclusive] => 4
 *                           [calls] => 1
 *                           [called_functions] => Array
 *                             (
 *                               [str_repeat] => Array
 *                                 (
 *                                   [memory_size] => 0
 *                                   [blocks_count] => 0
 *                                   [memory_size_inclusive] => 0
 *                                   [blocks_count_inclusive] => 0
 *                                   [calls] => 1
 *                                   [called_functions] => Array
 *                                     (
 *                                     )
 *                                 )
 *                             )
 *                         )
 *                     )
 *                 )
 *               [memprof_dump_array] => Array
 *                 (
 *                   [memory_size] => 0
 *                   [blocks_count] => 0
 *                   [memory_size_inclusive] => 0
 *                   [blocks_count_inclusive] => 0
 *                   [calls] => 1
 *                   [called_functions] => Array
 *                     (
 *                     )
 *                 )
 *             )
 *         )
 *     )
 * )
 *
 * @return array
 */
function memprof_dump_array() {}
arnaud-lb commented 5 years ago

Good idea. Would you contribute this ? What is the best location to place these stubs ? Maybe phpstorm-stubs directly ?

mjourard commented 5 years ago

RE in phpstorm-stubs directly, I would agree with you but I can't get their tests to pass in master on my machine and I think that's a requirement for them to accept a PR.

For reference, I did the same commands as their travis build and was still getting 5 failures each time :/

arnaud-lb commented 5 years ago

It might be caused by a difference in your environment ? Maybe try to submit a PR anyway

remicollet commented 3 years ago

@arnaud-lb side note, you can also take benefit of PHP 8 new feature, maintain a stub.php and generate arginfo from it (using PHP 8, but usable with older versions).

arnaud-lb commented 3 years ago

This looks convenient, I need to try that

remicollet commented 3 years ago

This looks convenient, I need to try that

See or #56 (partial, as aliased functions not handled)