agent/browser/ie/pagetest/PagetestReporting.cpp
/*-----------------------------------------------------------------------------
	Check each js or html element to make sure it has been minified
-----------------------------------------------------------------------------*/
Check each js or html element to make sure it has been minified
Check each js or html element to make sure it has been minified
void CPagetestReporting::CheckMinify()
{
	minifyScore = -1;
	int count = 0;
	int total = 0;
	DWORD totalBytes = 0;
	DWORD targetBytes = 0;
	ATLTRACE(_T("[Pagetest] - CheckMinify\n"));
	POSITION pos = events.GetHeadPosition();
	while( pos )
	{
		CTrackedEvent * e = events.GetNext(pos);
		if( e && e->type == CTrackedEvent::etWinInetRequest && !e->ignore )
		{
			CWinInetRequest * w = (CWinInetRequest *)e;
			CString mime = w->response.contentType;
			mime.MakeLower();
			LPBYTE body = w->body;
			DWORD bodyLen = w->bodyLen;
			if( w->fromNet && w->result == 200 && 
					((mime.Find(_T("javascript")) >= 0 || mime.Find(_T("json")) >= 0) && 
					body && bodyLen))
			{
				count++;
				bool failed = false;
				totalBytes += w->in;
				DWORD target = w->in;
				DWORD origSize = w->in;
				DWORD origLen = bodyLen;
				DWORD headSize = w->inHeaders.GetLength();
				if( w->in < 1400 )
					w->minifyScore = 100;
				else
				{
					DWORD len = origLen + 1;
					char * minified = new char [len];
					JSMin jsmin;
					if( jsmin.Minify((const char *)body, minified, len) && len )
					{
						CString enc = w->response.contentEncoding;
						enc.MakeLower();
						if( enc.Find(_T("gzip")) >= 0 )
						{
							DWORD cLen = compressBound(len);
							if( cLen )
							{
								LPBYTE buff = new BYTE[cLen];
								if( compress2(buff, &cLen, (LPBYTE)minified, len, 9) == Z_OK )
									target = cLen + headSize;
								delete [] buff;
							}
						}
						else
							target = len + headSize;
					}
					delete [] minified;
					if( target > origSize )
						target = origSize;
					DWORD savings = origSize - target;
					if( savings > 5120 || ((double)target <= (double)origSize * 0.9) )
						w->minifyScore = 0;
					else if( savings > 1024 )
						w->minifyScore = 50;
					else
					{
						w->minifyScore = 100;
						target = origSize;
					}
				}
				targetBytes += target;
				w->minifyTotal = w->in;
				w->minifyTarget = target;
				total += w->minifyScore;
			}
		}
	}
	minifyTotal = totalBytes;
	minifyTarget = targetBytes;
	if( count && totalBytes )
		minifyScore = targetBytes * 100 / totalBytes;
}
void CPagetestReporting :: CheckMinify ( ) { minifyScore = -1 ; int count = 0 ; int total = 0 ; DWORD totalBytes = 0 ; DWORD targetBytes = 0 ; ATLTRACE ( _T ( " \n " ) ) ; POSITION pos = events . GetHeadPosition ( ) ; while ( pos ) { CTrackedEvent * e = events . GetNext ( pos ) ; if ( e && e -> type == CTrackedEvent :: etWinInetRequest && ! e -> ignore ) { CWinInetRequest * w = ( CWinInetRequest * ) e ; CString mime = w -> response . contentType ; mime . MakeLower ( ) ; LPBYTE body = w -> body ; DWORD bodyLen = w -> bodyLen ; if ( w -> fromNet && w -> result == 200 && ( ( mime . Find ( _T ( " " ) ) >= 0 || mime . Find ( _T ( " " ) ) >= 0 ) && body && bodyLen ) ) { count ++ ; bool failed = false ; totalBytes += w -> in ; DWORD target = w -> in ; DWORD origSize = w -> in ; DWORD origLen = bodyLen ; DWORD headSize = w -> inHeaders . GetLength ( ) ; if ( w -> in < 1400 ) w -> minifyScore = 100 ; else { DWORD len = origLen + 1 ; char * minified = new char [ len ] ; JSMin jsmin ; if ( jsmin . Minify ( ( const char * ) body , minified , len ) && len ) { CString enc = w -> response . contentEncoding ; enc . MakeLower ( ) ; if ( enc . Find ( _T ( " " ) ) >= 0 ) { DWORD cLen = compressBound ( len ) ; if ( cLen ) { LPBYTE buff = new BYTE [ cLen ] ; if ( compress2 ( buff , & cLen , ( LPBYTE ) minified , len , 9 ) == Z_OK ) target = cLen + headSize ; delete [ ] buff ; } } else target = len + headSize ; } delete [ ] minified ; if ( target > origSize ) target = origSize ; DWORD savings = origSize - target ; if ( savings > 5120 || ( ( double ) target <= ( double ) origSize * 0.9 ) ) w -> minifyScore = 0 ; else if ( savings > 1024 ) w -> minifyScore = 50 ; else { w -> minifyScore = 100 ; target = origSize ; } } targetBytes += target ; w -> minifyTotal = w -> in ; w -> minifyTarget = target ; total += w -> minifyScore ; } } } minifyTotal = totalBytes ; minifyTarget = targetBytes ; if ( count && totalBytes ) minifyScore = targetBytes * 100 / totalBytes ; }
Check each js or html element to make sure it has been minified
Check each js or html element to make sure it has been minified
// run JSMin on the file and see how much smaller it is
 // if the original content was gzipped, gzip the minified result as well
 // if minification saves 10% or more then it fails
