| 
									
										
										
										
											2020-03-02 18:35:30 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2020-03-02 18:35:30 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Imports
 | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2021-03-08 13:47:46 +02:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2017-11-18 12:26:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Get branch and project name from command line arguments.
 | 
					
						
							|  |  |  | const [, , limitFile, project, branch, commit] = process.argv; | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Load sizes.
 | 
					
						
							|  |  |  | const currentSizes = JSON.parse(fs.readFileSync('/tmp/current.log', 'utf8')); | 
					
						
							|  |  |  | const allLimitSizes = JSON.parse(fs.readFileSync(limitFile, 'utf8')); | 
					
						
							|  |  |  | const limitSizes = allLimitSizes[project][branch] || allLimitSizes[project]['master']; | 
					
						
							| 
									
										
										
										
											2017-11-18 12:26:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Check current sizes against limits.
 | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | let failed = false; | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  | const successMessages = []; | 
					
						
							|  |  |  | const failureMessages = []; | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | for (const compressionType in limitSizes) { | 
					
						
							|  |  |  |   if (typeof limitSizes[compressionType] === 'object') { | 
					
						
							|  |  |  |     const limitPerFile = limitSizes[compressionType]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const filename in limitPerFile) { | 
					
						
							|  |  |  |       const expectedSize = limitPerFile[filename]; | 
					
						
							|  |  |  |       const actualSize = currentSizes[`${compressionType}/${filename}`]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 21:08:23 +02:00
										 |  |  |       if (actualSize === undefined) { | 
					
						
							|  |  |  |         failed = true; | 
					
						
							|  |  |  |         // An expected compression type/file combination is missing. Maybe the file was renamed or
 | 
					
						
							|  |  |  |         // removed. Report it as an error, so the user updates the corresponding limit file.
 | 
					
						
							| 
									
										
										
										
											2020-03-02 18:35:30 +01:00
										 |  |  |         console.error( | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  |             `ERROR: Commit ${commit} ${compressionType} ${filename} measurement is missing. ` + | 
					
						
							| 
									
										
										
										
											2018-11-23 21:08:23 +02:00
										 |  |  |             'Maybe the file was renamed or removed.'); | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         const absoluteSizeDiff = Math.abs(actualSize - expectedSize); | 
					
						
							|  |  |  |         // If size diff is larger than 1% or 500 bytes...
 | 
					
						
							|  |  |  |         if (absoluteSizeDiff > 500 || absoluteSizeDiff > expectedSize / 100) { | 
					
						
							|  |  |  |           failed = true; | 
					
						
							|  |  |  |           // We must also catch when the size is significantly lower than the payload limit, so
 | 
					
						
							|  |  |  |           // we are forced to update the expected payload number when the payload size reduces.
 | 
					
						
							|  |  |  |           // Otherwise, we won't be able to catch future regressions that happen to be below
 | 
					
						
							|  |  |  |           // the artificially inflated limit.
 | 
					
						
							|  |  |  |           const operator = actualSize > expectedSize ? 'exceeded' : 'fell below'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           failureMessages.push( | 
					
						
							| 
									
										
										
										
											2020-05-05 13:12:39 -07:00
										 |  |  |               `FAIL: Commit ${commit} ${compressionType} ${filename} ${ | 
					
						
							|  |  |  |                   operator} expected size by 500 bytes or >1% ` +
 | 
					
						
							|  |  |  |               `(expected: ${expectedSize}, actual: ${actualSize}).`); | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-05-05 13:12:39 -07:00
										 |  |  |           successMessages.push( | 
					
						
							|  |  |  |               `SUCCESS: Commit ${commit} ${compressionType} ${ | 
					
						
							|  |  |  |                   filename} did NOT cross size threshold of 500 bytes or >1% ` +
 | 
					
						
							|  |  |  |               `(expected: ${expectedSize}, actual: ${actualSize}).`); | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  | // Group failure messages separately from success messages so they are easier to find.
 | 
					
						
							| 
									
										
										
										
											2020-03-02 18:35:30 +01:00
										 |  |  | successMessages.concat(failureMessages).forEach(message => console.error(message)); | 
					
						
							| 
									
										
										
										
											2019-11-21 11:42:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | if (failed) { | 
					
						
							| 
									
										
										
										
											2021-03-08 13:47:46 +02:00
										 |  |  |   const projectRoot = path.resolve(__dirname, '../..'); | 
					
						
							|  |  |  |   const limitFileRelPath = path.relative(projectRoot, limitFile); | 
					
						
							| 
									
										
										
										
											2020-05-05 13:12:39 -07:00
										 |  |  |   console.info( | 
					
						
							| 
									
										
										
										
											2021-03-08 13:47:46 +02:00
										 |  |  |       `If this is a desired change, please update the size limits in file '${limitFileRelPath}'.`); | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  |   process.exit(1); | 
					
						
							|  |  |  | } else { | 
					
						
							| 
									
										
										
										
											2020-03-02 18:35:30 +01:00
										 |  |  |   console.info(`Payload size check passed. All diffs are less than 1% or 500 bytes.`); | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | } |