jcm / footprint-php

composer 包,允许您监控和检索运行中的 PHP 脚本的定制统计信息

v1.2 2021-02-12 09:22 UTC

This package is auto-updated.

Last update: 2024-09-05 15:51:14 UTC


README

Latest Stable Version Latest Release Version Total Downloads License

Composer 包,允许您跟踪/监控和检索/计算/生成运行中的 PHP 脚本的定制统计信息。

它以模块化方式构建,事件触发,易于跟踪和自定义。

该包已包含一些模块,例如:

  • ChartJS 折线图报告(输出仅限)
  • CSV 报告(日志和输出)
  • 时间跟踪(日志)
  • 内存跟踪(日志)

代码简单且简短,因此您可以快速上手并创建自己的代码。

快速入门

安装

composer require jcm/footprint-php

分步使用示例和输出

在这个示例中,我们想要测量函数调用或类方法使用的时间和内存。

请记住,这是一个示例,如果您编写代码得当,则不需要初始化超过一次

基本示例中,我们创建了一个名为 SomeClassName 的类和一个名为 testMethod 的方法,我们将测量调用此方法时的时间和内存使用情况。

我们将通过从头创建一个 PHP 项目来完成此示例,如果您已经有了项目,请根据您的需求调整示例。

步骤 1:使用 composer 创建 PHP 项目

运行 composer init 并创建一个 项目,您想怎么命名就怎么命名。

当向导询问有关项目的不同问题时,它会询问您要安装哪些依赖项,现在请保持空白。

看起来像这样:您想要交互式地定义依赖项(require)吗?[是]否回答否。

步骤 2:安装 footprint-php 包

运行 composer require jcm/footprint-php

您应该得到类似以下的输出

Using version ^1.1 for jcm/footprint-php
./composer.json has been updated
Running composer update jcm/footprint-php
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking jcm/footprint-php (v1.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing jcm/footprint-php (v1.1): Extracting archive
Generating autoload files

步骤 4:创建一个 index.php 并添加我们的代码

运行 touch index.php

在以下代码内部

<?php declare(strict_types = 1);

require("vendor/autoload.php");

use Footprint\Tracker;             //For the main Tracker instance
use Footprint\Modules\ChartJS;     //Tracker module
use Footprint\Modules\Memory;      //Tracker module
use Footprint\Modules\Time;        //Tracker module

class SomeClassName 
{
    public function testMethod() {
        $mem = str_repeat("X", 1024 * 1024); //We create a variable using certain memory size
        
        $moduleTime = new Time();  //Create instance of the Time module
        $moduleMem = new Memory(); //Create instance of the Memory module
        
        /**
         * We create an instance of the ChartJS module
         * and we also specify the name of the output file that it will generate for us
         */
        $moduleChartJS = new ChartJS("report.html");

        /**
         * Module ChartJS needs to know in advance which "Tracker keys" will
         * consider at the moment of generating the chart for us.
         * 
         * You can read more about this in the Wiki section, but basically we are adding all
         * the keys used in the Memory and Time modules.
         */
        foreach($moduleMem->getKeys() as $key) {
            $moduleChartJS->addKey($key);
        }

        foreach($moduleTime->getKeys() as $key) {
            $moduleChartJS->addKey($key);
        }
        
        $tracker = new Tracker(); //Main Tracker instance
        
        /**
         * We load the previously created modules into the Tracker.
         * 
         * The Tracker is the main actor in all this scenario.
         * 
         * The Tracker will use the modules properly.
         */
        $tracker->loadModule($moduleMem);
        $tracker->loadModule($moduleTime);
        $tracker->loadModule($moduleChartJS);
        
        $tracker->init(); //Start the Tracker
        
        $tracker->log(); //We do our first log
        
        /**
         * Now we will ...
         * 1) Increase the memory use by the $mem variable
         * 2) Make some sleeps to generate a delay
         * 
         * All these is done trying to simulate a real php code execution, that uses
         * different amount of memory and time execution.
         * 
         */

        $mem = str_repeat($mem, 2); //We duplicate the size of memory use by variable $mem
        
        sleep(1); //Wait 1 second
        
        $tracker->log(); //Log again
        
        sleep(2);
        
        $mem = str_repeat($mem, 2);
        
        $tracker->log();
        
        sleep(1);
        
        $mem = str_repeat("XXX", 2000);
        
        $tracker->log();
        
        /**
         * When we get to the point we dont want to track anymore, then we finalize tracking calling
         * the end() method of the Tracker.
         */
        $tracker->end();
    }
}

$testClass = new SomeClassName();

$testClass->testMethod();

echo "End here";

步骤 5:执行示例

从命令行运行 php -f index.php

步骤 6:检查结果

如果一切顺利,那么您应该有一个名为 report.html 的文件,内容应类似于以下内容

Output report using ChartJS

为什么是这个包?

创建此包是为了拥有一个简单且小巧的包,可自定义,允许您在执行 PHP 脚本时检索数据。

文档(正在进行中)

请参阅项目的 wiki 部分,了解有关包设计、使用方法和如何扩展包的更多信息。

为项目做出贡献

尽管我在努力改进这个包,但请随时提出问题、发送拉取请求、创建新模块或增强现有模块等。

可以创建高级模块(例如,带有 WebSocket 连接的实时仪表板,以便我们可以有一个实时仪表板等),但为了这个包的第一个版本,我想保持它简单且实用。

欢迎各种帮助

作者

Juan Carlos Morales jcmargentina@gmail.com