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