| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * This file contains code for collecting statistics about the use of fields in |
| 7 * a summary file. |
| 8 */ |
| 9 library analyzer.tool.summary.stats; |
| 10 |
| 11 import 'dart:io'; |
| 12 import 'dart:mirrors'; |
| 13 |
| 14 import 'package:analyzer/src/summary/base.dart'; |
| 15 import 'package:analyzer/src/summary/idl.dart'; |
| 16 |
| 17 main(List<String> args) { |
| 18 if (args.length != 1) { |
| 19 _printUsage(); |
| 20 exitCode = 1; |
| 21 return; |
| 22 } |
| 23 |
| 24 String inputFilePath = args[0]; |
| 25 |
| 26 // Read the input. |
| 27 PackageBundle bundle = |
| 28 new PackageBundle.fromBuffer(new File(inputFilePath).readAsBytesSync()); |
| 29 |
| 30 // Compute and output stats. |
| 31 Stats stats = new Stats(); |
| 32 stats.record(bundle); |
| 33 stats.dump(); |
| 34 } |
| 35 |
| 36 /** |
| 37 * The name of the stats tool. |
| 38 */ |
| 39 const String BINARY_NAME = "stats"; |
| 40 |
| 41 /** |
| 42 * Print information about how to use the stats tool. |
| 43 */ |
| 44 void _printUsage() { |
| 45 print('Usage: $BINARY_NAME input_file_path'); |
| 46 } |
| 47 |
| 48 /** |
| 49 * An instance of [Stats] keeps track of statistics about the use of fields in |
| 50 * summary objects. |
| 51 */ |
| 52 class Stats { |
| 53 /** |
| 54 * Map from type to field name to a count of how often the field is used. |
| 55 */ |
| 56 Map<Type, Map<String, int>> counts = <Type, Map<String, int>>{}; |
| 57 |
| 58 /** |
| 59 * Print out statistics gathered so far. |
| 60 */ |
| 61 void dump() { |
| 62 counts.forEach((Type type, Map<String, int> typeCounts) { |
| 63 print(type); |
| 64 List<String> keys = typeCounts.keys.toList(); |
| 65 keys.sort((String a, String b) => typeCounts[b].compareTo(typeCounts[a])); |
| 66 for (String key in keys) { |
| 67 print(' $key: ${typeCounts[key]}'); |
| 68 } |
| 69 print(''); |
| 70 }); |
| 71 } |
| 72 |
| 73 /** |
| 74 * Record statistics for [obj] and all objects it refers to. |
| 75 */ |
| 76 void record(SummaryClass obj) { |
| 77 Map<String, int> typeCounts = |
| 78 counts.putIfAbsent(obj.runtimeType, () => <String, int>{}); |
| 79 obj.toMap().forEach((String key, Object value) { |
| 80 if (value == null || |
| 81 value == 0 || |
| 82 value == false || |
| 83 value == '' || |
| 84 value is List && value.isEmpty || |
| 85 reflect(value).type.isEnum && (value as dynamic).index == 0) { |
| 86 return; |
| 87 } |
| 88 if (!typeCounts.containsKey(key)) { |
| 89 typeCounts[key] = 0; |
| 90 } |
| 91 typeCounts[key]++; |
| 92 if (value is SummaryClass) { |
| 93 record(value); |
| 94 } else if (value is List) { |
| 95 value.forEach((Object element) { |
| 96 if (element is SummaryClass) { |
| 97 record(element); |
| 98 } |
| 99 }); |
| 100 } |
| 101 }); |
| 102 } |
| 103 } |
| OLD | NEW |