| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.serialization_helper; | 5 library dart2js.serialization_helper; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:compiler/src/commandline_options.dart'; | 10 import 'package:compiler/src/commandline_options.dart'; |
| 11 import 'package:compiler/src/common/names.dart'; | 11 import 'package:compiler/src/common/names.dart'; |
| 12 import 'package:compiler/src/compiler.dart'; | 12 import 'package:compiler/src/compiler.dart'; |
| 13 import 'package:compiler/src/elements/elements.dart'; | 13 import 'package:compiler/src/elements/elements.dart'; |
| 14 | 14 |
| 15 import '../memory_compiler.dart'; | 15 import '../memory_compiler.dart'; |
| 16 import 'test_data.dart'; | 16 import 'test_data.dart'; |
| 17 | 17 |
| 18 const String DEFAULT_DATA_FILE_NAME = 'out.data'; | 18 const String DEFAULT_DATA_FILE_NAME = 'out.data'; |
| 19 | 19 |
| 20 class Arguments { | 20 class Arguments { |
| 21 final String filename; | 21 final String filename; |
| 22 final int start; | 22 final int start; |
| 23 final int end; | 23 final int end; |
| 24 final bool loadSerializedData; | 24 final bool loadSerializedData; |
| 25 final bool saveSerializedData; | 25 final bool saveSerializedData; |
| 26 final String serializedDataFileName; | 26 final String serializedDataFileName; |
| 27 final bool verbose; | 27 final bool verbose; |
| 28 | 28 |
| 29 const Arguments({ | 29 const Arguments( |
| 30 this.filename, | 30 {this.filename, |
| 31 this.start, | 31 this.start, |
| 32 this.end, | 32 this.end, |
| 33 this.loadSerializedData: false, | 33 this.loadSerializedData: false, |
| 34 this.saveSerializedData: false, | 34 this.saveSerializedData: false, |
| 35 this.serializedDataFileName: DEFAULT_DATA_FILE_NAME, | 35 this.serializedDataFileName: DEFAULT_DATA_FILE_NAME, |
| 36 this.verbose: false}); | 36 this.verbose: false}); |
| 37 | 37 |
| 38 factory Arguments.from(List<String> arguments) { | 38 factory Arguments.from(List<String> arguments) { |
| 39 String filename; | 39 String filename; |
| 40 int start; | 40 int start; |
| 41 int end; | 41 int end; |
| 42 for (String arg in arguments) { | 42 for (String arg in arguments) { |
| 43 if (!arg.startsWith('-')) { | 43 if (!arg.startsWith('-')) { |
| 44 int index = int.parse(arg, onError: (_) => null); | 44 int index = int.parse(arg, onError: (_) => null); |
| 45 if (index == null) { | 45 if (index == null) { |
| 46 filename = arg; | 46 filename = arg; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 64 } | 64 } |
| 65 return new Arguments( | 65 return new Arguments( |
| 66 filename: filename, | 66 filename: filename, |
| 67 start: start, | 67 start: start, |
| 68 end: end, | 68 end: end, |
| 69 verbose: verbose, | 69 verbose: verbose, |
| 70 loadSerializedData: loadSerializedData, | 70 loadSerializedData: loadSerializedData, |
| 71 saveSerializedData: saveSerializedData); | 71 saveSerializedData: saveSerializedData); |
| 72 } | 72 } |
| 73 | 73 |
| 74 Future forEachTest( | 74 Future forEachTest(SerializedData serializedData, List<Test> tests, |
| 75 SerializedData serializedData, | |
| 76 List<Test> tests, | |
| 77 TestFunction testFunction) async { | 75 TestFunction testFunction) async { |
| 78 Uri entryPoint = Uri.parse('memory:main.dart'); | 76 Uri entryPoint = Uri.parse('memory:main.dart'); |
| 79 int first = start ?? 0; | 77 int first = start ?? 0; |
| 80 int last = end ?? tests.length; | 78 int last = end ?? tests.length; |
| 81 | 79 |
| 82 for (int index = first; index < last; index++) { | 80 for (int index = first; index < last; index++) { |
| 83 Test test = TESTS[index]; | 81 Test test = TESTS[index]; |
| 84 List<SerializedData> dataList = | 82 List<SerializedData> dataList = |
| 85 await preserializeData(serializedData, test); | 83 await preserializeData(serializedData, test); |
| 86 Map<String, String> sourceFiles = <String, String>{}; | 84 Map<String, String> sourceFiles = <String, String>{}; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 99 await testFunction(entryPoint, | 97 await testFunction(entryPoint, |
| 100 sourceFiles: sourceFiles, | 98 sourceFiles: sourceFiles, |
| 101 resolutionInputs: resolutionInputs, | 99 resolutionInputs: resolutionInputs, |
| 102 index: index, | 100 index: index, |
| 103 test: test, | 101 test: test, |
| 104 verbose: verbose); | 102 verbose: verbose); |
| 105 } | 103 } |
| 106 } | 104 } |
| 107 } | 105 } |
| 108 | 106 |
| 109 typedef Future TestFunction( | 107 typedef Future TestFunction(Uri entryPoint, |
| 110 Uri entryPoint, | |
| 111 {Map<String, String> sourceFiles, | 108 {Map<String, String> sourceFiles, |
| 112 List<Uri> resolutionInputs, | 109 List<Uri> resolutionInputs, |
| 113 int index, | 110 int index, |
| 114 Test test, | 111 Test test, |
| 115 bool verbose}); | 112 bool verbose}); |
| 116 | 113 |
| 117 Future<SerializedData> serializeDartCore( | 114 Future<SerializedData> serializeDartCore( |
| 118 {Arguments arguments: const Arguments()}) { | 115 {Arguments arguments: const Arguments()}) { |
| 119 return measure('dart:core', 'serialize', () async { | 116 return measure('dart:core', 'serialize', () async { |
| 120 Uri uri = Uri.parse('memory:${arguments.serializedDataFileName}'); | 117 Uri uri = Uri.parse('memory:${arguments.serializedDataFileName}'); |
| 121 SerializedData serializedData; | 118 SerializedData serializedData; |
| 122 if (arguments.loadSerializedData) { | 119 if (arguments.loadSerializedData) { |
| 123 File file = new File(arguments.serializedDataFileName); | 120 File file = new File(arguments.serializedDataFileName); |
| 124 if (file.existsSync()) { | 121 if (file.existsSync()) { |
| 125 print('Loading data from $file'); | 122 print('Loading data from $file'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 | 138 |
| 142 class SerializationResult { | 139 class SerializationResult { |
| 143 final Compiler compiler; | 140 final Compiler compiler; |
| 144 final SerializedData serializedData; | 141 final SerializedData serializedData; |
| 145 | 142 |
| 146 SerializationResult(this.compiler, this.serializedData); | 143 SerializationResult(this.compiler, this.serializedData); |
| 147 } | 144 } |
| 148 | 145 |
| 149 Future<SerializationResult> serialize(Uri entryPoint, | 146 Future<SerializationResult> serialize(Uri entryPoint, |
| 150 {Map<String, String> memorySourceFiles: const <String, String>{}, | 147 {Map<String, String> memorySourceFiles: const <String, String>{}, |
| 151 List<Uri> resolutionInputs: const <Uri>[], | 148 List<Uri> resolutionInputs: const <Uri>[], |
| 152 Uri dataUri, | 149 Uri dataUri, |
| 153 bool deserializeCompilationDataForTesting: false}) async { | 150 bool deserializeCompilationDataForTesting: false}) async { |
| 154 if (dataUri == null) { | 151 if (dataUri == null) { |
| 155 dataUri = Uri.parse('memory:${DEFAULT_DATA_FILE_NAME}'); | 152 dataUri = Uri.parse('memory:${DEFAULT_DATA_FILE_NAME}'); |
| 156 } | 153 } |
| 157 OutputCollector outputCollector = new OutputCollector(); | 154 OutputCollector outputCollector = new OutputCollector(); |
| 158 Compiler compiler = compilerFor( | 155 Compiler compiler = compilerFor( |
| 159 options: [Flags.resolveOnly], | 156 options: [Flags.resolveOnly], |
| 160 memorySourceFiles: memorySourceFiles, | 157 memorySourceFiles: memorySourceFiles, |
| 161 resolutionInputs: resolutionInputs, | 158 resolutionInputs: resolutionInputs, |
| 162 outputProvider: outputCollector); | 159 outputProvider: outputCollector); |
| 163 compiler.serialization.deserializeCompilationDataForTesting = | 160 compiler.serialization.deserializeCompilationDataForTesting = |
| 164 deserializeCompilationDataForTesting; | 161 deserializeCompilationDataForTesting; |
| 165 await compiler.run(entryPoint); | 162 await compiler.run(entryPoint); |
| 166 SerializedData serializedData = new SerializedData( | 163 SerializedData serializedData = |
| 167 dataUri, outputCollector.getOutput('', 'data')); | 164 new SerializedData(dataUri, outputCollector.getOutput('', 'data')); |
| 168 return new SerializationResult(compiler, serializedData); | 165 return new SerializationResult(compiler, serializedData); |
| 169 } | 166 } |
| 170 | 167 |
| 171 class SerializedData { | 168 class SerializedData { |
| 172 final Uri uri; | 169 final Uri uri; |
| 173 final String data; | 170 final String data; |
| 174 | 171 |
| 175 SerializedData(this.uri, this.data) { | 172 SerializedData(this.uri, this.data) { |
| 176 assert(uri != null); | 173 assert(uri != null); |
| 177 assert(data != null); | 174 assert(data != null); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 uriList.add(Uri.parse('memory:$key')); | 216 uriList.add(Uri.parse('memory:$key')); |
| 220 } | 217 } |
| 221 Map<String, String> sourceFiles = serializedData.toMemorySourceFiles(); | 218 Map<String, String> sourceFiles = serializedData.toMemorySourceFiles(); |
| 222 sourceFiles.addAll(test.preserializedSourceFiles); | 219 sourceFiles.addAll(test.preserializedSourceFiles); |
| 223 if (test.unserializedSourceFiles != null) { | 220 if (test.unserializedSourceFiles != null) { |
| 224 sourceFiles.addAll(test.unserializedSourceFiles); | 221 sourceFiles.addAll(test.unserializedSourceFiles); |
| 225 } | 222 } |
| 226 Uri additionalDataUri = Uri.parse('memory:additional.data'); | 223 Uri additionalDataUri = Uri.parse('memory:additional.data'); |
| 227 SerializedData additionalSerializedData; | 224 SerializedData additionalSerializedData; |
| 228 if (test.sourceFiles.isEmpty) { | 225 if (test.sourceFiles.isEmpty) { |
| 229 SerializationResult result = await serialize( | 226 SerializationResult result = await serialize(uriList.first, |
| 230 uriList.first, | |
| 231 memorySourceFiles: sourceFiles, | 227 memorySourceFiles: sourceFiles, |
| 232 resolutionInputs: serializedData.toUris(), | 228 resolutionInputs: serializedData.toUris(), |
| 233 dataUri: additionalDataUri); | 229 dataUri: additionalDataUri); |
| 234 additionalSerializedData = result.serializedData; | 230 additionalSerializedData = result.serializedData; |
| 235 } else { | 231 } else { |
| 236 OutputCollector outputCollector = new OutputCollector(); | 232 OutputCollector outputCollector = new OutputCollector(); |
| 237 Compiler compiler = compilerFor( | 233 Compiler compiler = compilerFor( |
| 238 entryPoint: test.sourceFiles.isEmpty ? uriList.first : null, | 234 entryPoint: test.sourceFiles.isEmpty ? uriList.first : null, |
| 239 memorySourceFiles: sourceFiles, | 235 memorySourceFiles: sourceFiles, |
| 240 resolutionInputs: serializedData.toUris(), | 236 resolutionInputs: serializedData.toUris(), |
| 241 options: [Flags.resolveOnly], | 237 options: [Flags.resolveOnly], |
| 242 outputProvider: outputCollector); | 238 outputProvider: outputCollector); |
| 243 compiler.librariesToAnalyzeWhenRun = uriList; | 239 compiler.librariesToAnalyzeWhenRun = uriList; |
| 244 await compiler.run(null); | 240 await compiler.run(null); |
| 245 List<LibraryElement> libraries = <LibraryElement>[]; | 241 List<LibraryElement> libraries = <LibraryElement>[]; |
| 246 for (Uri uri in uriList) { | 242 for (Uri uri in uriList) { |
| 247 libraries.add(compiler.libraryLoader.lookupLibrary(uri)); | 243 libraries.add(compiler.libraryLoader.lookupLibrary(uri)); |
| 248 } | 244 } |
| 249 additionalSerializedData = new SerializedData( | 245 additionalSerializedData = new SerializedData( |
| 250 additionalDataUri, | 246 additionalDataUri, outputCollector.getOutput('', 'data')); |
| 251 outputCollector.getOutput('', 'data')); | |
| 252 } | 247 } |
| 253 return <SerializedData>[serializedData, additionalSerializedData]; | 248 return <SerializedData>[serializedData, additionalSerializedData]; |
| 254 } | 249 } |
| 255 | 250 |
| 256 class MeasurementResult { | 251 class MeasurementResult { |
| 257 final String title; | 252 final String title; |
| 258 final String taskTitle; | 253 final String taskTitle; |
| 259 final int elapsedMilliseconds; | 254 final int elapsedMilliseconds; |
| 260 | 255 |
| 261 MeasurementResult(this.title, this.taskTitle, this.elapsedMilliseconds); | 256 MeasurementResult(this.title, this.taskTitle, this.elapsedMilliseconds); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 /// [measurementResults] for a summary. | 292 /// [measurementResults] for a summary. |
| 298 Future measure(String title, String taskTitle, Future task()) async { | 293 Future measure(String title, String taskTitle, Future task()) async { |
| 299 Stopwatch stopwatch = new Stopwatch()..start(); | 294 Stopwatch stopwatch = new Stopwatch()..start(); |
| 300 print('================================================================'); | 295 print('================================================================'); |
| 301 print('$taskTitle: $title'); | 296 print('$taskTitle: $title'); |
| 302 print('----------------------------------------------------------------'); | 297 print('----------------------------------------------------------------'); |
| 303 var result = await task(); | 298 var result = await task(); |
| 304 stopwatch.stop(); | 299 stopwatch.stop(); |
| 305 int elapsedMilliseconds = stopwatch.elapsedMilliseconds; | 300 int elapsedMilliseconds = stopwatch.elapsedMilliseconds; |
| 306 print('$taskTitle: $title: ${elapsedMilliseconds}ms'); | 301 print('$taskTitle: $title: ${elapsedMilliseconds}ms'); |
| 307 measurementResults.add( | 302 measurementResults |
| 308 new MeasurementResult(title, taskTitle, elapsedMilliseconds)); | 303 .add(new MeasurementResult(title, taskTitle, elapsedMilliseconds)); |
| 309 return result; | 304 return result; |
| 310 } | 305 } |
| OLD | NEW |