分享开发的用于检测每个接口性能的插件
背景
在开发 Laravel 应用的时候,对于 MVC 我们可以直接在控制台看到接口渲染等信息,但是如果是纯API接口,信息很少;
可能会疏忽忘记优化数据库语句,例如忘记加索引,查询次数过多等;
也有可能接口上线后在服务器上遇到打开文件数过多的情况;
为了更加清晰直观的看到自己写的接口的性能问题,所以开发了 Performance Printer package,希望能够帮助到大家;
如果有bug或是其他建议也欢迎大家提 ISSUE。
也可以提交代码帮助完善这个package。
显示效果
[ POST ] /api/user/login
[ Content-Type ] : application/json
{
"user": "12345678910",
"password": "b49f16999ce2a0a0df9b6e0e66bd4f32"
}
[ Included Files Count ] 379
[ Total ] 2 queries and ran for 16.39 ms.
[ SQL ran for 11.56 ms ] RAW SQL: select * from `users` where `phone` = "12345678910" or `email` = "12345678910" limit 1
+----+-------------+-------+------------+------+--------------------+-----+---------+-----+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+-----+---------+-----+------+----------+-------------+
| 1 | SIMPLE | users | | ALL | users_email_unique | | | | 1770 | 19 | Using where |
+----+-------------+-------+------------+------+--------------------+-----+---------+-----+------+----------+-------------+
[ SQL ran for 4.83 ms ] RAW SQL: insert into `ticket` (`user_id`, `expire_time`, `ticket`, `updated_at`, `created_at`) values (1, "2020-06-08 17:20:39", "08c14bace9bdfd9dbe3558adba463d1f198", "2020-06-01 17:20:39", "2020-06-01 17:20:39")
+----+-------------+--------+------------+------+---------------+-----+---------+-----+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+-----+---------+-----+------+----------+-------+
| 1 | INSERT | ticket | | ALL | | | | | | | |
+----+-------------+--------+------------+------+---------------+-----+---------+-----+------+----------+-------+
[ Response Load 88.63 ms] {"code":200,"data":{"user":{"id":1,"username":"koga","phone":"12345678910","email":"koga@gmail.com","created_at":"2020-06-01 14:45:09","updated_at":"2019-06-01 14:45:09","loginname":"koga","from":"api","regip":null,"regdate":null,"ticket":"08c14bace9bdfd9dbe3558adba463d1f198"}},"msg":"\u767b\u5f55\u6210\u529f\uff01"}
使用
1.在开发环境安装
稳定版本:
composer require tsaikoga/performance-printer --dev
非稳定版本:
composer require tsaikoga/performance-printer:dev-master --dev
为performance printer 增加自定义配置
2.创建文件 config/performance_printer.php
<?php
return [
// print request message
'request' => true,
// print response datas
'response' => false,
// included files count
'included_files_count' => true,
// print table Style
'table_style' => 'default',
// language: Only support English(en) and Chinese(cn) now
'lang' => 'cn',
'query' => [
// print raw sql
'raw_sql' => true,
// print explain sql
'explain' => true,
// Same queries with different bindings are superimposed
'unique_query' => true,
// The connections
'connections' => ['mysql'],
],
// enable this package or disable it
'enable' => true,
// log
'log' => [
// is logging enable?
'enable' => true,
// The path that the log file stored
'filepath' => '/tmp/performance_printer.log',
],
];
3.在 .env
文件, 设置local
APP_ENV=local
4.运行服务
php artisan serve
如果你使用的 apache 或者 nginx 服务,你可以开启 “打印到日志” log => [ 'enable' => true, 'file_path' => '/tmp/performance_printer.log']
的功能,其中 file_path
为 日志输出文件路径;
laravel/framework 8.0以上装不了