janiaje/benchmark

基准测试包。

v2.0.1 2023-09-28 13:55 UTC

This package is auto-updated.

Last update: 2024-08-28 15:56:30 UTC


README

为您的项目添加基准测试辅助工具,以获取

  • 精确时间
  • 已过时间
  • RAM使用量
  • Eloquent执行的查询

在检查点之间。

安装

composer require janiaje/benchmark

如果Laravel版本小于5.5,您必须在config/app.php中手动包含以下行

Janiaje\Benchmark\Provider::class,

使用(基础)

使用'benchmark()'辅助函数轻松访问基准测试类

benchmark()

添加检查点

benchmark()->checkpoint();

获取第一个和最后一个检查点之间的已过时间

$elapsedTime = benchmark()->getElapsedTime();

获取检查点中PHP分配的最大RAM量(以字节为单位)

$ramUsage = benchmark()->getPeakRamUsage();

获取检查点

$checkpoints = benchmark()->getCheckpoints();

转储检查点

benchmark()->dump();

DD检查点

benchmark()->dd();

设置检查点的输出格式

benchmark()->setOutputFormat(ArrayFormat::class);
benchmark()->setOutputFormat(JsonFormat::class);

上述两个选项默认可用。您可以覆盖它们或创建自己的,但请确保它实现了\Janiaje\Benchmark\OutputFormats\OutputFormat接口。

ArrayFormat::class输出示例

Collection {#275 ▼
  #items: array:2 [▼
    0 => array:6 [▼
      "id" => "#1"
      "name" => null
      "group" => null
      "time" => Carbon @1521101210 {#272 ▶}
      "timeDifference" => null
      "ram" => 6291456
      "queries" => []
    ]
    1 => array:6 [▼
      "id" => "#2"
      "name" => null
      "group" => null
      "time" => Carbon @1521101211 {#270 ▶}
      "timeDifference" => DateInterval {#277 ▶}
      "ram" => 6291456
      "queries" => array:1 [▼
        0 => {#279 ▼
          +"query": "SELECT * FROM users WHERE email = ?"
          +"bindings": array:1 [▼
            0 => "janiaje@gmail.com"
          ]
          +"time": 1.15
        }
      ]
    ]
  ]
}

使用(附加选项)

命名检查点

此名称将显示在结果中,因此可以更容易地找到特定的检查点。

benchmark()->checkpoint("After expensive calculation");
    1 => array:6 [▼
      "id" => "#2"
      "name" => "After expensive calculation"
      "group" => null
      "time" => Carbon @1521101211 {#270 ▶}

分组检查点

创建带有分组的检查点

benchmark()->checkpointWithGroup("File generation");

此组将显示在结果中

    1 => array:6 [▼
      "id" => "#2"
      "name" => "After expensive calculation"
      "group" => "File generation"
      "time" => Carbon @1521101211 {#270 ▶}

您可以按组过滤结果

benchmark()->getCheckpointsByGroup("File generation");

删除检查点

删除所有检查点

benchmark()->deleteAllCheckpoints();

按组删除检查点

benchmark()->deleteCheckpoints("File generation");

按ID删除检查点

$checkpoint = benchmark()->checkpoint();
benchmark()->deleteCheckpoint($checkpoint->getId();

如果您不想删除它们,您始终可以创建自己的Benchmark实例并单独收集所有内容

$benchmark1 = new Benchmark;
$benchmark1->checkpoint();
$benchmark1->getAllCheckpoints();

$benchmark2 = new Benchmark;
$benchmark2->checkpoint();
$benchmark2->getAllCheckpoints();