File size: 2,448 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getDomContentLoadedEventStart, getNavigationStart } from '../api/performance-timing';
import { getResources } from '../api/resources-timing';
import type { Collector } from '../types';

export const collector: Collector = ( report ) => {
	const domContentLoaded = getDomContentLoadedEventStart() - getNavigationStart();
	if ( domContentLoaded <= 0 ) {
		return report;
	}

	/**
	 * Fetch resources that
	 * 	- are scripts
	 *  - finish downloading before domContentLoaded
	 *  - have a size (to exclude resources form a different origin)
	 */
	const blockingResources = getResources().filter(
		( r ) =>
			r.initiatorType === 'script' && r.responseEnd <= domContentLoaded && r.decodedBodySize > 0
	);

	const resourcesCount = blockingResources.length;

	// Gets when the first resource starts, and when the last resource ends
	const { start: resourcesStart, end: resourcesEnd } = blockingResources.reduce(
		( { start, end }, resource ) => ( {
			start: Math.min( start, resource.requestStart ),
			end: Math.max( end, resource.responseEnd ),
		} ),
		{ start: Infinity, end: -Infinity }
	);

	const { resourcesCompressed, resourcesUncompressed, resourcesTransferred } =
		blockingResources.reduce(
			( acc, script ) => ( {
				resourcesCompressed: acc.resourcesCompressed + script.encodedBodySize,
				resourcesUncompressed: acc.resourcesUncompressed + script.decodedBodySize,
				resourcesTransferred: acc.resourcesTransferred + script.transferSize,
			} ),
			{
				resourcesCompressed: 0,
				resourcesUncompressed: 0,
				resourcesTransferred: 0,
			}
		);

	// Hit ratio from 0 to 100 (0=nothing was cached, 100=everything comes from the cache).
	// resourcesTransferred includes the headers, but resourcesCompressed does not, so the division is a
	// bit off and can return values higher than 1. We cap it to 1 to avoid sending negative numbers
	const resourcesCacheRatio =
		100 - Math.round( Math.min( 1, resourcesTransferred / resourcesCompressed ) * 100 );

	report.data.set( 'resourcesCount', resourcesCount );
	report.data.set( 'resourcesStart', Math.round( resourcesStart ) );
	report.data.set( 'resourcesEnd', Math.round( resourcesEnd ) );
	report.data.set( 'resourcesCompressed', resourcesCompressed );
	report.data.set( 'resourcesUncompressed', resourcesUncompressed );
	report.data.set( 'resourcesTransferred', resourcesTransferred );
	report.data.set( 'resourcesCacheRatio', resourcesCacheRatio );

	return report;
};