在 Laravel 中获取服务器响应时间的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31619350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to get server response time in Laravel
提问by John Bupit
I've created a terminable middlewarethat sends a request to Google Analytics. One of the attributes I send is the server response time. Here's how I do it:
我创建了一个可终止的中间件,用于向 Google Analytics 发送请求。我发送的属性之一是服务器响应时间。这是我的方法:
In \App\Http\Kernel
I add the SendAnalytics
middleware:
在\App\Http\Kernel
我添加SendAnalytics
中间件:
class Kernel extends HttpKernel {
...
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
...
'App\Http\Middleware\SendAnalytics',
];
}
And the SendAnalytics
middleware looks like:
而SendAnalytics
中间件是这样的:
class SendAnalytics implements TerminableMiddleware {
protected $startTime;
public function __construct() {
$this->startTime = microtime(true);
}
public function handle($request, Closure $next) {
return $next($request);
}
public function terminate($request, $response) {
$responseTime = microtime(true) - $this->startTime;
/* I send a request to Google here, using their Measurement Protocol */
// Dying for debugging purposes
dd($responseTime); // Always prints 0.0
}
}
But this always shows 0.0
. What would be the correct way to show the server response time?
但这总是显示0.0
。显示服务器响应时间的正确方法是什么?
回答by John Bupit
I used microtime(true) - LARAVEL_START
. Seems to give a fairly accurate response time.
我用过microtime(true) - LARAVEL_START
。似乎给出了相当准确的响应时间。
As Bogdan mentions in the comment:
正如博格丹在评论中提到的:
The
LARAVEL_START
constant is defined inbootstrap/autoload.php
which is the very first file included frompublic/index.php
, so this makes it the first statement to be executed. If you place the middleware last on the list, its terminate method will be the last one executed beforeapp->terminate()
will be called, so you should get a pretty good computation of execution time.
该
LARAVEL_START
常数被定义在bootstrap/autoload.php
它是由包括第一个文件public/index.php
,所以这使得要执行的第一个语句。如果你把中间件放在列表的最后,它的 terminate 方法将是最后一个app->terminate()
被调用之前执行的方法,所以你应该得到一个很好的执行时间计算。
回答by mp31415
I noticed that in a single request life cycle middleware instance may be initialized more than once. The second time is right before terminate
call. That would explain zero time result (on my machine it was not zero but pretty close to it, while the actual request time was more like 200ms). The handle
method was obviously called only once and this is where start time must be recorded.
我注意到在单个请求生命周期中,中间件实例可能会被初始化多次。第二次是在terminate
打电话之前。这将解释零时间结果(在我的机器上它不是零而是非常接近它,而实际请求时间更像是 200 毫秒)。该handle
方法显然只被调用了一次,这是必须记录开始时间的地方。
class SendAnalytics implements TerminableMiddleware {
protected $startTime;
public function handle($request, Closure $next) {
$this->startTime = microtime(true);
return $next($request);
}
...
}