Skip to content

Commit

Permalink
Track entire web build directory size in web_size__compile_test (#115…
Browse files Browse the repository at this point in the history
…682)

* wip

* add track entire web build output dir size

* add more fields

* migrate metrics to use bytes rather than kb

* update keys

* use -9 on tar cz

* delete tempDir and tar first before measuring size
  • Loading branch information
christopherfujino authored Nov 30, 2022
1 parent 2ef2cc8 commit e62b6e7
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions dev/devicelab/lib/tasks/perf_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1339,8 +1339,17 @@ class WebCompileTest {
'--no-pub',
]);
watch?.stop();
final String outputFileName = path.join(directory, 'build/web/main.dart.js');
metrics.addAll(await getSize(outputFileName, metric: metric));
final String buildDir = path.join(directory, 'build', 'web');
metrics.addAll(await getSize(
directories: <String, String>{'web_build_dir': buildDir},
files: <String, String>{
'dart2js': path.join(buildDir, 'main.dart.js'),
'canvaskit_wasm': path.join(buildDir, 'canvaskit', 'canvaskit.wasm'),
'canvaskit_js': path.join(buildDir, 'canvaskit', 'canvaskit.js'),
'flutter_js': path.join(buildDir, 'flutter.js'),
},
metric: metric,
));

if (measureBuildTime) {
metrics['${metric}_dart2js_millis'] = watch!.elapsedMilliseconds;
Expand All @@ -1350,22 +1359,57 @@ class WebCompileTest {
});
}

/// Obtains the size and gzipped size of a file given by [fileName].
static Future<Map<String, int>> getSize(String fileName, {required String metric}) async {
/// Obtains the size and gzipped size of both [dartBundleFile] and [buildDir].
static Future<Map<String, int>> getSize({
/// Mapping of metric key name to file system path for directories to measure
Map<String, String> directories = const <String, String>{},
/// Mapping of metric key name to file system path for files to measure
Map<String, String> files = const <String, String>{},
required String metric,
}) async {
const String kGzipCompressionLevel = '-9';
final Map<String, int> sizeMetrics = <String, int>{};

final ProcessResult result = await Process.run('du', <String>['-k', fileName]);
sizeMetrics['${metric}_dart2js_size'] = _parseDu(result.stdout as String);
final Directory tempDir = Directory.systemTemp.createTempSync('perf_tests_gzips');
try {
for (final MapEntry<String, String> entry in files.entries) {
final String key = entry.key;
final String filePath = entry.value;
sizeMetrics['${metric}_${key}_uncompressed_bytes'] = File(filePath).lengthSync();

await Process.run('gzip',<String>['--keep', kGzipCompressionLevel, filePath]);
// gzip does not provide a CLI option to specify an output file, so
// instead just move the output file to the temp dir
final File compressedFile = File('$filePath.gz')
.renameSync(path.join(tempDir.absolute.path, '$key.gz'));
sizeMetrics['${metric}_${key}_compressed_bytes'] = compressedFile.lengthSync();
}

await Process.run('gzip',<String>['-k', '9', fileName]);
final ProcessResult resultGzip = await Process.run('du', <String>['-k', '$fileName.gz']);
sizeMetrics['${metric}_dart2js_size_gzip'] = _parseDu(resultGzip.stdout as String);
for (final MapEntry<String, String> entry in directories.entries) {
final String key = entry.key;
final String dirPath = entry.value;

return sizeMetrics;
}
final String tarball = path.join(tempDir.absolute.path, '$key.tar');
await Process.run('tar', <String>[
'--create',
'--verbose',
'--file=$tarball',
dirPath,
]);
sizeMetrics['${metric}_${key}_uncompressed_bytes'] = File(tarball).lengthSync();

static int _parseDu(String source) {
return int.parse(source.split(RegExp(r'\s+')).first.trim());
// get size of compressed build directory
await Process.run('gzip',<String>['--keep', kGzipCompressionLevel, tarball]);
sizeMetrics['${metric}_${key}_compressed_bytes'] = File('$tarball.gz').lengthSync();
}
} finally {
try {
tempDir.deleteSync(recursive: true);
} on FileSystemException {
print('Failed to delete ${tempDir.path}.');
}
}
return sizeMetrics;
}
}

Expand Down

0 comments on commit e62b6e7

Please sign in to comment.