-
Notifications
You must be signed in to change notification settings - Fork 28
/
MageCacheWarmer.php
260 lines (215 loc) · 7.89 KB
/
MageCacheWarmer.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* A simple full page cache warmer for Magento 1 and 2.
*
* Minimal PHP dependencies:
* Filter extension - http://php.net/manual/en/book.filter.php
* SimpleXML extension - http://php.net/manual/en/book.simplexml.php
* allow_url_fopen = 1 - http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
*
* Should work on old versions of PHP going back to 5.2?
*/
class MageCacheWarmer
{
const MAX_TEST_URLS = 10;
private
$_iUnsecure,
$_iDelay,
$_sSitemapUrl,
$_aSiteUrls,
$_iNumUrls,
$_cStatusCallback,
$_fAvgDownloadTime,
$_iTotalDownloadTime,
$_rStreamContext;
public function getAvgDownloadTime() { return $this->_fAvgDownloadTime; }
public function getTotalDownloadTime() { return $this->_iTotalDownloadTime; }
/**
* Download the sitemap for testing / warming.
*/
public function __construct($sSitemapUrl, $cStatusCallback, $iDelay, $iUnsecure)
{
$this->_sSitemapUrl = $sSitemapUrl;
$this->_cStatusCallback = $cStatusCallback;
$this->_iDelay = $iDelay;
$this->_iUnsecure = $iUnsecure;
$this->_rStreamContext = $this->_createStreamContext();
}
/**
* Download and parse the sitemap.
*
* @note This must be called before test() or warm().
*
* @note $sSitemapXml is a local variable,
* there's no point to keep it in memory for the life of the object
*/
public function loadSitemap()
{
$sSitemapXml = $this->_downloadSitemap($this->_sSitemapUrl);
$this->_parseSitemap($sSitemapXml);
return $this;
}
/**
* Hit a random subset of the site's URLs to gauge performance.
*/
public function test()
{
$aTestUrls = $this->_aSiteUrls;
$iNumUrls = $this->_iNumUrls;
// Truncate the list of URLs to self::MAX_TEST_URLS if the site has more
if($this->_iNumUrls > self::MAX_TEST_URLS) {
$aTestUrls = self::array_random($this->_aSiteUrls, self::MAX_TEST_URLS);
$iNumUrls = self::MAX_TEST_URLS;
}
call_user_func($this->_cStatusCallback, "Testing with $iNumUrls URLs" . PHP_EOL);
$this->_run($aTestUrls);
call_user_func(
$this->_cStatusCallback,
"Average page time is {$this->_fAvgDownloadTime}" . PHP_EOL);
}
/**
* Test the site to get an initial reading of its performance.
* Then run the tool across the given set of URLs.
* Lastly, test the site again so we can determine the performance gain from caching.
*/
public function warm()
{
// Run the initial test
$this->test($this->_sSitemapUrl);
$fOrigAvgTime = $this->_fAvgDownloadTime;
// Now warm the cache for the entire site
call_user_func(
$this->_cStatusCallback,
PHP_EOL . "Warming {$this->_iNumUrls} URLs" . PHP_EOL);
$this->_run($this->_aSiteUrls);
call_user_func($this->_cStatusCallback, PHP_EOL);
// Finally, test the site again
$this->test($this->_sSitemapUrl);
$fCachedAvgTime = $this->_fAvgDownloadTime;
// Return the speedup as a percentage of the original performance
$fChange = self::calcChange($fOrigAvgTime, $fCachedAvgTime);
return round(100 * $fChange, 2);
}
/**
* Calculate the relative difference between a starting and ending time.
* You would multiply this by 100 and round by 2 to see a human readable value.
*/
static public function calcChange($fStartingTime, $fEndingTime)
{
$fMinTime = min($fEndingTime, $fStartingTime);
$fMaxTime = max($fEndingTime, $fStartingTime);
$fChange = $fMaxTime - $fMinTime;
if($fChange < .01) {
return 0;
}
$fDelta = $fChange / $fStartingTime;
return $fDelta;
}
/**
* Download the URLs, timing each one
*/
private function _run(array $aUrls)
{
$iNumUrls = count($aUrls);
$iTotalDownloadTime = 0;
foreach($aUrls as $i => $sUrl) {
// Log the request
$iCur = $i + 1;
call_user_func(
$this->_cStatusCallback,
"($iCur/{$iNumUrls}) - Fetching " . $sUrl . PHP_EOL);
// Note the start time and download the page
$iPageStartTime = microtime(true);
file_get_contents($sUrl, false, $this->_rStreamContext);
// Update the total download time
$iTotalDownloadTime += microtime(true) - $iPageStartTime;
// Sleep between requests if we're told to
if($this->_iDelay > 0) {
sleep($this->_iDelay);
}
}
// Store the average download time
$this->_fAvgDownloadTime = $iTotalDownloadTime * 1000 / $iNumUrls;
$this->_iTotalDownloadTime = $iTotalDownloadTime;
}
/**
* Validate the sitemap url, download the sitemap and store it on this object
*/
private function _downloadSitemap($sSitemapUrl)
{
// Grab the sitemap URL from the CLI and verify it looks like a URL
if(filter_var($sSitemapUrl, FILTER_VALIDATE_URL) === false) {
throw new InvalidArgumentException(
"$sSitemapUrl is not a valid URL" . PHP_EOL);
}
$this->_sSitemapUrl = $sSitemapUrl;
// Try downloading the sitemap file
$sSitemapXml = file_get_contents($sSitemapUrl, false, $this->_rStreamContext );
if(!$sSitemapXml) {
throw new RuntimeException(
"Unable to download the sitemap file at $sSitemapUrl" . PHP_EOL);
}
return $sSitemapXml;
}
/**
* Parse the sitemap into structures we can use for further processing.
*/
private function _parseSitemap($sSitemapXml)
{
// Try to parse the sitemap file via Simple XML
try {
$oSitemap = new SimpleXMLElement($sSitemapXml);
} catch(Exception $e) {
throw new RuntimeException(
'Failed to parse the sitemap file' . PHP_EOL . $e->getMessage() . PHP_EOL);
}
// Extract the list of URLs from the sitemap that we intend to crawl
$aDocNamespaces = $oSitemap->getDocNamespaces();
$sXmlns = array_shift($aDocNamespaces);
$oSitemap->registerXPathNamespace('sitemap', $sXmlns);
$this->_aSiteUrls = $oSitemap->xpath("//sitemap:loc");
$this->_iNumUrls = count($this->_aSiteUrls);
}
private function _createStreamContext()
{
// Stream context for file_get_contents(),
// some webservers return a 503 error when no user agent is set.
$streamContext['http'] = array(
'header' => array(
'User-Agent: WFPC Cache Warmer'
)
);
if($this->_iUnsecure) {
$streamContext['ssl'] = array(
'verify_peer' => false,
'verify_peer_name' => false,
);
}
return stream_context_create($streamContext);
}
/**
* Format milliseconds nicely.
*/
static public function format_milli($ms)
{
$ms = (int)$ms;
return
floor($ms/3600000) . ':' . // hours
floor($ms/60000) . ':' . // minutes
floor(($ms % 60000) / 1000) . '.' . // seconds
str_pad(floor($ms % 1000), 3, '0', STR_PAD_LEFT); // milliseconds
}
/**
* Randomly select items from an array
* I think I lifted this from somehwere, replace or credit said source...
*/
static public function array_random(array $arr, $num=1)
{
shuffle($arr);
$r = array();
for($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
}