Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
tests:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
services:
redis:
image: redis:3.2-alpine
Expand Down Expand Up @@ -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
Expand All @@ -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') }}"
Expand Down
37 changes: 27 additions & 10 deletions Stopwatch/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Stopwatch
{
private const FLUSH_TIMERS_LIMIT = 1000;

protected $enabled = false;
protected $initTags = [];

Expand All @@ -29,25 +31,31 @@ 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
{
$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
Expand All @@ -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);
}
}
}
21 changes: 21 additions & 0 deletions meta/phpstan/stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}