Files
cacti/tests/Unit/DataCollection/Poller/ScriptServerLogHelpersTest.php
T
bd9998e723 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>
2026-11-04 15:38:01 -05:00

81 lines
3.3 KiB
PHP

<?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'");
});