Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 } | 128 } |
| 129 | 129 |
| 130 private: | 130 private: |
| 131 UnicodeCache* unicode_cache_; | 131 UnicodeCache* unicode_cache_; |
| 132 Scanner* scanner_; | 132 Scanner* scanner_; |
| 133 const byte* source_; | 133 const byte* source_; |
| 134 BufferedUtf16CharacterStream* stream_; | 134 BufferedUtf16CharacterStream* stream_; |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 | 137 |
| 138 struct TokenWithLocation { | |
| 139 Token::Value value; | |
| 140 size_t beg; | |
| 141 size_t end; | |
| 142 TokenWithLocation() : value(Token::ILLEGAL), beg(0), end(0) { } | |
| 143 TokenWithLocation(Token::Value value, size_t beg, size_t end) : | |
| 144 value(value), beg(beg), end(end) { } | |
| 145 bool operator==(const TokenWithLocation& other) { | |
| 146 return value == other.value && beg == other.beg && end == other.end; | |
| 147 } | |
| 148 bool operator!=(const TokenWithLocation& other) { | |
| 149 return !(*this == other); | |
| 150 } | |
| 151 void Print(const char* prefix) const { | |
| 152 printf("%s %11s at (%d, %d)\n", | |
| 153 prefix, Token::Name(value), | |
| 154 static_cast<int>(beg), static_cast<int>(end)); | |
| 155 } | |
| 156 }; | |
| 157 | |
| 158 | |
| 159 TimeDelta RunBaselineScanner(const char* fname, | |
| 160 Isolate* isolate, | |
| 161 Encoding encoding, | |
| 162 bool dump_tokens, | |
| 163 std::vector<TokenWithLocation>* tokens) { | |
| 164 ElapsedTimer timer; | |
| 165 BaselineScanner scanner(fname, isolate, encoding, &timer); | |
| 166 Token::Value token; | |
| 167 int beg, end; | |
| 168 do { | |
| 169 token = scanner.Next(&beg, &end); | |
| 170 if (dump_tokens) { | |
| 171 tokens->push_back(TokenWithLocation(token, beg, end)); | |
| 172 } | |
| 173 } while (token != Token::EOS); | |
| 174 return timer.Elapsed(); | |
| 175 } | |
| 176 | |
| 177 | |
| 178 TimeDelta RunExperimentalScanner(const char* fname, | |
| 179 Isolate* isolate, | |
| 180 Encoding encoding, | |
| 181 bool dump_tokens, | |
| 182 std::vector<TokenWithLocation>* tokens) { | |
| 183 ElapsedTimer timer; | |
| 184 timer.Start(); | |
| 185 ExperimentalScanner scanner(fname, true, isolate); | |
| 186 Token::Value token; | |
| 187 do { | |
| 188 token = scanner.Next(); | |
| 189 ExperimentalScanner::Location location = scanner.location(); | |
| 190 if (dump_tokens) { | |
| 191 tokens->push_back( | |
| 192 TokenWithLocation(token, location.beg_pos, location.end_pos)); | |
| 193 } | |
| 194 } while (token != Token::EOS); | |
| 195 return timer.Elapsed(); | |
| 196 } | |
| 197 | |
| 198 | |
| 199 void PrintTokens(const char* name, | |
| 200 const std::vector<TokenWithLocation>& tokens) { | |
| 201 printf("No of tokens: %d\n", | |
| 202 static_cast<int>(tokens.size())); | |
| 203 printf("%s:\n", name); | |
| 204 for (size_t i = 0; i < tokens.size(); ++i) { | |
| 205 tokens[i].Print("=>"); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 | |
| 138 int main(int argc, char* argv[]) { | 210 int main(int argc, char* argv[]) { |
| 139 v8::V8::InitializeICU(); | 211 v8::V8::InitializeICU(); |
| 140 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 212 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 141 Encoding encoding = ASCII; | 213 Encoding encoding = ASCII; |
| 142 bool print_baseline = false; | 214 bool print_tokens = false; |
| 143 bool run_baseline = true; | 215 bool run_baseline = true; |
| 144 bool run_experimental = true; | 216 bool run_experimental = true; |
| 217 char* fname = argv[1]; | |
| 145 for (int i = 0; i < argc; ++i) { | 218 for (int i = 0; i < argc; ++i) { |
| 146 if (strcmp(argv[i], "--latin1") == 0) { | 219 if (strcmp(argv[i], "--latin1") == 0) { |
| 147 encoding = LATIN1; | 220 encoding = LATIN1; |
| 148 } else if (strcmp(argv[i], "--utf8") == 0) { | 221 } else if (strcmp(argv[i], "--utf8") == 0) { |
| 149 encoding = UTF8; | 222 encoding = UTF8; |
| 150 } else if (strcmp(argv[i], "--utf16") == 0) { | 223 } else if (strcmp(argv[i], "--utf16") == 0) { |
| 151 encoding = UTF16; | 224 encoding = UTF16; |
| 152 } else if (strcmp(argv[i], "--ascii") == 0) { | 225 } else if (strcmp(argv[i], "--ascii") == 0) { |
| 153 encoding = ASCII; | 226 encoding = ASCII; |
| 154 } else if (strcmp(argv[i], "--print-baseline") == 0) { | 227 } else if (strcmp(argv[i], "--print-tokens") == 0) { |
| 155 print_baseline = true; | 228 print_tokens = true; |
| 156 } else if (strcmp(argv[i], "--no-baseline") == 0) { | 229 } else if (strcmp(argv[i], "--no-baseline") == 0) { |
| 157 run_baseline = false; | 230 run_baseline = false; |
| 158 } else if (strcmp(argv[i], "--no-experimental") == 0) { | 231 } else if (strcmp(argv[i], "--no-experimental") == 0) { |
| 159 run_experimental = false; | 232 run_experimental = false; |
| 233 } else if (argv[i][0] != '-') { | |
| 234 fname = argv[i]; | |
| 160 } | 235 } |
| 161 } | 236 } |
| 162 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 237 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 163 { | 238 { |
| 164 v8::HandleScope handle_scope(isolate); | 239 v8::HandleScope handle_scope(isolate); |
| 165 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 240 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 166 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 241 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 167 ASSERT(!context.IsEmpty()); | 242 ASSERT(!context.IsEmpty()); |
| 168 { | 243 { |
| 169 v8::Context::Scope scope(context); | 244 v8::Context::Scope scope(context); |
| 170 Isolate* isolate = Isolate::Current(); | 245 Isolate* isolate = Isolate::Current(); |
| 171 HandleScope handle_scope(isolate); | 246 HandleScope handle_scope(isolate); |
| 172 | 247 |
| 173 std::vector<Token::Value> baseline_tokens, experimental_tokens; | 248 std::vector<TokenWithLocation> baseline_tokens, experimental_tokens; |
| 174 std::vector<size_t> baseline_beg, baseline_end, experimental_beg, | |
| 175 experimental_end; | |
| 176 Token::Value token; | |
| 177 int beg, end; | |
| 178 | |
| 179 TimeDelta baseline_time, experimental_time; | 249 TimeDelta baseline_time, experimental_time; |
| 180 ElapsedTimer timer; | |
| 181 if (run_baseline) { | 250 if (run_baseline) { |
| 182 BaselineScanner baseline(argv[1], isolate, encoding, &timer); | 251 baseline_time = RunBaselineScanner( |
| 183 do { | 252 fname, isolate, encoding, print_tokens, &baseline_tokens); |
| 184 token = baseline.Next(&beg, &end); | |
| 185 baseline_tokens.push_back(token); | |
| 186 baseline_beg.push_back(beg); | |
| 187 baseline_end.push_back(end); | |
| 188 } while (token != Token::EOS); | |
| 189 baseline_time = timer.Elapsed(); | |
| 190 } | 253 } |
| 191 | |
| 192 if (run_experimental) { | 254 if (run_experimental) { |
| 193 ExperimentalScanner experimental(argv[1], true, isolate); | 255 experimental_time = RunExperimentalScanner( |
| 194 timer.Start(); | 256 fname, isolate, encoding, print_tokens, &experimental_tokens); |
| 195 do { | |
| 196 token = experimental.Next(); | |
| 197 experimental_tokens.push_back(token); | |
| 198 ExperimentalScanner::Location location = experimental.location(); | |
| 199 experimental_beg.push_back(location.beg_pos); | |
| 200 experimental_end.push_back(location.end_pos); | |
| 201 } while (token != Token::EOS); | |
| 202 experimental_time = timer.Elapsed(); | |
| 203 } | 257 } |
| 204 | 258 if (print_tokens && !run_experimental) { |
| 205 if (print_baseline) { | 259 PrintTokens("Baseline", baseline_tokens); |
| 206 printf("Baseline:\n"); | |
| 207 for (size_t i = 0; i < baseline_tokens.size(); ++i) { | |
| 208 printf("=> %11s at (%d, %d)\n", | |
| 209 Token::Name(baseline_tokens[i]), | |
| 210 static_cast<int>(baseline_beg[i]), | |
| 211 static_cast<int>(baseline_end[i])); | |
| 212 } | |
| 213 printf("(Mis)matches:\n"); | |
| 214 } | 260 } |
| 215 | 261 if (print_tokens && !run_baseline) { |
| 216 if (run_baseline && run_experimental) { | 262 PrintTokens("Experimental", experimental_tokens); |
| 263 } | |
| 264 if (print_tokens && run_baseline && run_experimental) { | |
| 265 printf("No of tokens in Baseline: %d\n", | |
| 266 static_cast<int>(baseline_tokens.size())); | |
| 267 printf("No of tokens in Experimental: %d\n", | |
| 268 static_cast<int>(experimental_tokens.size())); | |
| 269 printf("Baseline and Experimental:\n"); | |
| 217 for (size_t i = 0; i < experimental_tokens.size(); ++i) { | 270 for (size_t i = 0; i < experimental_tokens.size(); ++i) { |
| 218 printf("=> %11s at (%d, %d)\n", | 271 experimental_tokens[i].Print("=>"); |
| 219 Token::Name(experimental_tokens[i]), | 272 if (experimental_tokens[i] != baseline_tokens[i]) { |
| 220 static_cast<int>(experimental_beg[i]), | |
| 221 static_cast<int>(experimental_end[i])); | |
| 222 if (experimental_tokens[i] != baseline_tokens[i] || | |
| 223 experimental_beg[i] != baseline_beg[i] || | |
| 224 experimental_end[i] != baseline_end[i]) { | |
| 225 printf("MISMATCH:\n"); | 273 printf("MISMATCH:\n"); |
| 226 printf("Expected: %s at (%d, %d)\n", | 274 baseline_tokens[i].Print("Expected: "); |
| 227 Token::Name(baseline_tokens[i]), | 275 experimental_tokens[i].Print("Actual: "); |
| 228 static_cast<int>(baseline_beg[i]), | |
| 229 static_cast<int>(baseline_end[i])); | |
| 230 printf("Actual: %s at (%d, %d)\n", | |
| 231 Token::Name(experimental_tokens[i]), | |
| 232 static_cast<int>(experimental_beg[i]), | |
| 233 static_cast<int>(experimental_end[i])); | |
| 234 return 1; | 276 return 1; |
| 235 } | 277 } |
| 236 } | 278 } |
| 237 } | 279 } |
| 238 printf("No of tokens: %d\n", | 280 if (run_baseline) { |
| 239 static_cast<int>(experimental_tokens.size())); | 281 printf("Baseline : %.3f ms\n", baseline_time.InMillisecondsF()); |
| 240 printf("Baseline: %f ms\nExperimental %f ms\n", | 282 } |
| 241 baseline_time.InMillisecondsF(), | 283 if (run_baseline) { |
|
marja
2013/11/07 11:52:21
run_experimental
| |
| 242 experimental_time.InMillisecondsF()); | 284 printf("Experimental: %.3f ms\n", experimental_time.InMillisecondsF()); |
| 285 } | |
| 243 } | 286 } |
| 244 } | 287 } |
| 245 v8::V8::Dispose(); | 288 v8::V8::Dispose(); |
| 246 return 0; | 289 return 0; |
| 247 } | 290 } |
| OLD | NEW |