OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import '../lib/path.dart' as path; | 5 import 'package:path/path.dart' as p; |
6 | 6 |
7 void runBenchmark(String name, Function func, List files) { | 7 /// Some hopefully real-world representative platform-independent paths. |
8 // Warmup. | 8 const genericPaths = const [ |
9 for (int i = 0; i < 10000; i++) { | 9 '.', |
10 for (var p in files) { | 10 '..', |
11 func(p); | 11 'out/ReleaseIA32/packages', |
12 } | 12 'lib', |
13 } | 13 'lib/src/', |
14 var count = 100000; | 14 'lib/src/style/url.dart', |
15 var sw = new Stopwatch()..start(); | 15 'test/./not/.././normalized', |
16 for (int i = 0; i < count; i++) { | 16 'benchmark/really/long/path/with/many/components.dart', |
17 for (var p in files) { | 17 ]; |
18 func(p); | |
19 } | |
20 } | |
21 print("$name: ${count / sw.elapsedMicroseconds} iter/us (${sw.elapsed})"); | |
22 } | |
23 | 18 |
24 void runBenchmarkTwoArgs(String name, Function func, List files) { | 19 /// Some platform-specific paths. |
25 // Warmup. | 20 final platformPaths = { |
26 for (int i = 0; i < 1000; i++) { | 21 p.Style.posix: [ |
27 for (var file1 in files) { | 22 '/', |
28 for (var file2 in files) { | 23 '/home/user/dart/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart', |
29 func(file1, file2); | 24 ], |
30 } | 25 p.Style.url: ['https://example.server.org/443643002/path?top=yes#fragment',], |
31 } | 26 p.Style.windows: [ |
32 } | 27 r'C:\User\me\', |
| 28 r'\\server\share\my\folders\some\file.data', |
| 29 ], |
| 30 }; |
33 | 31 |
34 var count = 10000; | 32 /// The command line arguments passed to this script. |
35 var sw = new Stopwatch()..start(); | 33 List<String> arguments; |
36 for (int i = 0; i < count; i++) { | |
37 for (var file1 in files) { | |
38 for (var file2 in files) { | |
39 func(file1, file2); | |
40 } | |
41 } | |
42 } | |
43 print("$name: ${count / sw.elapsedMicroseconds} iter/us (${sw.elapsed})"); | |
44 } | |
45 | 34 |
46 main(args) { | 35 void main(List<String> args) { |
47 for (var style in [path.Style.posix, path.Style.url, path.Style.windows]) { | 36 arguments = args; |
48 var context = new path.Context(style: style); | |
49 var files = COMMON_PATHS.toList()..addAll(STYLE_PATHS[style]); | |
50 | 37 |
51 benchmark(name, func) { | 38 for (var style in [p.Style.posix, p.Style.url, p.Style.windows]) { |
52 name = style.name + '-' + name; | 39 var context = new p.Context(style: style); |
53 if (args.isEmpty || args.any((arg) => name.contains(arg))) { | 40 var files = genericPaths.toList()..addAll(platformPaths[style]); |
54 runBenchmark(name, func, files); | 41 |
55 } | 42 benchmark(name, function) { |
| 43 runBenchmark("${style.name}-$name", 100000, () { |
| 44 for (var file in files) { |
| 45 function(file); |
| 46 } |
| 47 }); |
56 } | 48 } |
57 | 49 |
58 benchmarkTwoArgs(name, func) { | 50 benchmarkPairs(name, function) { |
59 name = style.name + '-' + name + '-two'; | 51 runBenchmark("${style.name}-$name", 1000, () { |
60 if (args.isEmpty || args.any((arg) => name.contains(arg))) { | 52 for (var file1 in files) { |
61 runBenchmarkTwoArgs(name, func, files); | 53 for (var file2 in files) { |
62 } | 54 function(file1, file2); |
| 55 } |
| 56 } |
| 57 }); |
63 } | 58 } |
64 | 59 |
65 benchmark('absolute', context.absolute); | 60 benchmark('absolute', context.absolute); |
66 benchmark('basename', context.basename); | 61 benchmark('basename', context.basename); |
67 benchmark('basenameWithoutExtension', context.basenameWithoutExtension); | 62 benchmark('basenameWithoutExtension', context.basenameWithoutExtension); |
68 benchmark('dirname', context.dirname); | 63 benchmark('dirname', context.dirname); |
69 benchmark('extension', context.extension); | 64 benchmark('extension', context.extension); |
70 benchmark('rootPrefix', context.rootPrefix); | 65 benchmark('rootPrefix', context.rootPrefix); |
71 benchmark('isAbsolute', context.isAbsolute); | 66 benchmark('isAbsolute', context.isAbsolute); |
72 benchmark('isRelative', context.isRelative); | 67 benchmark('isRelative', context.isRelative); |
73 benchmark('isRootRelative', context.isRootRelative); | 68 benchmark('isRootRelative', context.isRootRelative); |
74 benchmark('normalize', context.normalize); | 69 benchmark('normalize', context.normalize); |
75 benchmark('relative', context.relative); | 70 benchmark('relative', context.relative); |
76 benchmarkTwoArgs('relative', context.relative); | 71 benchmarkPairs('relative from', (file, from) { |
| 72 try { |
| 73 return context.relative(file, from: from); |
| 74 } on p.PathException { |
| 75 // Do nothing. |
| 76 } |
| 77 }); |
77 benchmark('toUri', context.toUri); | 78 benchmark('toUri', context.toUri); |
78 benchmark('prettyUri', context.prettyUri); | 79 benchmark('prettyUri', context.prettyUri); |
79 benchmarkTwoArgs('isWithin', context.isWithin); | 80 benchmarkPairs('isWithin', context.isWithin); |
80 } | 81 } |
81 | 82 |
82 if (args.isEmpty || args.any((arg) => arg == 'current')) { | 83 runBenchmark('current', 100000, () => p.current); |
83 runBenchmark('current', (_) => path.current, [null]); | |
84 } | |
85 } | 84 } |
86 | 85 |
87 const COMMON_PATHS = const ['.', '..', 'out/ReleaseIA32/packages']; | 86 void runBenchmark(String name, int count, Function function) { |
| 87 // If names are passed on the command-line, they select which benchmarks are |
| 88 // run. |
| 89 if (arguments.isNotEmpty && !arguments.contains(name)) return; |
88 | 90 |
89 final STYLE_PATHS = { | 91 // Warmup. |
90 path.Style.posix: [ | 92 for (var i = 0; i < 10000; i++) { |
91 '/home/user/dart/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart', | 93 function(); |
92 ], | 94 } |
93 path.Style.url: [ | 95 |
94 'https://example.server.org/443643002/path?top=yes#fragment', | 96 var stopwatch = new Stopwatch()..start(); |
95 ], | 97 for (var i = 0; i < count; i++) { |
96 path.Style.windows: [ | 98 function(); |
97 r'C:\User\me\', | 99 } |
98 r'\\server\share\my\folders\some\file.data', | 100 |
99 ], | 101 var rate = |
100 }; | 102 (count / stopwatch.elapsedMicroseconds).toStringAsFixed(5).padLeft(9); |
| 103 print("${name.padLeft(32)}: $rate iter/us (${stopwatch.elapsed})"); |
| 104 } |
OLD | NEW |