Skip to content

Commit 3538515

Browse files
committed
feat: Adicionar suporte a variáveis de ambiente para configuração de testes de desempenho e iterações
1 parent 55bebc1 commit 3538515

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

docs/testing/CI_CONFIGURATION.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# CI/CD Configuration for PivotPHP Tests
2+
3+
## Environment Variables for Test Stability
4+
5+
To ensure stable tests across different environments (local development, CI/CD, production), PivotPHP supports the following environment variables:
6+
7+
### Performance Test Configuration
8+
9+
#### `PIVOTPHP_PERFORMANCE_THRESHOLD`
10+
- **Purpose**: Sets the minimum requests per second threshold for performance tests
11+
- **Default**:
12+
- Local development: `500` req/s
13+
- CI environments: `250` req/s
14+
- **Usage**: `export PIVOTPHP_PERFORMANCE_THRESHOLD=300`
15+
16+
#### `PIVOTPHP_CONCURRENT_REQUESTS`
17+
- **Purpose**: Sets the number of concurrent requests for stress tests
18+
- **Default**:
19+
- Local development: `10000` requests
20+
- CI environments: `5000` requests
21+
- **Usage**: `export PIVOTPHP_CONCURRENT_REQUESTS=3000`
22+
23+
#### `PIVOTPHP_TEST_ITERATIONS`
24+
- **Purpose**: Sets the number of iterations for integration tests
25+
- **Default**:
26+
- Local development: `500` iterations
27+
- CI environments: `250` iterations
28+
- **Usage**: `export PIVOTPHP_TEST_ITERATIONS=100`
29+
30+
## CI Environment Detection
31+
32+
The framework automatically detects CI environments by checking for:
33+
- `CI=true`
34+
- `GITHUB_ACTIONS=true`
35+
- `TRAVIS=true`
36+
37+
When a CI environment is detected, more conservative default values are used to prevent flaky tests.
38+
39+
## Example GitHub Actions Configuration
40+
41+
```yaml
42+
env:
43+
PIVOTPHP_PERFORMANCE_THRESHOLD: 200
44+
PIVOTPHP_CONCURRENT_REQUESTS: 2000
45+
PIVOTPHP_TEST_ITERATIONS: 100
46+
```
47+
48+
## Example Local Development
49+
50+
```bash
51+
# For slower development machines
52+
export PIVOTPHP_PERFORMANCE_THRESHOLD=100
53+
export PIVOTPHP_CONCURRENT_REQUESTS=1000
54+
export PIVOTPHP_TEST_ITERATIONS=50
55+
56+
# Run tests
57+
composer test
58+
```
59+
60+
## Benefits
61+
62+
1. **Consistent Test Results**: Tests adapt to environment capabilities
63+
2. **Reduced Flakiness**: Conservative thresholds in CI environments
64+
3. **Flexibility**: Easy to override for specific needs
65+
4. **Performance Insights**: Still validates performance while being realistic
66+
67+
## Test Groups
68+
69+
Performance tests are organized into groups:
70+
- `@group stress` - High-load stress tests
71+
- `@group high-performance` - Performance validation tests
72+
73+
To skip performance tests in CI:
74+
```bash
75+
vendor/bin/phpunit --exclude-group stress,high-performance
76+
```

tests/Integration/V11ComponentsTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ protected function setUp(): void
2929
$this->app = new Application();
3030
}
3131

32+
/**
33+
* Get test iteration count based on environment
34+
*/
35+
private function getTestIterationCount(): int
36+
{
37+
// Allow environment override
38+
if ($envCount = getenv('PIVOTPHP_TEST_ITERATIONS')) {
39+
return (int) $envCount;
40+
}
41+
42+
// Reduce iterations for CI environments
43+
if (getenv('CI') || getenv('GITHUB_ACTIONS') || getenv('TRAVIS')) {
44+
return 250; // Half the iterations for CI
45+
}
46+
47+
// Default for local development
48+
return 500;
49+
}
50+
3251
/**
3352
* Test high-performance mode integration
3453
*/
@@ -380,7 +399,8 @@ function ($req, $res) {
380399
$results = [];
381400
$startTime = microtime(true);
382401

383-
for ($i = 0; $i < 500; $i++) {
402+
$iterations = $this->getTestIterationCount();
403+
for ($i = 0; $i < $iterations; $i++) {
384404
// Mix of read and write operations
385405
if ($i % 3 === 0) {
386406
$request = new Request('GET', '/api/users/' . $i, '/api/users/' . $i);
@@ -406,7 +426,7 @@ function ($req, $res) {
406426
$metrics = $monitor->getLiveMetrics();
407427

408428
// Since Application doesn't auto-track requests, we check other metrics
409-
// Memory pressure should be reasonable after processing 500 requests
429+
// Memory pressure should be reasonable after processing requests
410430
$this->assertLessThan(1, $metrics['memory_pressure'], 'Memory pressure should be < 100%');
411431

412432
// Verify monitor is initialized and working

tests/Stress/HighPerformanceStressTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,44 @@ protected function setUp(): void
2727
$this->metrics = [];
2828
}
2929

30+
/**
31+
* Get performance threshold based on environment
32+
*/
33+
private function getPerformanceThreshold(): int
34+
{
35+
// Allow environment override for CI/CD
36+
if ($envThreshold = getenv('PIVOTPHP_PERFORMANCE_THRESHOLD')) {
37+
return (int) $envThreshold;
38+
}
39+
40+
// Detect CI environment and use more conservative thresholds
41+
if (getenv('CI') || getenv('GITHUB_ACTIONS') || getenv('TRAVIS')) {
42+
return 250; // More conservative for CI
43+
}
44+
45+
// Default for local development
46+
return 500;
47+
}
48+
49+
/**
50+
* Get number of concurrent requests based on environment
51+
*/
52+
private function getConcurrentRequestCount(): int
53+
{
54+
// Allow environment override
55+
if ($envCount = getenv('PIVOTPHP_CONCURRENT_REQUESTS')) {
56+
return (int) $envCount;
57+
}
58+
59+
// Reduce load for CI environments
60+
if (getenv('CI') || getenv('GITHUB_ACTIONS') || getenv('TRAVIS')) {
61+
return 5000; // Half the load for CI
62+
}
63+
64+
// Default for local development
65+
return 10000;
66+
}
67+
3068
/**
3169
* Test concurrent request handling under extreme load
3270
*
@@ -38,7 +76,7 @@ public function testConcurrentRequestHandling(): void
3876
// Enable extreme performance mode
3977
HighPerformanceMode::enable(HighPerformanceMode::PROFILE_EXTREME);
4078

41-
$concurrentRequests = 10000;
79+
$concurrentRequests = $this->getConcurrentRequestCount();
4280
$results = [];
4381
$startTime = microtime(true);
4482

@@ -58,7 +96,12 @@ public function testConcurrentRequestHandling(): void
5896
$duration = (microtime(true) - $startTime) * 1000;
5997
$throughput = $concurrentRequests / ($duration / 1000);
6098

61-
$this->assertGreaterThan(500, $throughput, 'Should handle >500 req/s');
99+
$threshold = $this->getPerformanceThreshold();
100+
$this->assertGreaterThan(
101+
$threshold,
102+
$throughput,
103+
"Should handle >{$threshold} req/s (env: " . (getenv('CI') ? 'CI' : 'local') . ")"
104+
);
62105

63106
// Check memory efficiency
64107
$memoryPerRequest = (memory_get_peak_usage(true) - memory_get_usage(true)) / $concurrentRequests;

0 commit comments

Comments
 (0)