查找Laravel应用程序实例正在使用多少RAM?

发布于 2025-02-12 04:09:06 字数 2327 浏览 0 评论 0原文

我需要弄清楚特定的Laravel应用程序正在使用多少RAM/CPU,但是到目前为止,当前代码显示了整个系统的用法。

我可以如何检查只有一个 laravel应用程序使用的RAM/CPU多少? (Windows任务管理器如何显示每个程序的CPU/RAM用法 - 但对于Web应用程序[尤其是Laravel,但欢迎使用Vanilla PHP解决方案]))

当前代码下面...

web.php:

...
Route::get('/details', function () {
    //RAM usage
    $free = shell_exec('free'); 
    $free = (string) trim($free);
    $free_arr = explode("\n", $free);
    $mem = explode(" ", $free_arr[1]);
    $mem = array_filter($mem);
    $mem = array_merge($mem);
    $usedmem = $mem[2];
    $usedmemInGB = number_format($usedmem / 1048576, 2) . ' GB';
    $memory1 = $mem[2] / $mem[1] * 100;
    $memory = round($memory1) . '%';
    $fh = fopen('/proc/meminfo', 'r');
    $mem = 0;
    while ($line = fgets($fh)) {
        $pieces = array();
        if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
            $mem = $pieces[1];
            break;
        }
    }
    fclose($fh);
    $totalram = number_format($mem / 1048576, 2) . ' GB';
    
    //cpu usage
    $cpu_load = sys_getloadavg(); 
    $load = $cpu_load[0] . '% / 100%';
    
    return view('details',compact('memory','totalram','usedmemInGB','load'));
});
...

lidess.blade.php:

<html>
<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current RAM Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$memory}}">
        <span class="sr-only">{{$memory}}</span>
      </div>
    </div>
    <span class="pull-right">{{$usedmemInGB}} / {{$totalram}} ({{$memory}})</span>  

  </div>

  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current CPU Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$load}}">
        <span class="sr-only">{{$load}}</span>
      </div>
    </div>
    <span class="pull-right">{{$load}}</span>   
  </div>

</body>
</html>

I need to figure out how much RAM/CPU a particular Laravel app is using, but so far the current code shows the entire system's usage.

How can I, instead, check how much RAM/CPU only one Laravel app is using? (kind of how the Windows task manager shows each program's CPU/RAM usage- but for web apps instead [particularly Laravel, but vanilla PHP solutions are welcome])

Current code below...

web.php:

...
Route::get('/details', function () {
    //RAM usage
    $free = shell_exec('free'); 
    $free = (string) trim($free);
    $free_arr = explode("\n", $free);
    $mem = explode(" ", $free_arr[1]);
    $mem = array_filter($mem);
    $mem = array_merge($mem);
    $usedmem = $mem[2];
    $usedmemInGB = number_format($usedmem / 1048576, 2) . ' GB';
    $memory1 = $mem[2] / $mem[1] * 100;
    $memory = round($memory1) . '%';
    $fh = fopen('/proc/meminfo', 'r');
    $mem = 0;
    while ($line = fgets($fh)) {
        $pieces = array();
        if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
            $mem = $pieces[1];
            break;
        }
    }
    fclose($fh);
    $totalram = number_format($mem / 1048576, 2) . ' GB';
    
    //cpu usage
    $cpu_load = sys_getloadavg(); 
    $load = $cpu_load[0] . '% / 100%';
    
    return view('details',compact('memory','totalram','usedmemInGB','load'));
});
...

details.blade.php:

<html>
<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current RAM Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$memory}}">
        <span class="sr-only">{{$memory}}</span>
      </div>
    </div>
    <span class="pull-right">{{$usedmemInGB}} / {{$totalram}} ({{$memory}})</span>  

  </div>

  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current CPU Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$load}}">
        <span class="sr-only">{{$load}}</span>
      </div>
    </div>
    <span class="pull-right">{{$load}}</span>   
  </div>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光匆匆的小流年 2025-02-19 04:09:06

请勿使用Laravel检测Laravel从您的服务器资源中消耗多少资源,而是使用服务器监视工具。

htop

是一个非常好的命令,我们可以用来检测哪些应用程序使用的服务器最多,多少!

现在,如果您对服务器良好 https://grafana.com/grafana.com/grafana/dashboards/ 检测RAM的使用等...

有一个很好的文档,但是如果您遇到任何麻烦,请发表评论,我将为您提供两个应用程序的服务器安装!

谢谢

Do not use Laravel to detect how much resources is Laravel consuming from your server resources, instead I would use server monitoring tools.

htop

Is a very good command that we can use to detect what apps are using our server the most and how much!

Now if you are good with servers, I advise you to use https://prometheus.io/, Use it with https://grafana.com/grafana/dashboards/ to detect the RAM usage etc...

There is a good documentation but if you faced any troubles, please comment and I'll help you with the server installation for both apps!

Thanks

§对你不离不弃 2025-02-19 04:09:06

或者,您可以使用 laravel-debugbar ,您可以在其中看到应用程序的内存量。
但是,这有点欺骗性,因为Debugbar本身来自记忆,因此更多的是出于信息目的。

Or you can use laravel-debugbar, where you can see how much memory the application consumes.
But, it's a little deceptive because the debugbar itself takes from memory, so it's more for informational purposes.

enter image description here

二货你真萌 2025-02-19 04:09:06

您可以获取Web服务器线程的内存使用量,而不是完全需要的,而是非常接近。

假设您使用Linux系统(在我的CentOS7+XAMPP环境上进行了测试):

$pid = getmypid();
exec("ps -eo%mem,rss,pid | grep $pid", $output);
$output = explode("  ", $output[0]);
dd($output[1]); // result in kB

用于Windows(在我的Win10+XAMPP环境中进行了测试):

$output = array();
exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
dd($output[5]); // result in kB

You could get the memory usage of you web server thread, not exactly what you need but very close.

Suppose you use linux system (tested on my CentOS7+XAMPP environment):

$pid = getmypid();
exec("ps -eo%mem,rss,pid | grep $pid", $output);
$output = explode("  ", $output[0]);
dd($output[1]); // result in kB

For windows (tested on my Win10+XAMPP environment):

$output = array();
exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
dd($output[5]); // result in kB
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文