soyhuce/dev-tools

Laravel 开发工具

3.9.0 2024-03-08 15:49 UTC

This package is auto-updated.

Last update: 2024-09-08 17:00:05 UTC


README

Laravel 开发工具

Latest Version on Packagist Total Downloads

安装

您可以通过 composer 安装此包

$ composer require soyhuce/dev-tools --dev

Laravel 将会发现服务提供者 Soyhuce\DevTools\ServiceProvider.

安装 Dev Tools 后,使用以下命令发布其资产

php artisan vendor:publish --provider="Soyhuce\DevTools\ServiceProvider" --tag="config"

您还可以在 config\app.php 中添加 facade 到别名

'Debug' => \Soyhuce\DevTools\Debug\Debug::class,

可用工具

调试

激活后,它允许在请求执行时记录一些信息:HTTP 请求、HTTP 响应、数据库请求、时间或甚至使用的内存。

所有收集的信息都会带时间戳,并以调试级别发送到您的默认日志通道。

每个模块都可以通过配置文件单独启用或禁用。您可以按需配置时间戳格式。

Artisan

当运行 artisan 命令时收集信息。当收集来自 artisan 命令的信息时,artisan 收集器将收集命令名称、参数和选项。

php artisan make:model User -m -f
[2020-06-11 08:26:38] local.DEBUG: 
=> [2020-06-11 08:26:38.574492] artisan : 'make:model' User -m -f

计数器

计数器可以帮助计数。计数器在递增之前不需要存在。因为它初始化为 0。

<?php

for ($i=0; $i<100; $i++) {
    if (rand(0,1) === 0) {
        \Soyhuce\DevTools\Debug\Debug::incrementCounter('zeros');
    } else {
        \Soyhuce\DevTools\Debug\Debug::incrementCounter('ones');
    }
}
[2020-06-10 11:41:08] testing.DEBUG: 
=> [2020-06-10 11:41:08.819242] counter : zeros -> 56
=> [2020-06-10 11:41:08.819243] counter : ones -> 44

计数器可以递增多个。

<?php

function foo(array $values)
{
    \Soyhuce\DevTools\Debug\Debug::incrementCounter('foo', count($values));
}

foo([1,2,3]);
foo([4,5]);
foo([6,7,8,9]);
[2020-06-10 11:47:18] testing.DEBUG: 
=> [2020-06-10 11:47:18.252515] counter : foo -> 9

数据库

每个执行的数据库查询都会收集其绑定和持续时间。还会记录执行查询的总数。

[2020-06-10 11:56:17] testing.DEBUG: 
=> [2020-06-10 11:56:17.897045] database : select * from "users" -> 290μs
=> [2020-06-10 11:56:17.898037] database : select "id", "name" from "users" where "email" like '%@foo.com' -> 120μs
=> [2020-06-10 11:56:17.901442] database : query executed : 2

您可以定义一个阈值,如果执行查询的数量超过此阈值,则发出警告。

[2020-06-10 12:05:47] testing.DEBUG: 
=> [2020-06-10 12:05:47.119866] database : select * from "users" -> 280μs
=> [2020-06-10 12:05:47.120715] database : select "id", "name" from "users" where "email" like '%@foo.com' -> 80μs
=> [2020-06-10 12:05:47.124023] database : query executed : 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! database : Number of queries exceeded max 1 allowed : 2 !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

内存

内存收集器将收集内存峰值使用情况并将其添加到日志中。

[2020-06-10 12:09:16] local.DEBUG: 
=> [2020-06-10 12:09:16.593738] memory : 16.22Mo

您可以定义一个阈值,如果内存峰值使用量超过此阈值,则发出警告。

[2020-06-10 12:10:47] testing.DEBUG: 
=> [2020-06-10 12:10:47.391816] memory : 18.27Mo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! memory : Memory exceeded max 16Mo allowed : 18.27Mo !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  

消息

您可以通过调用 Debug::message 在调试流程中发送消息

<?php
function foo(string $arg) 
{
    \Soyhuce\DevTools\Debug\Debug::message("foo with '${arg}'");
}

foo('bar');
[2020-06-10 12:20:15] testing.DEBUG: 
=> [2020-06-10 12:20:15.944849] message : foo with 'bar'

模型

当启用时,收集器允许您在查询期间计数从数据库检索的模型数量

[2020-06-10 12:36:12] testing.DEBUG: 
=> [2020-06-10 12:36:12.155134] model : App\User -> 8
=> [2020-06-10 12:36:12.156016] model : App\Post -> 13

请求

可以收集请求数据。我们存储 HTTP 动词、URL 和参数。

class ExampleTest extends TestCase
{
    public function testBasicTest()
    {
        $this->get('/?foo=bar')->assertOk();
    }
}
[2020-06-10 12:25:28] testing.DEBUG: 
=> [2020-06-10 12:25:28.130223] request : GET https://
{
    "foo": "bar"
}

响应

还可以收集响应,包括其状态码和主体。

class ExampleTest extends TestCase
{
    public function testBasicTest()
    {
        $this->getJson('api/users/1')->assertOk();
    }
}
[2020-06-10 12:27:21] testing.DEBUG: 
=> [2020-06-10 12:27:21.440427] response : 200 -> 
{"id":1,"name":"Melyssa Kautzer","email":"junius.steuber@example.net","email_verified_at":"2020-06-09T13:06:39.000000Z","created_at":"2020-06-09T13:06:39.000000Z","updated_at":"2020-06-09T13:06:39.000000Z"}

时间

时间收集器将自动测量启动时间(laravel)和应用时间(您的应用)。

可以通过 facade 的 startMeasurestopMeasure 调用来测量性能部分

<?php
\Soyhuce\DevTools\Debug\Debug::startMeasure('someOperation');
$value = someOperation();
\Soyhuce\DevTools\Debug\Debug::stopMeasure('someOperation');
[2020-06-10 12:48:02] testing.DEBUG: 
=> [2020-06-10 12:48:02.295700] time : Booting -> 98.57ms
=> [2020-06-10 12:48:02.513956] time : someOperation -> 816.11μs
=> [2020-06-10 12:48:02.517676] time : Application -> 222.05ms

startMeasurestopMeasure 调用不需要在代码部分中,但如果没有启动(或者如果已经启动),则不能停止(或者开始)一个测量。

上面的例子可以用 Debug::measuring 方法简化

<?php
$value = \Soyhuce\DevTools\Debug\Debug::measuring('someOperation', function () {
    return someOperation();
});
// Or even shorter :
$value = Debug::measuring('someOperation', fn () => someOperation());

在记录之前,停止所有正在进行的测量。

您还可以使用相同的键多次测量。所有测量都存储在一起,日志将提供一些统计信息。

<?php
foreach($users as $user) {
    \Soyhuce\DevTools\Debug\Debug::measuring('updateUser', fn () => $this->update($user));
}
[2020-06-10 12:58:18] testing.DEBUG: 
=> [2020-06-10 12:58:17.157334] time : Booting -> 86.97ms
=> [2020-06-10 12:58:18.251381] time : updateUser -> 904.28ms cumulated on 12 entries (avg : 75.36ms - min : 67.8ms - max : 90ms - std : 5.4ms)
=> [2020-06-10 12:58:18.259324] time : Application -> 1.1s

最后,您可以定义一个阈值,如果应用持续时间超过此阈值,则发出警告。

[2020-06-10 12:58:18] testing.DEBUG: 
=> [2020-06-10 12:58:17.157334] time : Booting -> 86.97ms
=> [2020-06-10 12:58:18.251381] time : updateUser -> 904.28ms cumulated on 12 entries (avg : 75.36ms - min : 67.8ms - max : 90ms - std : 5.4ms)
=> [2020-06-10 12:58:18.259324] time : Application -> 1.1s
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! time : Application duration exceeded max 500ms allowed : 1.1s !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

HTTP 测试的特殊用例

在进行HTTP测试时,通常需要启动数据库或执行一些我们不希望添加到调试日志中的操作。

例如

/** @test */
public function userIsFetched()
{
    $user = factory(\App\User::class)->create();
    
    $this->putJson("api/users/{$user->id}",[
            'name' => 'John Doe'
        ])
        ->assertOk();
    
    $this->assertDatabaseHas('users',[
        'id' => $user->id,
        'name' => 'John Doe'
    ]);    
}

我们实际上对(例如)factory(\App\User::class)->create()assertDatabaseHas的数据库查询不感兴趣。

本包将负责在启动过程中丢弃任何收集到的信息,只返回感兴趣的部分,即$this->putJson("api/users/{$user->id}",['name' => 'John Doe'])中发生的情况。您只需确保在测试期间使用testing环境(这应该是您的默认设置)。

[2020-06-10 13:47:11] testing.DEBUG: 
=> [2020-06-10 13:47:11.522116] time : Booting -> 522.11ms
=> [2020-06-10 13:47:11.522194] request : GET https:///user/25
=> [2020-06-10 13:47:11.528122] database : select * from "users" where "id" = '25' limit 1 -> 160μs
=> [2020-06-10 13:47:11.528266] model : App\User -> 1
=> [2020-06-10 13:47:11.532207] response : 200 -> 
{"id":25,"name":"Daisha Schuppe","email":"sauer.jarrell@example.com","email_verified_at":"2020-06-10T13:47:11.000000Z","created_at":"2020-06-10T13:47:11.000000Z","updated_at":"2020-06-10T13:47:11.000000Z"}
=> [2020-06-10 13:47:11.532334] memory : 20Mo
=> [2020-06-10 13:47:11.532339] database : query executed : 1
=> [2020-06-10 13:47:11.532344] time : Application -> 10.23ms

瓶颈中间件(BottleneckMiddleware)

此中间件会在服务器端请求中添加一些延迟。当用户网络连接不佳时,它可以帮助检查应用程序的行为。

可以使用Soyhuce\DevTools\Middlewares\BottleneckMiddleware作为经典中间件。要使用它,只需在您的App/Http/Kernel.php或路由文件中添加中间件。

您可以在config/dev-tools.php文件中修改瓶颈持续时间。

您可能还希望将其应用于仅ajax请求。如果是这样,调整only_ajax值。请确保ajax请求使用X-Requested-With头设置为XMLHttpRequest

图片生成器

有时您想为测试、占位符等在本地生成图像。

您可以使用Soyhuce\DevTools\Faker\Image来实现这一点。为此,您必须安装intervention/image

Image::generate(int $width = 640, int $height = 640, ?string $text = null, string $encoding = 'jpg'): \Intervention\Image\Image

它将生成一个具有随机颜色和给定文本(或“宽度 x 高度”)的图像。例如

Image::generate(200, 150)

Image::generate(300, 100, 'The colors are not that good', 'png')

有关如何使用返回的图像,请参阅intervention/image 文档