How to Fetch YSlow Beacon Result and Save as HTML

How to Fetch YSlow Beacon Result and Save as HTML

Introduction
YSlow analyzes web page performance by examining all the components on the page, including components dynamically created by using `JavaScript`. It measures the page's performance and offers suggestions for improvement. YSlow is integrated into the Firebug web development tool for Firefox.

On this tutorial, we will learn how to fetch YSlow beacon result and save it as html.


Requirements

- Firefox
- Firebug plugin
- YSlow plugin

Procedures

- Create a profile dedicated to that with only Firebug and YSlow.
firefox.exe -P yslow

- On pop-up, click `Create Profile` button and name the profile as `yslow`, then click `Start Firefox`.

- In Firefox address bar, enter the text
about:config

- In the filter field, enter the text
yslow*

- Set the following:
extensions.firebug.yslow.beaconInfo => all
extensions.firebug.yslow.beaconUrl => http://example.com/yslow.php?p=apw&
extensions.firebug.yslow.optinBeacon => true

- Copy and save the code below as yslow.php under /EXAMPLE_COM_WEB_DIRECTORY
<?php

$projectname = isset($_GET['p']) ? $_GET['p'] : null;

if(is_null($projectname) || empty($GLOBALS['HTTP_RAW_POST_DATA']))
{
    die;
}

$File = 'yslow.html';

$Handle = fopen($File, 'w+');

$str = '<style>body {font-family:arial,sans-serif;font-size:11px;} div {border:solid 1px #ccddee;padding:3px;margin:3px;}</style>';

$arr = objectToArray(json_decode($GLOBALS['HTTP_RAW_POST_DATA']));

$str .= '<h1>YSlow report: '.$projectname.'</h1>
<b>Grade</b>: '.getscore($arr['o']).'<br>
<b>Overall performance score</b>: '.$arr['o'].'<br>
<b>URL</b>: '.urldecode($arr['u']).'<br>
<b>Total page size</b>: '.$arr['w'].'<br>
<b>Number of requests</b>: '.$arr['r'].'<br>
<b>Page load time</b>: '.$arr['lt'].'
';

$str .= arrayToHtml($arr);

fwrite($Handle, $str."\n");
fclose($Handle);

function objectToArray( $object )
{
    if( !is_object( $object ) && !is_array( $object ) )
    {
        return $object;
    }
    if( is_object( $object ) )
    {
        $object = get_object_vars( $object );
    }
    return array_map( 'objectToArray', $object );
}

function arrayToHtml($arr)
{
    $str = '';
    foreach($arr as $k => $v)
    {
        if(is_array($v))
        {
            $str .= '<div>'.'<b>'.$k.'</b>: '.getdefinition($k)." \n".arrayToTable($v).'</div>'."\n";
        }
        else
        {
            $grade = ($k==='score' || $k==='o') ? ' <b>Grade</b>: '.getscore($v) : null;
            $str .= '<div>'.'<b>'.$k.'</b>: '.urldecode($v).$grade.'</div>'."\n";
        }
    }
    return $str;
}

function getscore($val)
{
    if($val >= 90)
        return 'A';
    if($val >= 80)
        return 'B';
    if($val >= 70)
        return 'C';
    if($val >= 60)
        return 'D';
    return 'F';
}

function getdefinition($i)
{
switch ($i) {
    case 'ynumreq':
        $def = 'score for Make fewer HTTP Requests<br>Decreasing the number of components on a page reduces the number of HTTP requests required to render the page, resulting in faster page loads. Some ways to reduce the number of components include: combine files, combine multiple scripts into one script, combine multiple CSS files into one style sheet, and use CSS Sprites and image maps.';
        break;
    case 'ycdn':
        $def = 'score for Use a Content Delivery Network(CDN)<br>User proximity to web servers impacts response times. Deploying content across multiple geographically dispersed servers helps users perceive that pages are loading faster.';
        break;
    case 'yexpires':
        $def = 'score for Add Expires headers<br>Web pages are becoming increasingly complex with more scripts, style sheets, images, and Flash on them. A first-time visit to a page may require several HTTP requests to load all the components. By using Expires headers these components become cacheable, which avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often associated with images, but they can and should be used on all page components including scripts, style sheets, and Flash.';
        break;
    case 'ycompress':
        $def = 'score for Compress components with gzip<br>Compression reduces response times by reducing the size of the HTTP response. Gzip is the most popular and effective compression method currently available and generally reduces the response size by about 70%. Approximately 90% of today\'s Internet traffic travels through browsers that claim to support gzip.';
        break;
    case 'ycsstop':
        $def = 'score for Put CSS at top<br>Moving style sheets to the document HEAD element helps pages appear to load quicker since this allows pages to render progressively.';
        break;
    case 'yjsbottom':
        $def = 'score for Put JavaScript at bottom<br>JavaScript scripts block parallel downloads; that is, when a script is downloading, the browser will not start any other downloads. To help the page load faster, move scripts to the bottom of the page if they are deferrable.';
        break;
    case 'yexpressions':
        $def = 'score for Avoid CSS expressions<br>CSS expressions (supported in IE beginning with Version 5) are a powerful, and dangerous, way to dynamically set CSS properties. These expressions are evaluated frequently: when the page is rendered and resized, when the page is scrolled, and even when the user moves the mouse over the page. These frequent evaluations degrade the user experience.';
        break;
    case 'yexternal':
        $def = 'score for Make JavaScript and CSS external<br>Using external JavaScript and CSS files generally produces faster pages because the files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded each time the HTML document is requested. This reduces the number of HTTP requests but increases the HTML document size. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the HTML document size is reduced without increasing the number of HTTP requests.';
        break;
    case 'ydns':
        $def = 'score for Reduce DNS lookups<br>The Domain Name System (DNS) maps hostnames to IP addresses, just like phonebooks map people\'s names to their phone numbers. When you type URL www.yahoo.com into the browser, the browser contacts a DNS resolver that returns the server\'s IP address. DNS has a cost; typically it takes 20 to 120 milliseconds for it to look up the IP address for a hostname. The browser cannot download anything from the host until the lookup completes.';
        break;
    case 'yminify':
        $def = 'score for Minify JavaScript and CSS<br>Minification removes unnecessary characters from a file to reduce its size, thereby improving load times. When a file is minified, comments and unneeded white space characters (space, newline, and tab) are removed. This improves response time since the size of the download files is reduced.';
        break;
    case 'yredirects':
        $def = 'score for Avoid URL redirects<br>URL redirects are made using HTTP status codes 301 and 302. They tell the browser to go to another location. Inserting a redirect between the user and the final HTML document delays everything on the page since nothing on the page can be rendered and no components can be downloaded until the HTML document arrives.';
        break;
    case 'ydupes':
        $def = 'score for Remove duplicate JavasScript and CSS<br>Duplicate JavaScript and CSS files hurt performance by creating unnecessary HTTP requests (IE only) and wasted JavaScript execution (IE and Firefox). In IE, if an external script is included twice and is not cacheable, it generates two HTTP requests during page loading. Even if the script is cacheable, extra HTTP requests occur when the user reloads the page. In both IE and Firefox, duplicate JavaScript scripts cause wasted time evaluating the same scripts more than once. This redundant script execution happens regardless of whether the script is cacheable.';
        break;
    case 'yetags':
        $def = 'score for Configure entity tags (ETags)<br>Entity tags (ETags) are a mechanism web servers and the browser use to determine whether a component in the browser\'s cache matches one on the origin server. Since ETags are typically constructed using attributes that make them unique to a specific server hosting a site, the tags will not match when a browser gets the original component from one server and later tries to validate that component on a different server.';
        break;
    case 'yxhr':
        $def = 'score for Use GET for AJAX requests<br>One of AJAX\'s benefits is it provides instantaneous feedback to the user because it requests information asynchronously from the backend web server. However, using AJAX does not guarantee the user will not wait for the asynchronous JavaScript and XML responses to return. Optimizing AJAX responses is important to improve performance, and making the responses cacheable is the best way to optimize them.';
        break;
    case 'yxhrmethod':
        $def = 'score for Use GET for AJAX requests<br>When using the XMLHttpRequest object, the browser implements POST in two steps: (1) send the headers, and (2) send the data. It is better to use GET instead of POST since GET sends the headers and the data together (unless there are many cookies). IE\'s maximum URL length is 2 KB, so if you are sending more than this amount of data you may not be able to use GET.';
        break;
    case 'ymindom':
        $def = 'score for Reduce the number of DOM elements<br>A complex page means more bytes to download, and it also means slower DOM access in JavaScript. Reduce the number of DOM elements on the page to improve performance.';
        break;
    case 'yno404':
        $def = 'score for Avoid HTTP 404 (Not Found) error<br>Making an HTTP request and receiving a 404 (Not Found) error is expensive and degrades the user experience. Some sites have helpful 404 messages (for example, "Did you mean ...?"), which may assist the user, but server resources are still wasted.';
        break;
    case 'ymincookie':
        $def = 'score for Reduce cookie size<br>HTTP cookies are used for authentication, personalization, and other purposes. Cookie information is exchanged in the HTTP headers between web servers and the browser, so keeping the cookie size small minimizes the impact on response time.';
        break;
    case 'ycookiefree':
        $def = 'score for Use cookie-free domains<br>When the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic. To workaround this problem, make sure that static components are requested with cookie-free requests by creating a subdomain and hosting them there.';
        break;
    case 'ynofilter':
        $def = 'score for Avoid AlphaImageLoader filter<br>The IE-proprietary AlphaImageLoader filter attempts to fix a problem with semi-transparent true color PNG files in IE versions less than Version 7. However, this filter blocks rendering and freezes the browser while the image is being downloaded. Additionally, it increases memory consumption. The problem is further multiplied because it is applied per element, not per image.';
        break;
    case 'yimgnoscale':
        $def = 'score for Do not scale images in HTML<br>Web page designers sometimes set image dimensions by using the width and height attributes of the HTML image element. Avoid doing this since it can result in images being larger than needed. For example, if your page requires image myimg.jpg which has dimensions 240x720 but displays it with dimensions 120x360 using the width and height attributes, then the browser will download an image that is larger than necessary.';
        break;
    case 'yfavicon':
        $def = 'score for Make favicon small and cacheable<br>A favicon is an icon associated with a web page; this icon resides in the favicon.ico file in the server\'s root. Since the browser requests this file, it needs to be present; if it is missing, the browser returns a 404 error (see "Avoid HTTP 404 (Not Found) error" above). Since favicon.ico resides in the server\'s root, each time the browser requests this file, the cookies for the server\'s root are sent. Making the favicon small and reducing the cookie size for the server\'s root cookies improves performance for retrieving the favicon. Making favicon.ico cacheable avoids frequent requests for it.';
        break;
    default:
       $def = '';
}
return $def;
}


- On Firefox's address bar, enter the url of site to be tested.

- Run YSlow. (Open Firebug window, click `YSlow` Tab, click `Run Test`).

- View output http://example.com/yslow.html

References
YSlow User Guide, http://developer.yahoo.com/yslow/help/

YSlow Guide: Beacons, http://developer.yahoo.com/yslow/help/beacons.html

Comments

Unknown said…
its not working :(

the yslow.html file is being created . .but is empty . .no data is been written into it . .

Popular posts from this blog

Remote Deployment with Ant and Rsync

Site Performance Enhancement