OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 * A benchmark for the Dart parser. | |
7 */ | |
8 class ParserBench extends BaseParserBench { | |
9 int charCount = 0; | |
10 double score = 0.0; | |
11 | |
12 Token scanFileNamed(String filename) { | |
13 Token token; | |
14 getBytes(filename, (bytes) { | |
15 Scanner scanner = makeScanner(bytes); | |
16 try { | |
17 token = scanner.tokenize(); | |
18 printTokens(token); | |
19 charCount += scanner.charOffset; | |
20 } catch (MalformedInputException e) { | |
21 print("${filename}: ${e}"); | |
22 } | |
23 }); | |
24 return token; | |
25 } | |
26 | |
27 void timedParseAll(List<String> arguments) { | |
28 charCount = 0; | |
29 Stopwatch timer = new Stopwatch(); | |
30 timer.start(); | |
31 BenchListener listener = parseAll(arguments); | |
32 timer.stop(); | |
33 print("Parsing (${listener.libraryTagCount} tags, " | |
34 "${listener.classCount} classes, " | |
35 "${listener.interfaceCount} interfaces, " | |
36 "${listener.aliasCount} typedefs, " | |
37 "${listener.topLevelMemberCount} top-level members) " | |
38 "took ${timer.elapsedInMs()}ms"); | |
39 } | |
40 | |
41 BenchListener parseAll(List<String> arguments) { | |
42 charCount = 0; | |
43 Stopwatch timer = new Stopwatch(); | |
44 timer.start(); | |
45 BenchListener listener = new BenchListener(); | |
46 for (String argument in arguments) { | |
47 parseFileNamed(argument, listener); | |
48 } | |
49 timer.stop(); | |
50 score = charCount / timer.elapsedInMs(); | |
51 return listener; | |
52 } | |
53 | |
54 void parseFileNamed(String argument, Listener listener) { | |
55 bool failed = true; | |
56 try { | |
57 PartialParser parser = new PartialParser(listener); | |
58 parser.parseUnit(scanFileNamed(argument)); | |
59 failed = false; | |
60 } finally { | |
61 if (failed) print('Error in ${argument}'); | |
62 } | |
63 } | |
64 | |
65 void main(List<String> arguments) { | |
66 for (int i = 0; i < 10; i++) { | |
67 timedParseAll(arguments); | |
68 } | |
69 final int iterations = 500; | |
70 VerboseProgressBar bar = new VerboseProgressBar(iterations); | |
71 bar.begin(); | |
72 for (int i = 0; i < iterations; i++) { | |
73 bar.tick(); | |
74 parseAll(arguments); | |
75 bar.recordScore(score); | |
76 } | |
77 bar.end(); | |
78 for (int i = 0; i < 10; i++) { | |
79 timedParseAll(arguments); | |
80 } | |
81 } | |
82 } | |
83 | |
84 main() { | |
85 new ParserBench().main(argv); | |
86 } | |
87 | |
88 class BenchListener extends Listener { | |
89 int aliasCount = 0; | |
90 int classCount = 0; | |
91 int interfaceCount = 0; | |
92 int libraryTagCount = 0; | |
93 int topLevelMemberCount = 0; | |
94 | |
95 void beginTopLevelMember(Token token) { | |
96 topLevelMemberCount++; | |
97 } | |
98 | |
99 void beginLibraryTag(Token token) { | |
100 libraryTagCount++; | |
101 } | |
102 | |
103 void beginInterface(Token token) { | |
104 interfaceCount++; | |
105 } | |
106 | |
107 void beginClass(Token token) { | |
108 classCount++; | |
109 } | |
110 | |
111 void beginFunctionTypeAlias(Token token) { | |
112 aliasCount++; | |
113 } | |
114 } | |
OLD | NEW |