Files
core/Zotlabs/Module/Perfstats.php
Harald Eilertsen 52a2a0d89a Pass loadavg as array from Perfstats
There's no reason we should format the data into a string in the
Perfstats module. Let the recipient do what they want with it instead.

As an example, we reduce the precision of the loadavg stats in the
system status widget. 3 digits precision should be more than enough for
this type of status display.

Project......: Performance Profiling
Sponsored-by.: NLnet NGI0 Commons Fund
2026-03-01 11:34:28 +01:00

80 lines
1.7 KiB
PHP

<?php
/* Handler for perfstats requests.
*
* SPDX-FileCopyrightText: 2026 The Hubzilla Community
* SPDX-FileContributor: Harald Eilertsen <haraldei@anduin.net>
*
* SPDX-License-Identifier: MIT
*/
namespace Zotlabs\Module;
use DBA;
use Zotlabs\Lib\DbStats;
use Zotlabs\Lib\Queue;
use Zotlabs\Lib\QueueWorkerStats;
use Zotlabs\Web\Controller;
/**
* Controller for the `/perfstats` module.
*
* Collects various performance stats for the site, and reponds with the stats
* as a json array.
*/
class Perfstats extends Controller
{
public function init(): void {
//
// We only accept GET requests
//
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_status_exit(400, 'Unsupported method');
}
//
// We only accept json requests
//
if (getBestSupportedMimeType(['application/json']) === null) {
http_status_exit(400, 'No supported format');
}
//
// Only admins should be given access
//
if (!is_site_admin()) {
http_status(401, 'Access denied');
json_return_and_die(['error' => 'access denied']);
}
$data = $this->getStats();
json_return_and_die($data);
}
private function getStats(): array {
$stats = [];
if (function_exists('sys_getloadavg')) {
$stats['loadavg'] = sys_getloadavg();
}
$stats['dbqueries'] = $this->getNumQueries();
$stats['outqueue'] = Queue::count();
$qwstats = new QueueWorkerStats();
$stats['queueworkers'] = $qwstats->active;
$stats['workqsz'] = $qwstats->size;
// Return a timestamp, so that it is possible to infer
// changes of the stats over time. A resolution of
// seconds should be good enough for our purposes.
$stats['ts'] = time();
return $stats;
}
private function getNumQueries(): int {
$stats = DbStats::getStats();
return $stats->getQueries();
}
}