php profiler
2.2.0
該工具可讓您監控效能並檢測記憶體洩漏以及應用程式隨時間變化的不一致的效能行為。
對於基本分析,您可以使用分析助理。 Profiling將允許您在start和finish方法呼叫之間進行分析。
namespace PetrKnap Profiler ;
$ profiling = Profiling:: start ();
// do something
$ profile = $ profiling -> finish ();
printf ( ' It took %.1f s to do something. ' , $ profile -> getDuration ()); Profiling很簡單,不能輕易開啟和關閉。因此,創建了一個探查器,用於硬編碼更複雜的探查。
請求探查器作為依賴項並呼叫其profile方法。
namespace PetrKnap Profiler ;
function doSomething ( ProfilerInterface $ profiler ): string {
return $ profiler -> profile ( function (): string {
return ' something ' ;
})-> process ( fn ( ProfileInterface $ profile ) => printf (
' It took %.1f s to do something. ' ,
$ profile -> getDuration (),
));
}它可以透過 DI輕鬆啟用或停用,DI 提供Profiler或NullProfiler 。
namespace PetrKnap Profiler ;
echo doSomething ( new Profiler ());
echo doSomething ( new NullProfiler ());如果您需要測量目前值,只需呼叫Profiling或分析器上的takeSnapshot方法。
namespace PetrKnap Profiler ;
$ profiling = Profiling:: start ();
// do something
$ profiling -> takeSnapshot ();
// do something more
$ profile = $ profiling -> finish ();
printf ( ' There are %d memory usage records. ' , count ( $ profile -> getMemoryUsages ()));如果您想自動化它,請在滴答聲中拍攝快照。或者您可以使用更實用的級聯分析。
為了獲得更高的精度,您可以在每個N刻度上拍攝快照。
declare (ticks= 2 ); // this declaration is important (N=2)
namespace PetrKnap Profiler ;
$ profiling = Profiling:: start (takeSnapshotOnTick: true );
( fn () => ' something ' )();
$ profile = $ profiling -> finish ();
printf ( ' There are %d memory usage records. ' , count ( $ profile -> getMemoryUsages ()));這將導致非常詳細的程式碼跟踪,從而降低受監視應用程式的效能。
profile方法為您提供了一個嵌套的分析器,您可以使用它來進行更詳細的級聯分析。
namespace PetrKnap Profiler ;
$ profile = ( new Profiler ())-> profile ( function ( ProfilerInterface $ profiler ): void {
// do something
$ profiler -> profile ( function (): void {
// do something more
});
});
printf ( ' There are %d memory usage records. ' , count ( $ profile -> getMemoryUsages ()));執行composer require petrknap/profiler來安裝它。您可以透過捐款支持該項目。該專案根據LGPL-3.0-or-later的條款獲得許可。