| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 #library('scanner_bench'); | |
| 6 #import('scannerlib.dart'); | |
| 7 #import('scanner_implementation.dart'); | |
| 8 #source('source_list.dart'); | |
| 9 | |
| 10 /** | |
| 11 * A common superclass for scanner benchmarks. | |
| 12 */ | |
| 13 class ScannerBench { | |
| 14 void main(List<String> arguments) { | |
| 15 for (String argument in arguments) { | |
| 16 checkExistence(argument); | |
| 17 } | |
| 18 tokenizeAll(print, 10, arguments); | |
| 19 tokenizeAll((x) {}, 1000, arguments); | |
| 20 tokenizeAll(print, 10, arguments); | |
| 21 } | |
| 22 | |
| 23 void tokenizeAll(void log(String s), int iterations, List<String> arguments) { | |
| 24 VerboseProgressBar bar = new VerboseProgressBar(iterations); | |
| 25 bar.begin(); | |
| 26 for (int i = 0; i < iterations; i++) { | |
| 27 bar.tick(); | |
| 28 Stopwatch timer = new Stopwatch(); | |
| 29 timer.start(); | |
| 30 int charCount = 0; | |
| 31 for (final String argument in arguments) { | |
| 32 charCount += tokenizeOne(argument); | |
| 33 } | |
| 34 timer.stop(); | |
| 35 bar.recordScore(charCount / timer.elapsedInMs()); | |
| 36 log("Tokenized ${arguments.length} files " + | |
| 37 "(total size = ${charCount} chars) " + | |
| 38 "in ${timer.elapsedInMs()}ms"); | |
| 39 } | |
| 40 bar.end(); | |
| 41 } | |
| 42 | |
| 43 int tokenizeOne(String filename) { | |
| 44 return getBytes(filename, (bytes) { | |
| 45 Scanner scanner = makeScanner(bytes); | |
| 46 try { | |
| 47 printTokens(scanner.tokenize()); | |
| 48 } catch (MalformedInputException e) { | |
| 49 print("${filename}: ${e}"); | |
| 50 } | |
| 51 }); | |
| 52 } | |
| 53 | |
| 54 void printTokens(Token token) { | |
| 55 // TODO(ahe): Turn this into a proper test. | |
| 56 return; | |
| 57 StringBuffer sb = new StringBuffer(); | |
| 58 for (; token != null; token = token.next) { | |
| 59 if (token.kind < 127) { | |
| 60 sb.add(new String.fromCharCodes([token.kind])); | |
| 61 } else { | |
| 62 sb.add(token.kind); | |
| 63 } | |
| 64 sb.add(":"); | |
| 65 sb.add(token); | |
| 66 sb.add(" "); | |
| 67 } | |
| 68 print(sb.toString()); | |
| 69 } | |
| 70 | |
| 71 abstract int getBytes(String filename, void callback(bytes)); | |
| 72 abstract Scanner makeScanner(bytes); | |
| 73 abstract void checkExistence(String filename); | |
| 74 } | |
| 75 | |
| 76 class ProgressBar { | |
| 77 static final String hashes = "##############################################"; | |
| 78 static final String spaces = " "; | |
| 79 static final int GEOMEAN_COUNT = 50; | |
| 80 | |
| 81 final String esc; | |
| 82 final String up; | |
| 83 final String clear; | |
| 84 final int total; | |
| 85 final List<num> scores; | |
| 86 int ticks = 0; | |
| 87 | |
| 88 ProgressBar(int total) : this.escape(total, new String.fromCharCodes([27])); | |
| 89 | |
| 90 ProgressBar.escape(this.total, String esc) | |
| 91 : esc = esc, up = "$esc[1A", clear = "$esc[K", scores = new List<num>(); | |
| 92 | |
| 93 void begin() { | |
| 94 if (total > 10) { | |
| 95 print("[$spaces] 0%"); | |
| 96 print("$up[${hashes.substring(0, ticks * spaces.length ~/ total)}"); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void tick() { | |
| 101 if (total > 10 && ticks % 5 === 0) { | |
| 102 print("$up$clear[$spaces] ${ticks * 100 ~/ total}% ${score()}"); | |
| 103 print("$up[${hashes.substring(0, ticks * spaces.length ~/ total)}"); | |
| 104 } | |
| 105 ++ticks; | |
| 106 } | |
| 107 | |
| 108 void end() { | |
| 109 if (total > 10) { | |
| 110 print("$up$clear[$hashes] 100% ${score()}"); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void recordScore(num score) { | |
| 115 scores.addLast(score); | |
| 116 } | |
| 117 | |
| 118 int score() { | |
| 119 num geoMean = 1; | |
| 120 int count = Math.min(scores.length, GEOMEAN_COUNT); | |
| 121 for (int i = scores.length - count; i < scores.length; i++) { | |
| 122 geoMean *= scores[i]; | |
| 123 } | |
| 124 geoMean = Math.pow(geoMean, 1/Math.max(count, 1)); | |
| 125 return geoMean.round().toInt(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 class VerboseProgressBar { | |
| 130 final int total; | |
| 131 int ticks = 0; | |
| 132 | |
| 133 VerboseProgressBar(int this.total); | |
| 134 | |
| 135 void begin() { | |
| 136 } | |
| 137 | |
| 138 void tick() { | |
| 139 ++ticks; | |
| 140 } | |
| 141 | |
| 142 void end() { | |
| 143 } | |
| 144 | |
| 145 void recordScore(num score) { | |
| 146 if (total > 10) { | |
| 147 print("$ticks, $score, ${ticks * 100 ~/ total}%"); | |
| 148 } | |
| 149 } | |
| 150 } | |
| OLD | NEW |