Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track entire web build directory size in web_size__compile_test #115682

Merged
merged 7 commits into from
Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1328,8 +1328,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'),
},
Comment on lines +1333 to +1339
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this extensible!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gratitude still stands.

metric: metric,
));

if (measureBuildTime) {
metrics['${metric}_dart2js_millis'] = watch!.elapsedMilliseconds;
Expand All @@ -1339,22 +1348,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');
christopherfujino marked this conversation as resolved.
Show resolved Hide resolved
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