diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fcf2d6d..1dbea39 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ on: jobs: tests: - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 services: redis: image: redis:3.2-alpine @@ -60,7 +60,7 @@ jobs: run: composer test static-analysis: - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 name: "Static analysis" strategy: fail-fast: false @@ -80,7 +80,7 @@ jobs: coverage: "none" - name: "Cache dependencies installed with composer" - uses: "actions/cache@v2" + uses: "actions/cache@v4" with: path: "~/.composer/cache" key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}" diff --git a/Stopwatch/Stopwatch.php b/Stopwatch/Stopwatch.php index d98ca4b..1f7e3f4 100644 --- a/Stopwatch/Stopwatch.php +++ b/Stopwatch/Stopwatch.php @@ -4,6 +4,8 @@ class Stopwatch { + private const FLUSH_TIMERS_LIMIT = 1000; + protected $enabled = false; protected $initTags = []; @@ -29,7 +31,9 @@ function_exists('pinba_timer_start') && function_exists('pinba_timer_stop') && function_exists('pinba_timer_add') && function_exists('pinba_get_info') - ; + && function_exists('pinba_timers_get') + && function_exists('pinba_flush') + ; } public function disable(): void @@ -37,17 +41,21 @@ public function disable(): void $this->enabled = false; } - public function start(array $tags) + public function start(array $tags): StopwatchEvent { - if ($this->enabled) { - $tags = array_merge($this->initTags, $tags); - if (isset($tags['group']) && !isset($tags['category']) && false !== strpos($tags['group'], '::')) { - $v = explode('::', $tags['group']); - $tags['category'] = $v[0]; - } + if (!$this->enabled) { + return new StopwatchEvent(); + } + + $this->flushIfTimersLimitReached(); + + $tags = array_merge($this->initTags, $tags); + if (isset($tags['group']) && !isset($tags['category']) && false !== strpos($tags['group'], '::')) { + $v = explode('::', $tags['group']); + $tags['category'] = $v[0]; } - return new StopwatchEvent($this->enabled ? pinba_timer_start($tags) : null); + return new StopwatchEvent(pinba_timer_start($tags)); } public function add(array $tags, $time): void @@ -56,8 +64,17 @@ public function add(array $tags, $time): void return; } - $tags = array_merge($this->initTags, $tags); + $this->flushIfTimersLimitReached(); + $tags = array_merge($this->initTags, $tags); pinba_timer_add($tags, $time); } + + private function flushIfTimersLimitReached(): void + { + $timersCount = count(pinba_timers_get(PINBA_ONLY_STOPPED_TIMERS)); + if ($timersCount >= self::FLUSH_TIMERS_LIMIT) { + pinba_flush(null, PINBA_FLUSH_ONLY_STOPPED_TIMERS); + } + } } diff --git a/meta/phpstan/stub.php b/meta/phpstan/stub.php index 1d7bc38..2f31349 100644 --- a/meta/phpstan/stub.php +++ b/meta/phpstan/stub.php @@ -24,3 +24,24 @@ function pinba_timer_add(array $tags, int $value) { } } + +if (!function_exists('pinba_timers_get')) { + function pinba_timers_get(int $flag = 0): array + { + return []; + } +} + +if (!function_exists('pinba_flush')) { + function pinba_flush(?string $scriptName = null, int $flags = 0): void + { + } +} + +if (!defined('PINBA_ONLY_STOPPED_TIMERS')) { + define('PINBA_ONLY_STOPPED_TIMERS', 1); +} + +if (!defined('PINBA_FLUSH_ONLY_STOPPED_TIMERS')) { + define('PINBA_FLUSH_ONLY_STOPPED_TIMERS', 1); +}