Skip to content

Commit

Permalink
Run dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
michalowskim committed Aug 19, 2024
1 parent 2653101 commit d6f655b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ of SVG optimization in real-world application scenarios.

Key Points:

- The benchmarking was performed on an example application that utilizes the `svg_optimizer`package.
- The benchmarking was performed on an example application that utilizes the `svg_optimizer` package.
- A comprehensive test covering all SVG files included in the project was conducted.
- The results indicated a notable improvement, with a ~30% reduction in the time required to render SVGs on-screen.
- The testing device used for this benchmark was an iPhone 15 Plus.
15 changes: 10 additions & 5 deletions benchmark/lib/benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ class Benchmark {
// Reset pubspec.yaml to original form before making changes
yamlFile.writeAsStringSync(originalYaml.toString());

final List<dynamic> currentAssets = jsonDecode(jsonEncode(editor.parseAt(['flutter', 'assets']).value));
final List<dynamic> currentAssets =
jsonDecode(jsonEncode(editor.parseAt(['flutter', 'assets']).value));

late final int nonOptimizedBuildSize;
late final int optimizedBuildSize;

// Append assets to pubspec.yaml without transformer
projectSize.getAssetRecords(false).forEach((element) => editor.appendToList(['flutter', 'assets'], element));
projectSize.getAssetRecords(false).forEach(
(element) => editor.appendToList(['flutter', 'assets'], element));
yamlFile.writeAsStringSync(editor.toString());

// Make a benchmark with non optimized SVG files
Directory.current = Directory('../example');
print('Building without svg_optimizer...');
final ProcessResult nonOptimizedResult = await Process.run('flutter', platform.buildArguments);
final ProcessResult nonOptimizedResult =
await Process.run('flutter', platform.buildArguments);
if (nonOptimizedResult.exitCode != 0) {
print(nonOptimizedResult.stderr);
throw Exception('Failed to build non optimized project');
Expand All @@ -49,13 +52,15 @@ class Benchmark {
// Reset previous changes to assets in pubspec.yaml
editor.update(['flutter', 'assets'], currentAssets);
// Append assets to pubspec.yaml with transformer
projectSize.getAssetRecords(true).forEach((element) => editor.appendToList(['flutter', 'assets'], element));
projectSize.getAssetRecords(true).forEach(
(element) => editor.appendToList(['flutter', 'assets'], element));
yamlFile.writeAsStringSync(editor.toString());

// Make a benchmark with optimized SVG files
print('Building with svg_optimizer...');
Directory.current = Directory('../example');
final ProcessResult optimizedResult = await Process.run('flutter', platform.buildArguments);
final ProcessResult optimizedResult =
await Process.run('flutter', platform.buildArguments);
if (optimizedResult.exitCode != 0) {
print(optimizedResult.stderr);
throw Exception('Failed to build optimized project');
Expand Down
3 changes: 2 additions & 1 deletion benchmark/lib/benchmark_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class BenchmarkResult {
});

@override
String toString() => 'Build size without svg_optimizer: ${FileSizeFormatter.format(nonOptimizedBuildSize)}\n'
String toString() =>
'Build size without svg_optimizer: ${FileSizeFormatter.format(nonOptimizedBuildSize)}\n'
'Build size with svg_optimizer: ${FileSizeFormatter.format(optimizedBuildSize)}\n'
'Optimized build size is ${FileSizeFormatter.format(nonOptimizedBuildSize - optimizedBuildSize)} smaller '
'(${((nonOptimizedBuildSize - optimizedBuildSize) / nonOptimizedBuildSize * 100).toStringAsFixed(2)}%)\n';
Expand Down
9 changes: 6 additions & 3 deletions benchmark/lib/project_size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ enum ProjectSize {
}

extension ProjectSizeExtension on ProjectSize {
List<Map<String, dynamic>> getAssetRecords(bool withTransformer) => switch (this) {
List<Map<String, dynamic>> getAssetRecords(bool withTransformer) =>
switch (this) {
ProjectSize.small => [
AssetRecordUtils.getAssetRecord(_smallProjectPath, withTransformer),
],
ProjectSize.medium => [
AssetRecordUtils.getAssetRecord(_smallProjectPath, withTransformer),
AssetRecordUtils.getAssetRecord(_mediumProjectPath, withTransformer),
AssetRecordUtils.getAssetRecord(
_mediumProjectPath, withTransformer),
],
ProjectSize.big => [
AssetRecordUtils.getAssetRecord(_smallProjectPath, withTransformer),
AssetRecordUtils.getAssetRecord(_mediumProjectPath, withTransformer),
AssetRecordUtils.getAssetRecord(
_mediumProjectPath, withTransformer),
AssetRecordUtils.getAssetRecord(_bigProjectPath, withTransformer),
],
};
Expand Down
3 changes: 2 additions & 1 deletion benchmark/svg_optimizer_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Future<void> main(List<String> arguments) async {
final ArgParser argParser = buildParser();
final ArgResults results = argParser.parse(arguments);

SupportedPlatform? platform = SupportedPlatform.values.firstWhereOrNull((element) => results[element.value] == true);
SupportedPlatform? platform = SupportedPlatform.values
.firstWhereOrNull((element) => results[element.value] == true);

if (platform == null) {
print('Please provide platform argument');
Expand Down
4 changes: 3 additions & 1 deletion benchmark/utils/asset_record_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
abstract class AssetRecordUtils {
static Map<String, dynamic> getAssetRecord(String path, bool withTransformer) => <String, dynamic>{
static Map<String, dynamic> getAssetRecord(
String path, bool withTransformer) =>
<String, dynamic>{
'path': path,
if (withTransformer)
'transformers': [
Expand Down
18 changes: 11 additions & 7 deletions example/lib/benchmarks/loading_svg_benchmark_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ class _BenchmarkPageState extends State<BenchmarkPage> {
}

Future<List<String>> getSvgPaths() async {
final List<String> bigProjectImages = await getImagePaths('assets/benchmark_svg/big_project');
final List<String> mediumProjectImages = await getImagePaths('assets/benchmark_svg/medium_project');
final List<String> smallProjectImages = await getImagePaths('assets/benchmark_svg/small_project');
final List<String> bigProjectImages =
await getImagePaths('assets/benchmark_svg/big_project');
final List<String> mediumProjectImages =
await getImagePaths('assets/benchmark_svg/medium_project');
final List<String> smallProjectImages =
await getImagePaths('assets/benchmark_svg/small_project');
return bigProjectImages + mediumProjectImages + smallProjectImages;
}

Future<List<String>> getImagePaths(String path) async => jsonDecode(await rootBundle.loadString('AssetManifest.json'))
.keys
.where((String key) => key.contains(path))
.toList();
Future<List<String>> getImagePaths(String path) async =>
jsonDecode(await rootBundle.loadString('AssetManifest.json'))
.keys
.where((String key) => key.contains(path))
.toList();
}

const Key picturesListKey = ValueKey('picturesListKey');
Expand Down
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class _MyHomePageState extends State<MyHomePage> {
children: <Widget>[
const Text('Non optimized svg:'),
FutureBuilder(
future: rootBundle.loadString('assets/svg_no_optimize/heart.svg'),
future:
rootBundle.loadString('assets/svg_no_optimize/heart.svg'),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!);
Expand Down
3 changes: 2 additions & 1 deletion lib/optimize/optimizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void optimizeSvg(List<String> arguments) {
print(argParser.usage);
exit(1);
} on ProcessException catch (_) {
print('It looks like svgo is not configured properly. To install it, refer to README file.');
print(
'It looks like svgo is not configured properly. To install it, refer to README file.');
exit(1);
} catch (e) {
print(e.toString());
Expand Down

0 comments on commit d6f655b

Please sign in to comment.