-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubHomePagePerformanceTest.php
More file actions
126 lines (111 loc) · 4.33 KB
/
Copy pathGitHubHomePagePerformanceTest.php
File metadata and controls
126 lines (111 loc) · 4.33 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Exception\WebDriverCurlException;
class GitHubHomePagePerformanceTest extends TestCase
{
private $webDriver;
private $metrics = [];
private $startTime;
private const MAX_RETRIES = 3;
private const TIMEOUT = 60; // seconds
protected function setUp(): void
{
$options = new ChromeOptions();
$options->addArguments([
'--headless',
'--disable-gpu',
'--no-sandbox',
'--disable-dev-shm-usage',
'--ignore-certificate-errors'
]);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
// Initialize WebDriver with retry logic
$this->initializeWebDriver($capabilities);
$this->startTime = microtime(true);
}
private function initializeWebDriver($capabilities)
{
$retry = 0;
while ($retry < self::MAX_RETRIES) {
try {
$this->webDriver = RemoteWebDriver::create(
'http://localhost:9515',
$capabilities,
self::TIMEOUT * 1000, // connection timeout in milliseconds
self::TIMEOUT * 1000 // request timeout in milliseconds
);
return;
} catch (\Exception $e) {
$retry++;
if ($retry === self::MAX_RETRIES) {
throw new RuntimeException(
"Failed to initialize WebDriver after {$retry} attempts: " . $e->getMessage()
);
}
sleep(2); // Wait before retry
}
}
}
public function testGitHubHomepagePerformance()
{
try {
// Initial page load time with retry
$loadSuccess = false;
$retry = 0;
while (!$loadSuccess && $retry < self::MAX_RETRIES) {
try {
$start = microtime(true);
$this->webDriver->get('https://github.com');
$initialLoadTime = microtime(true) - $start;
$this->metrics['initial_load'] = round($initialLoadTime, 2);
$loadSuccess = true;
} catch (WebDriverCurlException $e) {
$retry++;
if ($retry === self::MAX_RETRIES) {
throw $e;
}
sleep(2);
}
}
$domStart = microtime(true);
$this->webDriver->wait(self::TIMEOUT)->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector('body')
)
);
$domLoadTime = microtime(true) - $domStart;
$this->metrics['dom_ready'] = round($domLoadTime, 2);
$this->metrics['memory_usage'] = round(memory_get_usage() / 1024 / 1024, 2);
// Performance assertions
$this->assertLessThan(5.0, $initialLoadTime, 'Initial page load too slow');
$this->assertLessThan(2.0, $domLoadTime, 'DOM ready time too slow');
$this->assertLessThan(50, $this->metrics['memory_usage'], 'Memory usage too high');
$this->webDriver->wait(self::TIMEOUT)->until(
WebDriverExpectedCondition::elementToBeClickable(
WebDriverBy::cssSelector('.logged-out, .logged-in')
)
);
} catch (\Exception $e) {
$this->metrics['error'] = $e->getMessage();
throw $e;
}
}
protected function tearDown(): void
{
$totalTime = microtime(true) - $this->startTime;
$this->metrics['total_time'] = round($totalTime, 2);
file_put_contents(
__DIR__ . 'output/homepage_performance_report.json',
json_encode($this->metrics, JSON_PRETTY_PRINT)
);
if ($this->webDriver) {
$this->webDriver->quit();
}
}
}