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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 using namespace v8::internal; | 49 using namespace v8::internal; |
50 | 50 |
51 enum Encoding { | 51 enum Encoding { |
52 LATIN1, | 52 LATIN1, |
53 UTF8, | 53 UTF8, |
54 UTF16 | 54 UTF16 |
55 }; | 55 }; |
56 | 56 |
57 | 57 |
58 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { | 58 const byte* ReadFile(const char* name, Isolate* isolate, |
| 59 int* size, int repeat) { |
59 FILE* file = fopen(name, "rb"); | 60 FILE* file = fopen(name, "rb"); |
60 *size = 0; | 61 *size = 0; |
61 if (file == NULL) return NULL; | 62 if (file == NULL) return NULL; |
62 | 63 |
63 fseek(file, 0, SEEK_END); | 64 fseek(file, 0, SEEK_END); |
64 *size = ftell(file); | 65 int file_size = ftell(file); |
65 rewind(file); | 66 rewind(file); |
66 | 67 |
| 68 *size = file_size * repeat; |
| 69 |
67 byte* chars = new byte[*size + 1]; | 70 byte* chars = new byte[*size + 1]; |
68 chars[*size] = 0; | 71 for (int i = 0; i < file_size;) { |
69 for (int i = 0; i < *size;) { | 72 int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file)); |
70 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); | |
71 i += read; | 73 i += read; |
72 } | 74 } |
73 fclose(file); | 75 fclose(file); |
| 76 |
| 77 for (int i = file_size; i < *size; i++) { |
| 78 chars[i] = chars[i - file_size]; |
| 79 } |
| 80 chars[*size] = 0; |
| 81 |
74 return chars; | 82 return chars; |
75 } | 83 } |
76 | 84 |
77 | 85 |
78 class BaselineScanner { | 86 class BaselineScanner { |
79 public: | 87 public: |
80 BaselineScanner(const char* fname, | 88 BaselineScanner(const char* fname, |
81 Isolate* isolate, | 89 Isolate* isolate, |
82 Encoding encoding, | 90 Encoding encoding, |
83 ElapsedTimer* timer) | 91 ElapsedTimer* timer, |
| 92 int repeat) |
84 : stream_(NULL) { | 93 : stream_(NULL) { |
85 int length = 0; | 94 int length = 0; |
86 source_ = ReadFile(fname, isolate, &length); | 95 source_ = ReadFile(fname, isolate, &length, repeat); |
87 unicode_cache_ = new UnicodeCache(); | 96 unicode_cache_ = new UnicodeCache(); |
88 scanner_ = new Scanner(unicode_cache_); | 97 scanner_ = new Scanner(unicode_cache_); |
89 switch (encoding) { | 98 switch (encoding) { |
90 case UTF8: | 99 case UTF8: |
91 stream_ = new Utf8ToUtf16CharacterStream(source_, length); | 100 stream_ = new Utf8ToUtf16CharacterStream(source_, length); |
92 break; | 101 break; |
93 case UTF16: { | 102 case UTF16: { |
94 Handle<String> result = isolate->factory()->NewStringFromTwoByte( | 103 Handle<String> result = isolate->factory()->NewStringFromTwoByte( |
95 Vector<const uint16_t>( | 104 Vector<const uint16_t>( |
96 reinterpret_cast<const uint16_t*>(source_), | 105 reinterpret_cast<const uint16_t*>(source_), |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 prefix, Token::Name(value), | 160 prefix, Token::Name(value), |
152 static_cast<int>(beg), static_cast<int>(end)); | 161 static_cast<int>(beg), static_cast<int>(end)); |
153 } | 162 } |
154 }; | 163 }; |
155 | 164 |
156 | 165 |
157 TimeDelta RunBaselineScanner(const char* fname, | 166 TimeDelta RunBaselineScanner(const char* fname, |
158 Isolate* isolate, | 167 Isolate* isolate, |
159 Encoding encoding, | 168 Encoding encoding, |
160 bool dump_tokens, | 169 bool dump_tokens, |
161 std::vector<TokenWithLocation>* tokens) { | 170 std::vector<TokenWithLocation>* tokens, |
| 171 int repeat) { |
162 ElapsedTimer timer; | 172 ElapsedTimer timer; |
163 BaselineScanner scanner(fname, isolate, encoding, &timer); | 173 BaselineScanner scanner(fname, isolate, encoding, &timer, repeat); |
164 Token::Value token; | 174 Token::Value token; |
165 int beg, end; | 175 int beg, end; |
166 do { | 176 do { |
167 token = scanner.Next(&beg, &end); | 177 token = scanner.Next(&beg, &end); |
168 if (dump_tokens) { | 178 if (dump_tokens) { |
169 tokens->push_back(TokenWithLocation(token, beg, end)); | 179 tokens->push_back(TokenWithLocation(token, beg, end)); |
170 } | 180 } |
171 } while (token != Token::EOS); | 181 } while (token != Token::EOS); |
172 return timer.Elapsed(); | 182 return timer.Elapsed(); |
173 } | 183 } |
174 | 184 |
175 | 185 |
176 void PrintTokens(const char* name, | 186 void PrintTokens(const char* name, |
177 const std::vector<TokenWithLocation>& tokens) { | 187 const std::vector<TokenWithLocation>& tokens) { |
178 printf("No of tokens: %d\n", | 188 printf("No of tokens: %d\n", |
179 static_cast<int>(tokens.size())); | 189 static_cast<int>(tokens.size())); |
180 printf("%s:\n", name); | 190 printf("%s:\n", name); |
181 for (size_t i = 0; i < tokens.size(); ++i) { | 191 for (size_t i = 0; i < tokens.size(); ++i) { |
182 tokens[i].Print("=>"); | 192 tokens[i].Print("=>"); |
183 } | 193 } |
184 } | 194 } |
185 | 195 |
186 | 196 |
187 TimeDelta ProcessFile( | 197 TimeDelta ProcessFile( |
188 const char* fname, | 198 const char* fname, |
189 Encoding encoding, | 199 Encoding encoding, |
190 Isolate* isolate, | 200 Isolate* isolate, |
191 bool print_tokens) { | 201 bool print_tokens, |
| 202 int repeat) { |
192 if (print_tokens) { | 203 if (print_tokens) { |
193 printf("Processing file %s\n", fname); | 204 printf("Processing file %s\n", fname); |
194 } | 205 } |
195 HandleScope handle_scope(isolate); | 206 HandleScope handle_scope(isolate); |
196 std::vector<TokenWithLocation> baseline_tokens; | 207 std::vector<TokenWithLocation> baseline_tokens; |
197 TimeDelta baseline_time; | 208 TimeDelta baseline_time; |
198 baseline_time = RunBaselineScanner( | 209 baseline_time = RunBaselineScanner( |
199 fname, isolate, encoding, print_tokens, | 210 fname, isolate, encoding, print_tokens, |
200 &baseline_tokens); | 211 &baseline_tokens, repeat); |
201 if (print_tokens) { | 212 if (print_tokens) { |
202 PrintTokens("Baseline", baseline_tokens); | 213 PrintTokens("Baseline", baseline_tokens); |
203 } | 214 } |
204 return baseline_time; | 215 return baseline_time; |
205 } | 216 } |
206 | 217 |
207 | 218 |
208 int main(int argc, char* argv[]) { | 219 int main(int argc, char* argv[]) { |
209 v8::V8::InitializeICU(); | 220 v8::V8::InitializeICU(); |
210 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 221 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
211 Encoding encoding = LATIN1; | 222 Encoding encoding = LATIN1; |
212 bool print_tokens = false; | 223 bool print_tokens = false; |
213 std::vector<std::string> fnames; | 224 std::vector<std::string> fnames; |
214 std::string benchmark; | 225 std::string benchmark; |
| 226 int repeat = 1; |
215 for (int i = 0; i < argc; ++i) { | 227 for (int i = 0; i < argc; ++i) { |
216 if (strcmp(argv[i], "--latin1") == 0) { | 228 if (strcmp(argv[i], "--latin1") == 0) { |
217 encoding = LATIN1; | 229 encoding = LATIN1; |
218 } else if (strcmp(argv[i], "--utf8") == 0) { | 230 } else if (strcmp(argv[i], "--utf8") == 0) { |
219 encoding = UTF8; | 231 encoding = UTF8; |
220 } else if (strcmp(argv[i], "--utf16") == 0) { | 232 } else if (strcmp(argv[i], "--utf16") == 0) { |
221 encoding = UTF16; | 233 encoding = UTF16; |
222 } else if (strcmp(argv[i], "--print-tokens") == 0) { | 234 } else if (strcmp(argv[i], "--print-tokens") == 0) { |
223 print_tokens = true; | 235 print_tokens = true; |
224 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { | 236 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { |
225 benchmark = std::string(argv[i]).substr(12); | 237 benchmark = std::string(argv[i]).substr(12); |
| 238 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { |
| 239 std::string repeat_str = std::string(argv[i]).substr(9); |
| 240 repeat = atoi(repeat_str.c_str()); |
226 } else if (i > 0 && argv[i][0] != '-') { | 241 } else if (i > 0 && argv[i][0] != '-') { |
227 fnames.push_back(std::string(argv[i])); | 242 fnames.push_back(std::string(argv[i])); |
228 } | 243 } |
229 } | 244 } |
230 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 245 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
231 { | 246 { |
232 v8::HandleScope handle_scope(isolate); | 247 v8::HandleScope handle_scope(isolate); |
233 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 248 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
234 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 249 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
235 ASSERT(!context.IsEmpty()); | 250 ASSERT(!context.IsEmpty()); |
236 { | 251 { |
237 v8::Context::Scope scope(context); | 252 v8::Context::Scope scope(context); |
238 Isolate* isolate = Isolate::Current(); | 253 Isolate* isolate = Isolate::Current(); |
239 double baseline_total = 0; | 254 double baseline_total = 0; |
240 for (size_t i = 0; i < fnames.size(); i++) { | 255 for (size_t i = 0; i < fnames.size(); i++) { |
241 TimeDelta time; | 256 TimeDelta time; |
242 time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens); | 257 time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens, |
| 258 repeat); |
243 baseline_total += time.InMillisecondsF(); | 259 baseline_total += time.InMillisecondsF(); |
244 } | 260 } |
245 if (benchmark.empty()) benchmark = "Baseline"; | 261 if (benchmark.empty()) benchmark = "Baseline"; |
246 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total); | 262 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total); |
247 } | 263 } |
248 } | 264 } |
249 v8::V8::Dispose(); | 265 v8::V8::Dispose(); |
250 return 0; | 266 return 0; |
251 } | 267 } |
OLD | NEW |