| 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 library kernel.treeshaker_bench; | 4 library kernel.treeshaker_bench; |
| 5 | 5 |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 9 import 'package:kernel/class_hierarchy.dart'; | 9 import 'package:kernel/class_hierarchy.dart'; |
| 10 import 'package:kernel/core_types.dart'; | 10 import 'package:kernel/core_types.dart'; |
| 11 import 'package:kernel/kernel.dart'; | 11 import 'package:kernel/kernel.dart'; |
| 12 import 'package:kernel/transformations/treeshaker.dart'; | 12 import 'package:kernel/transformations/treeshaker.dart'; |
| 13 | 13 |
| 14 import 'class_hierarchy_basic.dart'; | 14 import 'class_hierarchy_basic.dart'; |
| 15 | 15 |
| 16 ArgParser argParser = new ArgParser() | 16 ArgParser argParser = new ArgParser(allowTrailingOptions: true) |
| 17 ..addFlag('basic', | 17 ..addFlag('basic', |
| 18 help: 'Use the basic class hierarchy implementation', negatable: false) | 18 help: 'Use the basic class hierarchy implementation', negatable: false) |
| 19 ..addFlag('from-scratch', | 19 ..addFlag('from-scratch', |
| 20 help: 'Rebuild class hierarchy for each tree shaking', negatable: false) | 20 help: 'Rebuild class hierarchy for each tree shaking', negatable: false) |
| 21 ..addFlag('diagnose', | 21 ..addFlag('diagnose', |
| 22 abbr: 'd', help: 'Print internal diagnostics', negatable: false); | 22 abbr: 'd', help: 'Print internal diagnostics', negatable: false) |
| 23 ..addFlag('strong', |
| 24 help: 'Run the tree shaker in strong mode', negatable: false); |
| 23 | 25 |
| 24 String usage = ''' | 26 String usage = ''' |
| 25 Usage: treeshaker_bench [options] FILE.dart | 27 Usage: treeshaker_bench [options] FILE.dill |
| 26 | 28 |
| 27 Benchmark the tree shaker and the class hierarchy it depends on. | 29 Benchmark the tree shaker and the class hierarchy it depends on. |
| 28 | 30 |
| 29 Options: | 31 Options: |
| 30 ${argParser.usage} | 32 ${argParser.usage} |
| 31 '''; | 33 '''; |
| 32 | 34 |
| 33 void main(List<String> args) { | 35 void main(List<String> args) { |
| 34 if (args.length == 0) { | 36 if (args.length == 0) { |
| 35 print(usage); | 37 print(usage); |
| 36 exit(1); | 38 exit(1); |
| 37 } | 39 } |
| 38 ArgResults options = argParser.parse(args); | 40 ArgResults options = argParser.parse(args); |
| 39 if (options.rest.length != 1) { | 41 if (options.rest.length != 1) { |
| 40 print('Exactly one file must be given'); | 42 print('Exactly one file must be given'); |
| 41 exit(1); | 43 exit(1); |
| 42 } | 44 } |
| 43 String filename = options.rest.single; | 45 String filename = options.rest.single; |
| 46 bool strongMode = options['strong']; |
| 44 | 47 |
| 45 Program program = loadProgramFromBinary(filename); | 48 Program program = loadProgramFromBinary(filename); |
| 46 | 49 |
| 47 ClassHierarchy buildClassHierarchy() { | 50 ClassHierarchy buildClassHierarchy() { |
| 48 return options['basic'] | 51 return options['basic'] |
| 49 ? new BasicClassHierarchy(program) | 52 ? new BasicClassHierarchy(program) |
| 50 : new ClassHierarchy(program); | 53 : new ClassHierarchy(program); |
| 51 } | 54 } |
| 52 | 55 |
| 53 CoreTypes coreTypes = new CoreTypes(program); | 56 CoreTypes coreTypes = new CoreTypes(program); |
| 54 | 57 |
| 55 var watch = new Stopwatch()..start(); | 58 var watch = new Stopwatch()..start(); |
| 56 ClassHierarchy sharedClassHierarchy = buildClassHierarchy(); | 59 ClassHierarchy sharedClassHierarchy = buildClassHierarchy(); |
| 57 int coldHierarchyTime = watch.elapsedMicroseconds; | 60 int coldHierarchyTime = watch.elapsedMicroseconds; |
| 58 var shaker = new TreeShaker(program, | 61 var shaker = new TreeShaker(program, |
| 59 hierarchy: sharedClassHierarchy, coreTypes: coreTypes); | 62 hierarchy: sharedClassHierarchy, |
| 63 coreTypes: coreTypes, |
| 64 strongMode: strongMode); |
| 60 if (options['diagnose']) { | 65 if (options['diagnose']) { |
| 61 print(shaker.getDiagnosticString()); | 66 print(shaker.getDiagnosticString()); |
| 62 } | 67 } |
| 63 shaker = null; | 68 shaker = null; |
| 64 int coldTreeShakingTime = watch.elapsedMicroseconds; | 69 int coldTreeShakingTime = watch.elapsedMicroseconds; |
| 65 | 70 |
| 66 ClassHierarchy getClassHierarchy() { | 71 ClassHierarchy getClassHierarchy() { |
| 67 return options['from-scratch'] | 72 return options['from-scratch'] |
| 68 ? buildClassHierarchy() | 73 ? buildClassHierarchy() |
| 69 : sharedClassHierarchy; | 74 : sharedClassHierarchy; |
| 70 } | 75 } |
| 71 | 76 |
| 72 const int numberOfTrials = 50; | 77 const int numberOfTrials = 50; |
| 73 int hotHierarchyTime = 0; | 78 int hotHierarchyTime = 0; |
| 74 int hotTreeShakingTime = 0; | 79 int hotTreeShakingTime = 0; |
| 75 watch.reset(); | 80 watch.reset(); |
| 76 for (int i = 0; i < numberOfTrials; i++) { | 81 for (int i = 0; i < numberOfTrials; i++) { |
| 77 watch.reset(); | 82 watch.reset(); |
| 78 var hierarchy = getClassHierarchy(); | 83 var hierarchy = getClassHierarchy(); |
| 79 hotHierarchyTime += watch.elapsedMicroseconds; | 84 hotHierarchyTime += watch.elapsedMicroseconds; |
| 80 new TreeShaker(program, hierarchy: hierarchy, coreTypes: coreTypes); | 85 new TreeShaker(program, |
| 86 hierarchy: hierarchy, coreTypes: coreTypes, strongMode: strongMode); |
| 81 hotTreeShakingTime += watch.elapsedMicroseconds; | 87 hotTreeShakingTime += watch.elapsedMicroseconds; |
| 82 } | 88 } |
| 83 hotHierarchyTime ~/= numberOfTrials; | 89 hotHierarchyTime ~/= numberOfTrials; |
| 84 hotTreeShakingTime ~/= numberOfTrials; | 90 hotTreeShakingTime ~/= numberOfTrials; |
| 85 | 91 |
| 86 var coldShakingMs = coldTreeShakingTime ~/ 1000; | 92 var coldShakingMs = coldTreeShakingTime ~/ 1000; |
| 87 var coldHierarchyMs = coldHierarchyTime ~/ 1000; | 93 var coldHierarchyMs = coldHierarchyTime ~/ 1000; |
| 88 var hotShakingMs = hotTreeShakingTime ~/ 1000; | 94 var hotShakingMs = hotTreeShakingTime ~/ 1000; |
| 89 var hotHierarchyMs = hotHierarchyTime ~/ 1000; | 95 var hotHierarchyMs = hotHierarchyTime ~/ 1000; |
| 90 | 96 |
| 91 print(''' | 97 print(''' |
| 92 build.cold $coldShakingMs ms ($coldHierarchyMs ms from hierarchy) | 98 build.cold $coldShakingMs ms ($coldHierarchyMs ms from hierarchy) |
| 93 build.hot $hotShakingMs ms ($hotHierarchyMs ms from hierarchy)'''); | 99 build.hot $hotShakingMs ms ($hotHierarchyMs ms from hierarchy)'''); |
| 94 } | 100 } |
| OLD | NEW |