This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
/
optimization.inc
600 lines (535 loc) · 22 KB
/
optimization.inc
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
require_once __DIR__ . '/include/TestPaths.php';
/**
* Re-format the optimization report to make it prettier
*
* @param mixed $testPath
* @param mixed $run
* @param mixed $cached
* @return string The optimization report
*/
function dumpOptimizationReport(&$pageData, &$requests, $id, $run, $cached, &$test)
{
$localPaths = new TestPaths(GetTestPath($id), $run, $cached, 1);
return dumpOptimizationReportForStep($localPaths, $pageData, $requests, $test["testinfo"], 1);
}
/**
* Re-format the optimization report to make it prettier
*
* @param TestPaths $localPaths Step specific test paths
* @param array $pageData Raw step results array
* @param array $requests Requests of the result
* @param array $testinfoArray The "testinfo" array
* @param int $step The step number (used for ids)
* @return string The optimization report
*/
function dumpOptimizationReportForStep($localPaths, &$pageData, &$requests, $testinfoArray, $step)
{
$out = "";
if( $pageData && $requests )
{
$out .= ReportTTFB($localPaths, $pageData, $testinfoArray, $step);
$out .= ReportKeepAlive($pageData, $requests, $step);
$out .= ReportTransferCompression($pageData, $requests, $step);
$out .= ReportImageCompression($pageData, $requests, $step);
$out .= ReportProgressiveJpeg($pageData, $requests, $step);
$out .= ReportCache($pageData, $requests, $step);
$out .= ReportCDN($pageData, $requests, $step);
}
return $out;
}
/**
* @param TestPaths $localPaths
* @param array $pageData
* @param array $testinfoArray
* @param int $step The step number (used for id)
* @return string The TTFB report
*/
function ReportTTFB($localPaths, $pageData, $testinfoArray, $step)
{
require_once __DIR__ . '/optimization_detail.inc.php';
$ttfb = (int) $pageData["TTFB"];
$latency = isset($testinfoArray['latency']) ? $testinfoArray['latency'] : null;
$score = gradeTTFBForStep($ttfb, $latency, $localPaths, $target);
$out = "";
if( isset($score) )
{
$out .= "<a name=\"first_byte_time\"><h3 id='ttfb_step$step'>First Byte Time (back-end processing): $score/100</h3></a><p class=\"indented1\">\n";
$out .= "<b>$ttfb ms</b> First Byte Time<br>\n";
$out .= "<b>$target ms</b> Target First Byte Time<br>\n";
}
else
$out .= "<a name=\"first_byte_time\"><h3>First Byte Time (back-end processing): N/A</h3></a><p class=\"indented1\">\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportKeepAlive(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_keep-alive']) && $pageData['score_keep-alive'] >= 0 )
{
$score = "{$pageData['score_keep-alive']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_keep-alive']) && $request['score_keep-alive'] >= 0 && $request['score_keep-alive'] < 100 )
{
$key = $request['host'] . ' ' . $index;
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value = 'FAILED - ' . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
ksort($report);
}
$out .= "<a name=\"keep_alive_enabled\"><h3 id='keep-alive_step$step'>Use persistent connections (keep alive): $score</h3></a><p class=\"indented1\">\n";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportTransferCompression(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_gzip']) && $pageData['score_gzip'] >= 0 )
{
$original = number_format($pageData['gzip_total'] / 1024.0, 1);
$save = number_format($pageData['gzip_savings'] / 1024.0, 1);
$target = number_format(($pageData['gzip_total'] - $pageData['gzip_savings']) / 1024.0, 1);
$summary = "$original KB total in compressible text, target size = $target KB - potential savings = <b>$save KB</b>";
$score = "{$pageData['score_gzip']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_gzip']) && $request['score_gzip'] >= 0 && $request['score_gzip'] < 100 )
{
$key = $request['gzip_save'] . ' ' . $request['host'] . ' ' . $index;
$original = number_format($request['gzip_total'] / 1024.0, 1);
$save = number_format($request['gzip_save'] / 1024.0, 1);
$target = number_format(($request['gzip_total'] - $request['gzip_save']) / 1024.0, 1);
if( $request['score_gzip'] < 50 )
$value = 'FAILED - ';
else
$value = 'WARNING - ';
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value .= "($original KB, compressed = $target KB - savings of <b>$save KB</b>) - " . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
krsort($report, SORT_NUMERIC);
}
$out .= "<a name=\"compress_text\"><h3 id='gzip_step$step'>Use gzip compression for transferring compressable responses: $score</h3></a>\n";
if( isset($summary) )
$out .= "<p>$summary</p>\n";
$out .= "<p class=\"indented1\">";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportImageCompression(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_compress']) && $pageData['score_compress'] >= 0 )
{
$original = number_format($pageData['image_total'] / 1024.0, 1);
$save = number_format($pageData['image_savings'] / 1024.0, 1);
$target = number_format(($pageData['image_total'] - $pageData['image_savings']) / 1024.0, 1);
$summary = "$original KB total in images, target size = $target KB - potential savings = <b>$save KB</b>";
$score = "{$pageData['score_compress']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_compress']) && $request['score_compress'] >= 0 && $request['score_compress'] < 100 )
{
$key = $request['image_save'] . ' ' . $request['host'] . ' ' . $index;
$original = number_format($request['image_total'] / 1024.0, 1);
$save = number_format($request['image_save'] / 1024.0, 1);
$target = number_format(($request['image_total'] - $request['image_save']) / 1024.0, 1);
if( $request['score_compress'] < 50 )
$value = 'FAILED - ';
else
$value = 'WARNING - ';
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value .= "($original KB, compressed = $target KB - savings of <b>$save KB</b>) - " . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
krsort($report, SORT_NUMERIC);
}
$out .= "<a name=\"compress_images\"><h3 id='image_compression_step$step'>Compress Images: $score</h3></a>\n";
if( isset($summary) )
$out .= "<p>$summary</p>\n";
$out .= "<p class=\"indented1\">";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
function ReportProgressiveJpeg(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_progressive_jpeg']) && $pageData['score_progressive_jpeg'] >= 0 )
{
$score = "{$pageData['score_progressive_jpeg']}/100";
$candidateBytes = 0;
$progressiveBytes = 0;
foreach( $requests as $index => &$request )
{
if (array_key_exists('score_progressive_jpeg', $request) && $request['score_progressive_jpeg'] >= 0) {
if ($request['score_progressive_jpeg'] < 100 ) {
$key = $request['image_total'] . ' ' . $request['host'] . ' ' . $index;
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$kb = number_format($request['image_total'] / 1024.0, 1);
if ($request['score_progressive_jpeg'] > 0 )
$report[$key] = "Info ($kb KB) - " . $proto . $request['host'] . $request['url'];
else {
$candidateBytes += $request['image_total'];
$report[$key] = "FAILED ($kb KB) - " . $proto . $request['host'] . $request['url'];
}
} else {
$candidateBytes += $request['image_total'];
$progressiveBytes += $request['image_total'];
}
}
}
if ($candidateBytes > 0) {
$pct = number_format($progressiveBytes * 100.0 / $candidateBytes, 0);
$candidateBytes = number_format($candidateBytes / 1024.0, 1);
$progressiveBytes = number_format($progressiveBytes / 1024.0, 1);
$summary = "$progressiveBytes KB of a possible $candidateBytes KB ($pct%) were from progressive JPEG images";
}
krsort($report, SORT_NUMERIC);
}
$out .= "<a name=\"progressive_jpeg\"><h3 id='progressive_jpeg_step$step'>Use Progressive JPEGs: $score</h3></a>\n";
if( isset($summary) )
$out .= "<p>$summary</p>\n";
$out .= "<p class=\"indented1\">";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportCache(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_cache']) && $pageData['score_cache'] >= 0 )
{
$score = "{$pageData['score_cache']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_cache']) && $request['score_cache'] >= 0 && $request['score_cache'] < 100 )
{
$key = $request['cache_time'] . ' ' . $request['host'] . ' ' . $index;
if( $request['score_cache'] < 50 )
$value = 'FAILED - ';
else
$value = 'WARNING - ';
$time = 'No max-age or expires';
if( $request['cache_time'] > 0 )
{
if( $request['cache_time'] > 86400 )
$time = number_format($request['cache_time'] / 86400.0, 1) . ' days';
elseif( $request['cache_time'] > 3600 )
$time = number_format($request['cache_time'] / 3600.0, 1) . ' hours';
elseif( $request['cache_time'] > 60 )
$time = number_format($request['cache_time'] / 60.0, 1) . ' minutes';
else
$time = $request['cache_time'] . ' seconds';
}
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value .= "(<b>$time</b>) - " . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
ksort($report, SORT_NUMERIC);
}
$out .= "<a name=\"cache_static_content\"><h3 id='caching_step$step'>Leverage browser caching of static assets: $score</h3></a>\n";
$out .= "<p class=\"indented1\">";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportCombine(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_combine']) && $pageData['score_combine'] >= 0 )
{
$score = "{$pageData['score_combine']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_combine']) && $request['score_combine'] >= 0 && $request['score_combine'] < 100 )
{
$key = $request['contentType'] . ' ' . $request['host'] . ' ' . $index;
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value = 'FAILED - ' . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
ksort($report);
}
$out .= "<a name=\"combine_js_css_files\"><h3 id='combine_step$step'>Combine static CSS and JS files: $score</h3></a><p class=\"indented1\">\n";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportCDN(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
$cdns = array();
if( isset($pageData['score_cdn']) && $pageData['score_cdn'] >= 0 )
{
$score = "{$pageData['score_cdn']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_cdn']) && $request['score_cdn'] >= 0 && $request['score_cdn'] < 100 )
{
$key = $request['host'] . ' ' . $index;
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value = 'FAILED - ' . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
elseif( isset($request['cdn_provider']) )
{
$cdns[$request['host']] = $request['cdn_provider'];
}
}
ksort($report);
}
$out .= "<a name=\"use_of_cdn\"><h3 id='cdn_step$step'>Use a CDN for all static assets: $score</h3></a><p class=\"indented1\">\n";
foreach( $report as $entry )
$out .= "$entry<br>\n";
if( count($cdns) )
{
$out .= "<br>CDN's Used:<br>";
foreach( $cdns as $host => $provider )
$out .= "$host : $provider<br>";
}
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportMinify(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_minify']) && $pageData['score_minify'] >= 0 )
{
$original = number_format($pageData['minify_total'] / 1024.0, 1);
$save = number_format($pageData['minify_savings'] / 1024.0, 1);
$target = number_format(($pageData['minify_total'] - $pageData['minify_savings']) / 1024.0, 1);
$summary = "$original KB total in minifiable text, target size = $target KB - potential savings = <b>$save KB</b>";
$score = "{$pageData['score_minify']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_minify']) && $request['score_minify'] >= 0 && $request['score_minify'] < 100 )
{
$key = $request['minify_save'] . ' ' . $request['host'] . ' ' . $index;
$original = number_format($request['minify_total'] / 1024.0, 1);
$save = number_format($request['minify_save'] / 1024.0, 1);
$target = number_format(($request['minify_total'] - $request['minify_save']) / 1024.0, 1);
if( $request['score_minify'] < 50 )
$value = 'FAILED - ';
else
$value = 'WARNING - ';
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value .= "($original KB, minified = $target KB - savings of <b>$save KB</b>) - " . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
krsort($report, SORT_NUMERIC);
}
$out .= "<a name=\"minify_js\"><h3 id='minify_step$step'>Minify JS: $score</h3></a>\n";
if( isset($summary) )
$out .= "<p>$summary</p>\n";
$out .= "<p class=\"indented1\">";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
*
*
* @param mixed $requests
*/
function ReportCookies(&$pageData, &$requests, $step)
{
$out = "";
$score = 'N/A';
$report = array();
if( isset($pageData['score_cookies']) && $pageData['score_cookies'] >= 0 )
{
$score = "{$pageData['score_cookies']}/100";
foreach( $requests as $index => &$request )
{
if( isset($request['score_cookies']) && $request['score_cookies'] >= 0 && $request['score_cookies'] < 100 )
{
$key = $request['host'] . ' ' . $index;
$proto = 'http://';
if( $request['is_secure'] )
$proto = 'https://';
$value = 'FAILED - ' . $proto . $request['host'] . $request['url'];
$report[$key] = $value;
}
}
ksort($report);
}
$out .= "<a name=\"cookies\"><h3 id='cookies_step$step'>Proper cookie usage: $score</h3></a><p class=\"indented1\">\n";
foreach( $report as $entry )
$out .= "$entry<br>\n";
$out .= "</p><br>\n";
return $out;
}
/**
* Display a glossary for the optimization results
*
* @param mixed $settings
*/
function dumpOptimizationGlossary(&$settings)
{
?>
<hr>
<h2>Glossary:</h2>
<p></p>
<table border="1" cellpadding="5px" cellspacing="0" style="width:100%">
<tr><th class="nowrap" rowspan=2 colspan=1>First Byte Time</th>
<td class="nowrap">Applicable Objects</td>
<td >Time to First Byte for the page (back-end processing + redirects)</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >The target time is the time needed for the DNS, socket and SSL negotiations + 100ms. A single letter grade will be deducted for every 100ms beyond the target.</td>
</tr>
<tr class="blank"></tr>
<tr><th class="nowrap" rowspan=2 colspan=1>Keep-Alive</th>
<td class="nowrap">Applicable Objects</td>
<td >All objects that are from a domain that serves more than one object for the page (i.e. if only a single object is served from a given domain it will not be checked)</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >The response header contains a "keep-alive" directive or the same socket was used for more than one object from the given host</td>
</tr>
<tr class="blank"></tr>
<tr><th class="nowrap" rowspan=2 colspan=1>GZIP Text</th>
<td class="nowrap">Applicable Objects</td>
<td >All objects with a mime type of "text/*" or "*javascript*"</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >Transfer-encoding is checked to see if it is gzip. If it is not then the file is compressed and the percentage of compression
is the result (so a page that can save 30% of the size of it's text by compressing would yield a 70% test result)</td>
</tr>
<tr class="blank"></tr>
<tr><th rowspan=2 colspan=1>Compress Images</th>
<td class="nowrap">Applicable Objects</td>
<td >JPEG Images</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >Within 10% of a photoshop quality 50 will pass, up to 50% larger will warn and anything larger than that will fail.<br>
The overall score is the percentage of image bytes that can be saved by re-compressing the images.
</td>
</tr>
<tr class="blank"></tr>
<tr><th rowspan=2 colspan=1>Use Progressive JPEGs</th>
<td class="nowrap">Applicable Objects</td>
<td >All JPEG Images</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >Each JPEG image is checked and the resulting score is the percentage of JPEG bytes that were served as progressive images relative to the total JPEG bytes.</td>
</tr>
<tr class="blank"></tr>
<tr><th class="nowrap" rowspan=2 colspan=1>Cache Static</th>
<td class="nowrap">Applicable Objects</td>
<td >Any non-html object with a mime type of "text/*", "*javascript*" or "image/*" that does not
explicitly have an Expires header of 0 or -1, a cache-control header of "private",
"no-store" or "no-cache" or a pragma header of "no-cache"</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >
An "Expires" header is present (and is not 0 or -1) or a
"cache-control: max-age" directive is present and set for an
hour or greater. If the expiration is set for less than 30
days you will get a warning (only applies to max-age currently).
</td>
</tr>
<tr class="blank"></tr>
<tr><th class="nowrap" rowspan=2 colspan=1>Use A CDN</th>
<td class="nowrap">Applicable Objects</td>
<td >All static non-html content (css, js and images)</td>
</tr>
<tr>
<td class="nowrap">What is checked</td>
<td >Checked to see if it is hosted on a known CDN (CNAME mapped to a known CDN network). 80% of the static resources need to be served from a CDN for the overall page to be considered using a CDN. The current list of known CDN's is <a href="https://github.com/WPO-Foundation/webpagetest/blob/master/agent/wpthook/cdn.h">here</a></td>
</tr>
</table>
<?php
}
?>