Skip to content

Commit 569ae1e

Browse files
committed
Move global function under the Stackwatch namespace
1 parent c2f73d2 commit 569ae1e

6 files changed

Lines changed: 213 additions & 150 deletions

File tree

.github/workflows/build.yml

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
---
21
name: build
32
on:
43
push: ~
54
pull_request: ~
65

76
jobs:
8-
linux_tests:
9-
name: PHP on ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.composer-flags }}
7+
tests:
8+
name: Tests • PHP ${{ matrix.php }} ${{ matrix.stability }}
109
runs-on: ubuntu-22.04
10+
1111
strategy:
1212
matrix:
13-
php: ['8.1', '8.2', '8.3', '8.4']
13+
php: ['8.1', '8.2', '8.3', '8.4', '8.5']
1414
stability: [prefer-lowest, prefer-stable]
15-
include:
16-
- php: '8.5'
17-
flags: "--ignore-platform-req=php"
18-
stability: prefer-stable
15+
1916
steps:
2017
- name: Checkout code
2118
uses: actions/checkout@v4
@@ -26,39 +23,111 @@ jobs:
2623
php-version: ${{ matrix.php }}
2724
coverage: xdebug
2825
tools: composer:v2
29-
- name: Check PHP Version
30-
run: php -v
3126

3227
- name: Validate composer files
3328
run: composer validate --strict
3429

30+
# -----------------------
31+
# Composer cache
32+
# -----------------------
3533
- name: Get Composer Cache Directory
3634
id: composer-cache
37-
run: |
38-
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
39-
- uses: actions/cache@v3
35+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
36+
37+
- name: Cache Composer Files
38+
uses: actions/cache@v3
4039
with:
4140
path: ${{ steps.composer-cache.outputs.dir }}
42-
key: ${{ runner.os }}-composer-${{ matrix.stability }}-${{ matrix.flags }}-${{ hashFiles('**/composer.lock') }}
41+
key: ${{ runner.os }}-php${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('composer.lock') }}
4342
restore-keys: |
44-
${{ runner.os }}-composer-${{ matrix.stability }}-${{ matrix.flags }}-
43+
${{ runner.os }}-php${{ matrix.php }}-${{ matrix.stability }}-composer-
44+
${{ runner.os }}-php${{ matrix.php }}-composer-
4545
${{ runner.os }}-composer-
4646
47+
# -----------------------
48+
# vendor/ cache
49+
# -----------------------
50+
- name: Cache vendor directory
51+
id: vendor-cache
52+
uses: actions/cache@v3
53+
with:
54+
path: vendor
55+
key: vendor-${{ runner.os }}-php${{ matrix.php }}-${{ matrix.stability }}-${{ hashFiles('composer.lock') }}
56+
4757
- name: Install dependencies
48-
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}
58+
if: steps.vendor-cache.outputs.cache-hit != 'true'
59+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
60+
61+
# -----------------------
62+
# PHPUnit coverage cache
63+
# -----------------------
64+
- name: Cache PHPUnit coverage
65+
uses: actions/cache@v3
66+
with:
67+
path: build/coverage
68+
key: coverage-${{ runner.os }}-php${{ matrix.php }}-${{ github.sha }}
69+
70+
- name: Run Unit tests
71+
run: composer phpunit
72+
73+
# -----------------------------------------------------------
74+
75+
static_analysis:
76+
name: Static Analysis (PHP 8.4, prefer-stable)
77+
runs-on: ubuntu-22.04
78+
needs: tests
79+
80+
steps:
81+
- name: Checkout code
82+
uses: actions/checkout@v4
83+
84+
- name: Setup PHP 8.4
85+
uses: shivammathur/setup-php@v2
86+
with:
87+
php-version: 8.4
88+
tools: composer:v2
4989

50-
- name: Run Unit tests with coverage
51-
run: composer phpunit -- ${{ matrix.phpunit-flags }}
52-
if: ${{ matrix.php != '8.5' }}
90+
# Restore vendor cache
91+
- name: Restore vendor cache
92+
uses: actions/cache@v3
93+
with:
94+
path: vendor
95+
key: vendor-${{ runner.os }}-php8.4-prefer-stable-${{ hashFiles('composer.lock') }}
5396

54-
- name: Run Unit tests without coverage
55-
run: vendor/bin/phpunit --no-coverage
56-
if: ${{ matrix.php == '8.5' }}
97+
- name: Install dependencies if needed
98+
run: composer update --prefer-stable --no-interaction --no-progress
99+
if: always() && !exists('vendor/autoload.php')
57100

58-
- name: Run static analysis
101+
- name: Run PHPStan
59102
run: composer phpstan
60-
if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable'}}
61103

62-
- name: Run Coding style rules
104+
# -----------------------------------------------------------
105+
106+
coding_style:
107+
name: Coding Style (PHP 8.4, prefer-stable)
108+
runs-on: ubuntu-22.04
109+
needs: tests
110+
111+
steps:
112+
- name: Checkout code
113+
uses: actions/checkout@v4
114+
115+
- name: Setup PHP 8.4
116+
uses: shivammathur/setup-php@v2
117+
with:
118+
php-version: 8.4
119+
tools: composer:v2
120+
121+
# Restore vendor cache
122+
- name: Restore vendor cache
123+
uses: actions/cache@v3
124+
with:
125+
path: vendor
126+
key: vendor-${{ runner.os }}-php8.4-prefer-stable-${{ hashFiles('composer.lock') }}
127+
128+
- name: Install dependencies if needed
129+
run: composer update --prefer-stable --no-interaction --no-progress
130+
if: always() && !exists('vendor/autoload.php')
131+
132+
- name: Run PHPCS
63133
run: composer phpcs:fix
64-
if: ${{ matrix.php == '8.4' && matrix.stability == 'prefer-stable'}}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All Notable changes to `bakame/stackwatch` will be documented in this file.
1313

1414
- **BC BREAK:** improve `Report` generation validation and adds missing `Report::iterations()` method
1515
- `AggregatedMetrics::dd` and `Statistics::dd` improved implementation
16+
- **BC BREAK:** global functions are namespaced
17+
- Improve github actions
1618

1719
### Deprecated
1820

docs/1.0/stack.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ $service->calculateHeavyStuff();
1313
echo microtime(true) - $start; // the execution time of your code
1414
```
1515

16-
**StackWatch** makes this process simpler and more powerful with a set of **global helper functions**.
16+
**StackWatch** makes this process simpler and more powerful with a set of **namespaced helper functions**
17+
defined under the `Bakame\Stackwatch` namespace.
1718

1819
## Quick Profiling
1920

2021
```php
21-
$result = stack_call($service->calculateHeavyStuff(...));
22+
use function Bakame\Stackwatch\stack_call;
2223

24+
$result = stack_call($service->calculateHeavyStuff(...));
2325
$result->returnValue; // the actual return value from the callback
2426
$result->span; // profiling data (Span object)
2527
```
@@ -33,6 +35,7 @@ Use `stack_bench()` to collect execution time, memory usage, and CPU time:
3335

3436
```php
3537
use Bakame\Stackwatch\DurationUnit;
38+
use function Bakame\Stackwatch\stack_bench;
3639

3740
$metrics = stack_bench($service->calculateHeavyStuff(...));
3841
echo DurationUnit::format($metrics->executionTime); // "1.271 ms"
@@ -59,6 +62,7 @@ Choose how results are aggregated:
5962

6063
```php
6164
use Bakame\Stackwatch\AggregationType;
65+
use function Bakame\Stackwatch\stack_bench;
6266

6367
$metrics = stack_bench(
6468
callback: $service->calculateHeavyStuff(...),
@@ -87,6 +91,7 @@ For detailed statistics instead of a single number:
8791
```php
8892
use Bakame\Stackwatch\AggregationType;
8993
use Bakame\Stackwatch\MetricType;
94+
use function Bakame\Stackwatch\stack_report;
9095

9196
$report = stack_report($service->calculateHeavyStuff(...), 100);
9297
$report->row(MetricType::CpuTime)->minimum;
@@ -105,7 +110,7 @@ You can learn more about this `Statistics` and `AggregatedMetrics` in the [build
105110

106111
For quick inspection:
107112

108-
- `stack_cdump()` → dumps span/metrics, continues execution
113+
- `stack_cdump()` → dumps span/metrics, continues execution
109114
- `stack_cdd()` → dumps span/metrics, halts execution
110115
- `stack_rdump()` → dumps detailed report, continues execution
111116
- `stack_rdd()` → dumps detailed report, halts execution

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Once installed, it will allow you to
1212
## Profile any block using closures
1313

1414
```php
15+
use function Bakame\Stackwatch\stack_bench;
16+
1517
$duration = stack_bench(function () {
1618
// code that will be profiled
1719
})->executionTime;

0 commit comments

Comments
 (0)