@@ -1339,8 +1339,17 @@ class WebCompileTest {
13391339 '--no-pub' ,
13401340 ]);
13411341 watch? .stop ();
1342- final String outputFileName = path.join (directory, 'build/web/main.dart.js' );
1343- metrics.addAll (await getSize (outputFileName, metric: metric));
1342+ final String buildDir = path.join (directory, 'build' , 'web' );
1343+ metrics.addAll (await getSize (
1344+ directories: < String , String > {'web_build_dir' : buildDir},
1345+ files: < String , String > {
1346+ 'dart2js' : path.join (buildDir, 'main.dart.js' ),
1347+ 'canvaskit_wasm' : path.join (buildDir, 'canvaskit' , 'canvaskit.wasm' ),
1348+ 'canvaskit_js' : path.join (buildDir, 'canvaskit' , 'canvaskit.js' ),
1349+ 'flutter_js' : path.join (buildDir, 'flutter.js' ),
1350+ },
1351+ metric: metric,
1352+ ));
13441353
13451354 if (measureBuildTime) {
13461355 metrics['${metric }_dart2js_millis' ] = watch! .elapsedMilliseconds;
@@ -1350,22 +1359,57 @@ class WebCompileTest {
13501359 });
13511360 }
13521361
1353- /// Obtains the size and gzipped size of a file given by [fileName] .
1354- static Future <Map <String , int >> getSize (String fileName, {required String metric}) async {
1362+ /// Obtains the size and gzipped size of both [dartBundleFile] and [buildDir] .
1363+ static Future <Map <String , int >> getSize ({
1364+ /// Mapping of metric key name to file system path for directories to measure
1365+ Map <String , String > directories = const < String , String > {},
1366+ /// Mapping of metric key name to file system path for files to measure
1367+ Map <String , String > files = const < String , String > {},
1368+ required String metric,
1369+ }) async {
1370+ const String kGzipCompressionLevel = '-9' ;
13551371 final Map <String , int > sizeMetrics = < String , int > {};
13561372
1357- final ProcessResult result = await Process .run ('du' , < String > ['-k' , fileName]);
1358- sizeMetrics['${metric }_dart2js_size' ] = _parseDu (result.stdout as String );
1373+ final Directory tempDir = Directory .systemTemp.createTempSync ('perf_tests_gzips' );
1374+ try {
1375+ for (final MapEntry <String , String > entry in files.entries) {
1376+ final String key = entry.key;
1377+ final String filePath = entry.value;
1378+ sizeMetrics['${metric }_${key }_uncompressed_bytes' ] = File (filePath).lengthSync ();
1379+
1380+ await Process .run ('gzip' ,< String > ['--keep' , kGzipCompressionLevel, filePath]);
1381+ // gzip does not provide a CLI option to specify an output file, so
1382+ // instead just move the output file to the temp dir
1383+ final File compressedFile = File ('$filePath .gz' )
1384+ .renameSync (path.join (tempDir.absolute.path, '$key .gz' ));
1385+ sizeMetrics['${metric }_${key }_compressed_bytes' ] = compressedFile.lengthSync ();
1386+ }
13591387
1360- await Process . run ( 'gzip' , < String > [ '-k' , '9' , fileName]);
1361- final ProcessResult resultGzip = await Process . run ( 'du' , < String > [ '-k' , '$ fileName .gz' ]) ;
1362- sizeMetrics[ '${ metric }_dart2js_size_gzip' ] = _parseDu (resultGzip.stdout as String ) ;
1388+ for ( final MapEntry <String , String > entry in directories.entries) {
1389+ final String key = entry.key ;
1390+ final String dirPath = entry.value ;
13631391
1364- return sizeMetrics;
1365- }
1392+ final String tarball = path.join (tempDir.absolute.path, '$key .tar' );
1393+ await Process .run ('tar' , < String > [
1394+ '--create' ,
1395+ '--verbose' ,
1396+ '--file=$tarball ' ,
1397+ dirPath,
1398+ ]);
1399+ sizeMetrics['${metric }_${key }_uncompressed_bytes' ] = File (tarball).lengthSync ();
13661400
1367- static int _parseDu (String source) {
1368- return int .parse (source.split (RegExp (r'\s+' )).first.trim ());
1401+ // get size of compressed build directory
1402+ await Process .run ('gzip' ,< String > ['--keep' , kGzipCompressionLevel, tarball]);
1403+ sizeMetrics['${metric }_${key }_compressed_bytes' ] = File ('$tarball .gz' ).lengthSync ();
1404+ }
1405+ } finally {
1406+ try {
1407+ tempDir.deleteSync (recursive: true );
1408+ } on FileSystemException {
1409+ print ('Failed to delete ${tempDir .path }.' );
1410+ }
1411+ }
1412+ return sizeMetrics;
13691413 }
13701414}
13711415
0 commit comments