mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
The total number of queries is not that interesting for performance measurements, so let's do queries pr/second instead. Adds a timestamp column to the dataset, which could be useful for other purposes as well (like making a graph over time etc.) Project......: Performance Profiling Sponsored-by.: NLnet NGI0 Commons Fund
80 lines
1.8 KiB
PHP
80 lines
1.8 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'] = implode(' / ', 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();
|
|
}
|
|
}
|