fix: Improve Script Server performance by guarding debug log strings (#8586)

Building the per-request INC and RESPONSE debug log strings (basename(),
implode() and string concatenation) on every poll is wasteful when debug
logging is disabled, which is the normal production configuration.

Introduce testable helpers in lib/functions.php:
- cacti_effective_log_level() resolves the effective verbosity the same way
  cacti_log() does (selective per-file/per-plugin override, else log_verbosity).
- script_server_log_request() / script_server_log_response() only assemble and
  emit their debug strings when that level is POLLER_VERBOSITY_DEBUG.

script_server.php now calls these helpers instead of building the strings
inline, so the hot path stays cheap while selective script-server debugging
keeps working. Unit coverage is added for the new helpers.

This is the develop counterpart of the 1.2.x change in #8585.

Co-authored-by: TheWitness <copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-04 15:38:01 -05:00
1 parent 48cb019174
commit bd9998e723
4 files changed
+148 -4

No files matched your search

+1
View File
@@ -0,0 +1 @@
Improve Script Server performance by only building the debug log strings when debug logging is enabled
+65
View File
@@ -24,6 +24,71 @@
require_once __DIR__ . '/CactiProcessLock.php';
/**
* Resolves the effective logging verbosity for the current request.
*
* This mirrors the level resolution that cacti_log() performs: a selective
* (per-file or per-plugin) debug override takes precedence, otherwise the
* configured log_verbosity setting is used. Callers can use the result to
* skip assembling expensive debug strings when debug logging is not enabled.
*
* @return mixed The effective POLLER_VERBOSITY_* level.
*/
function cacti_effective_log_level() : mixed {
$level = get_selective_log_level();
if ($level == -1) {
$level = read_config_option('log_verbosity');
}
return $level;
}
/**
* Logs a Script Server request line at debug verbosity.
*
* The (potentially large) debug string is only assembled when DEBUG logging is
* actually enabled, which keeps the Script Server's hot path cheap on a normal
* production installation.
*
* @param int $pid The Script Server process id.
* @param int $ctr The request counter.
* @param string $include_file The script file named by the request.
* @param string $function The function named by the request.
* @param array $parameter_array The parsed function parameters.
*
* @return void
*/
function script_server_log_request(int $pid, int $ctr, string $include_file, string $function, array $parameter_array) : void {
if (cacti_effective_log_level() != POLLER_VERBOSITY_DEBUG) {
return;
}
cacti_log("DEBUG: PID[$pid] CTR[$ctr] INC: '" . basename($include_file) .
"' FUNC: '$function' PARMS: '" . implode('\', \'', $parameter_array) .
"'", false, 'PHPSVR');
}
/**
* Logs a Script Server response line at debug verbosity.
*
* As with script_server_log_request(), the debug string is only assembled when
* DEBUG logging is actually enabled.
*
* @param int $pid The Script Server process id.
* @param int $ctr The request counter.
* @param string $result The function result returned to the poller.
*
* @return void
*/
function script_server_log_response(int $pid, int $ctr, string $result) : void {
if (cacti_effective_log_level() != POLLER_VERBOSITY_DEBUG) {
return;
}
cacti_log("DEBUG: PID[$pid] CTR[$ctr] RESPONSE:'$result'", false, 'PHPSVR');
}
/**
* exec_poll - executes a command and returns its output
*
+2 -4
View File
@@ -279,9 +279,7 @@ while (1) {
continue;
}
cacti_log("DEBUG: PID[$pid] CTR[$ctr] INC: '" . basename($include_file) .
"' FUNC: '$function' PARMS: '" . implode('\', \'',$parameter_array) .
"'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG);
script_server_log_request((int) $pid, $ctr, $include_file, $function, $parameter_array);
// validate the existence of the function, and include if applicable
if (!function_exists($function)) {
@@ -323,7 +321,7 @@ while (1) {
fputs(STDOUT, trim($result) . "\n");
fflush(STDOUT);
cacti_log("DEBUG: PID[$pid] CTR[$ctr] RESPONSE:'$result'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG);
script_server_log_response((int) $pid, $ctr, $result);
$ctr++;
} else {
@@ -0,0 +1,80 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
/*
* Covers the Script Server logging helpers that assemble debug strings only
* when debug logging is enabled, keeping the Script Server hot path cheap on a
* normal production installation.
*/
beforeEach(function () {
global $config;
// Redirect cacti_log() to a temp file and serve every option it reads from
// the CLI option cache so nothing reaches the (absent) test database.
$this->logfile = tempnam(sys_get_temp_dir(), 'ss-log-');
$config[OPTIONS_CLI]['path_cactilog'] = $this->logfile;
$config[OPTIONS_CLI]['log_destination'] = 1;
$config[OPTIONS_CLI]['selective_debug'] = '';
$config[OPTIONS_CLI]['selective_plugin_debug'] = '';
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE;
});
afterEach(function () {
if (!empty($this->logfile) && file_exists($this->logfile)) {
unlink($this->logfile);
}
});
test('cacti_effective_log_level falls back to log_verbosity without a selective override', function () {
global $config;
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_MEDIUM;
expect(cacti_effective_log_level())->toBe(POLLER_VERBOSITY_MEDIUM);
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG;
expect(cacti_effective_log_level())->toBe(POLLER_VERBOSITY_DEBUG);
});
test('script_server_log_request logs only at debug verbosity', function () {
global $config;
// Debug disabled: the helper returns without touching the log.
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE;
script_server_log_request(1234, 1, '/scripts/ss_example.php', 'ss_example', ['one', 'two']);
expect(file_get_contents($this->logfile))->toBe('');
// Debug enabled: the helper assembles and logs the request line.
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG;
script_server_log_request(1234, 2, '/scripts/ss_example.php', 'ss_example', ['one', 'two']);
$logged = file_get_contents($this->logfile);
expect($logged)->toContain("PID[1234] CTR[2] INC: 'ss_example.php'")
->and($logged)->toContain("FUNC: 'ss_example'")
->and($logged)->toContain("PARMS: 'one', 'two'");
});
test('script_server_log_response logs only at debug verbosity', function () {
global $config;
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE;
script_server_log_response(1234, 1, 'result-value');
expect(file_get_contents($this->logfile))->toBe('');
$config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG;
script_server_log_response(1234, 2, 'result-value');
expect(file_get_contents($this->logfile))->toContain("PID[1234] CTR[2] RESPONSE:'result-value'");
});