{"id":21194135,"url":"https://github.com/petrknap/php-profiler","last_synced_at":"2025-07-10T03:33:25.715Z","repository":{"id":57038293,"uuid":"48235332","full_name":"petrknap/php-profiler","owner":"petrknap","description":"PHP profiler for short-term \u0026 long-term profiling","archived":false,"fork":false,"pushed_at":"2024-10-19T17:50:37.000Z","size":104,"stargazers_count":7,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-19T21:17:12.775Z","etag":null,"topics":["debug","debugging","performance-analysis","performance-monitoring","php","php-library","profile","profiler","profiling"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrknap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-18T12:54:13.000Z","updated_at":"2024-10-19T17:49:22.000Z","dependencies_parsed_at":"2024-10-17T10:39:05.771Z","dependency_job_id":null,"html_url":"https://github.com/petrknap/php-profiler","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"d61d8e1e72346356b4d3c0987fbfbf6faccfe6a6"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-profiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-profiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-profiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-profiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrknap","download_url":"https://codeload.github.com/petrknap/php-profiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225615751,"owners_count":17497051,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debug","debugging","performance-analysis","performance-monitoring","php","php-library","profile","profiler","profiling"],"created_at":"2024-11-20T19:19:49.930Z","updated_at":"2024-11-20T19:19:50.643Z","avatar_url":"https://github.com/petrknap.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP profiler for short-term \u0026 long-term profiling\n\nThis tool allows you to monitor performance and detect memory leaks as well as inconsistent performance behavior of your application over time.\n\n## Basic profiling\n\nFor basic profiling you can use a profiling helper.\nThe [`Profiling`](./src/Profiling.php) will allow you to profile between `start` and `finish` methods calls.\n\n```php\nnamespace PetrKnap\\Profiler;\n\n$profiling = Profiling::start();\n// do something\n$profile = $profiling-\u003efinish();\n\nprintf('It took %.1f s to do something.', $profile-\u003egetDuration());\n```\n\nThe [`Profiling`](./src/Profiling.php) is simple and **cannot be turned on and off** easily.\nSo a [profiler](./src/ProfilerInterface.php) was created for the purpose of hard-coded more complex profiling.\n\n## Complex profiling\n\nRequest a [profiler](./src/ProfilerInterface.php) as a dependency and call a `profile` method on it.\n\n```php\nnamespace PetrKnap\\Profiler;\n\nfunction doSomething(ProfilerInterface $profiler): string {\n    return $profiler-\u003eprofile(function (): string {\n        return 'something';\n    })-\u003eprocess(fn (ProfileInterface $profile) =\u003e printf(\n        'It took %.1f s to do something.',\n        $profile-\u003egetDuration(),\n    ));\n}\n```\n\n### How to enable / disable it\n\nIt can be easily enabled, or disabled **through the DI**, which provides either the [`Profiler`](./src/Profiler.php) or the [`NullProfiler`](./src/NullProfiler.php).\n\n```php\nnamespace PetrKnap\\Profiler;\n\necho doSomething(new Profiler());\necho doSomething(new NullProfiler());\n```\n\n## Useful features\n\n### Take snapshot\n\nIf you need to **measure the current values**, just call the `takeSnapshot` method on the [`Profiling`](./src/Profiling.php), or a [profiler](./src/ProfilerInterface.php).\n\n```php\nnamespace PetrKnap\\Profiler;\n\n$profiling = Profiling::start();\n// do something\n$profiling-\u003etakeSnapshot();\n// do something more\n$profile = $profiling-\u003efinish();\n\nprintf('There are %d memory usage records.', count($profile-\u003egetMemoryUsages()));\n```\n\nIf you want to automate it then [take snapshot on tick](#take-snapshot-on-tick).\nOr you can use a more practical [cascade profiling](#cascade-profiling).\n\n#### Take snapshot on tick\n\nFor greater precision, you can take **snapshot on each `N` tick**.\n\n```php\ndeclare(ticks=2); // this declaration is important (N=2)\n\nnamespace PetrKnap\\Profiler;\n\n$profiling = Profiling::start(takeSnapshotOnTick: true);\n(fn () =\u003e 'something')();\n$profile = $profiling-\u003efinish();\n\nprintf('There are %d memory usage records.', count($profile-\u003egetMemoryUsages()));\n```\n\nThis will result in **very detailed code tracking**, which can degrade the performance of the monitored application.\n\n### Cascade profiling\n\nThe `profile` method provides you a nested [profiler](./src/ProfilerInterface.php) that you can use for more detailed cascade profiling.\n\n```php\nnamespace PetrKnap\\Profiler;\n\n$profile = (new Profiler())-\u003eprofile(function (ProfilerInterface $profiler): void {\n    // do something\n    $profiler-\u003eprofile(function (): void {\n        // do something more\n    });\n});\n\nprintf('There are %d memory usage records.', count($profile-\u003egetMemoryUsages()));\n```\n\n---\n\nRun `composer require petrknap/profiler` to install it.\nYou can [support this project via donation](https://petrknap.github.io/donate.html).\nThe project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-profiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrknap%2Fphp-profiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-profiler/lists"}