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