开发接口过程中,经常需要测试接口性能,接口运行时长,内存占用开销等。可以使用系统提供的think\Debug类获取某个区间代码的运行时长和内存占用情况,方便快捷。代码如下:
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Debug;
class Index extends Controller
{
public function index()
{
Debug('begin');
//自己需要测试的代码
//...
Debug('end');
return Debug('begin','end',6).'s';//时间
return Debug('begin','end','m').'kb';//内存
}
}