-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubStressTest.php
More file actions
101 lines (85 loc) · 2.87 KB
/
Copy pathGitHubStressTest.php
File metadata and controls
101 lines (85 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use PHPUnit\Framework\TestCase;
class GitHubStressTest extends TestCase
{
private $driver;
private const LOGIN_URL = 'https://github.com/login';
private const MAX_RESPONSE_TIME = 5.0; // seconds
protected function setUp(): void
{
$options = new ChromeOptions();
$options->addArguments(['--no-sandbox', '--headless']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$this->driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);
}
/**
* @test
* @dataProvider loadTestDataProvider
*/
public function testLoginPageLoadTime($iterations)
{
$loadTimes = [];
for ($i = 0; $i < $iterations; $i++) {
$start = microtime(true);
try {
$this->driver->get(self::LOGIN_URL);
$loadTime = microtime(true) - $start;
$loadTimes[] = $loadTime;
$this->assertLessThan(
self::MAX_RESPONSE_TIME,
$loadTime,
"Response time exceeded threshold on iteration {$i}"
);
$this->logMetrics($i, $loadTime);
$this->driver->manage()->deleteAllCookies();
sleep(1); // Prevent rate limiting
} catch (\Exception $e) {
$this->fail("Iteration {$i} failed: " . $e->getMessage());
}
}
$this->outputStats($loadTimes);
}
public static function loadTestDataProvider(): array
{
return [
'small load test' => [10],
'medium load test' => [50],
'large load test' => [100]
];
}
private function logMetrics($iteration, $loadTime): void
{
$metrics = [
'iteration' => $iteration,
'timestamp' => date('Y-m-d H:i:s'),
'response_time' => round($loadTime, 3),
'memory_usage' => memory_get_usage(true)
];
file_put_contents(
'output/stress_test_metrics.log',
json_encode($metrics) . PHP_EOL,
FILE_APPEND
);
}
private function outputStats(array $loadTimes): void
{
$avg = array_sum($loadTimes) / count($loadTimes);
$max = max($loadTimes);
echo sprintf(
"\nTest Statistics:\nAvg Response: %.2fs\nMax Response: %.2fs\n",
$avg,
$max
);
}
protected function tearDown(): void
{
if ($this->driver) {
$this->driver->quit();
}
}
}