OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include <assert.h> |
| 29 #include <string.h> |
| 30 #include <stdio.h> |
| 31 #include <stdlib.h> |
| 32 #include <string> |
| 33 #include <vector> |
| 34 #include "v8.h" |
| 35 |
| 36 #include "api.h" |
| 37 #include "compiler.h" |
| 38 #include "scanner-character-streams.h" |
| 39 #include "shell-utils.h" |
| 40 #include "parser.h" |
| 41 #include "preparse-data-format.h" |
| 42 #include "preparse-data.h" |
| 43 #include "preparser.h" |
| 44 |
| 45 using namespace v8::internal; |
| 46 |
| 47 enum TestMode { |
| 48 PreParseAndParse, |
| 49 PreParse, |
| 50 Parse |
| 51 }; |
| 52 |
| 53 std::pair<TimeDelta, TimeDelta> RunBaselineParser( |
| 54 const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate, |
| 55 v8::Handle<v8::Context> context, TestMode test_mode) { |
| 56 int length = 0; |
| 57 const byte* source = ReadFileAndRepeat(fname, &length, repeat); |
| 58 v8::Handle<v8::String> source_handle; |
| 59 switch (encoding) { |
| 60 case UTF8: { |
| 61 source_handle = v8::String::NewFromUtf8( |
| 62 isolate, reinterpret_cast<const char*>(source)); |
| 63 break; |
| 64 } |
| 65 case UTF16: { |
| 66 source_handle = v8::String::NewFromTwoByte( |
| 67 isolate, reinterpret_cast<const uint16_t*>(source), |
| 68 v8::String::kNormalString, length / 2); |
| 69 break; |
| 70 } |
| 71 case LATIN1: { |
| 72 source_handle = v8::String::NewFromOneByte(isolate, source); |
| 73 break; |
| 74 } |
| 75 } |
| 76 v8::ScriptData* cached_data = NULL; |
| 77 TimeDelta preparse_time, parse_time; |
| 78 if (test_mode == PreParseAndParse || test_mode == PreParse) { |
| 79 ElapsedTimer timer; |
| 80 timer.Start(); |
| 81 cached_data = v8::ScriptData::PreCompile(source_handle); |
| 82 preparse_time = timer.Elapsed(); |
| 83 if (cached_data == NULL || cached_data->HasError()) { |
| 84 fprintf(stderr, "Preparsing failed\n"); |
| 85 return std::make_pair(TimeDelta(), TimeDelta()); |
| 86 } |
| 87 } |
| 88 if (test_mode == PreParseAndParse || test_mode == Parse) { |
| 89 Handle<String> str = v8::Utils::OpenHandle(*source_handle); |
| 90 i::Isolate* internal_isolate = str->GetIsolate(); |
| 91 Handle<Script> script = internal_isolate->factory()->NewScript(str); |
| 92 CompilationInfoWithZone info(script); |
| 93 info.MarkAsGlobal(); |
| 94 i::ScriptDataImpl* cached_data_impl = |
| 95 static_cast<i::ScriptDataImpl*>(cached_data); |
| 96 if (test_mode == PreParseAndParse) { |
| 97 info.SetCachedData(&cached_data_impl, |
| 98 i::CONSUME_CACHED_DATA); |
| 99 } |
| 100 info.SetContext(v8::Utils::OpenHandle(*context)); |
| 101 ElapsedTimer timer; |
| 102 timer.Start(); |
| 103 // Allow lazy parsing; otherwise the preparse data won't help. |
| 104 bool success = Parser::Parse(&info, true); |
| 105 parse_time = timer.Elapsed(); |
| 106 if (!success) { |
| 107 fprintf(stderr, "Parsing failed\n"); |
| 108 return std::make_pair(TimeDelta(), TimeDelta()); |
| 109 } |
| 110 } |
| 111 return std::make_pair(preparse_time, parse_time); |
| 112 } |
| 113 |
| 114 |
| 115 int main(int argc, char* argv[]) { |
| 116 v8::V8::InitializeICU(); |
| 117 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 118 Encoding encoding = LATIN1; |
| 119 TestMode test_mode = PreParseAndParse; |
| 120 std::vector<std::string> fnames; |
| 121 std::string benchmark; |
| 122 int repeat = 1; |
| 123 for (int i = 0; i < argc; ++i) { |
| 124 if (strcmp(argv[i], "--latin1") == 0) { |
| 125 encoding = LATIN1; |
| 126 } else if (strcmp(argv[i], "--utf8") == 0) { |
| 127 encoding = UTF8; |
| 128 } else if (strcmp(argv[i], "--utf16") == 0) { |
| 129 encoding = UTF16; |
| 130 } else if (strcmp(argv[i], "--preparse-and-parse") == 0) { |
| 131 test_mode = PreParseAndParse; |
| 132 } else if (strcmp(argv[i], "--preparse") == 0) { |
| 133 test_mode = PreParse; |
| 134 } else if (strcmp(argv[i], "--parse") == 0) { |
| 135 test_mode = Parse; |
| 136 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { |
| 137 benchmark = std::string(argv[i]).substr(12); |
| 138 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { |
| 139 std::string repeat_str = std::string(argv[i]).substr(9); |
| 140 repeat = atoi(repeat_str.c_str()); |
| 141 } else if (i > 0 && argv[i][0] != '-') { |
| 142 fnames.push_back(std::string(argv[i])); |
| 143 } |
| 144 } |
| 145 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 146 { |
| 147 v8::HandleScope handle_scope(isolate); |
| 148 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
| 149 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 150 ASSERT(!context.IsEmpty()); |
| 151 { |
| 152 v8::Context::Scope scope(context); |
| 153 double preparse_total = 0; |
| 154 double parse_total = 0; |
| 155 for (size_t i = 0; i < fnames.size(); i++) { |
| 156 std::pair<TimeDelta, TimeDelta> time = RunBaselineParser( |
| 157 fnames[i].c_str(), encoding, repeat, isolate, context, test_mode); |
| 158 preparse_total += time.first.InMillisecondsF(); |
| 159 parse_total += time.second.InMillisecondsF(); |
| 160 } |
| 161 if (benchmark.empty()) benchmark = "Baseline"; |
| 162 printf("%s(PreParseRunTime): %.f ms\n", benchmark.c_str(), |
| 163 preparse_total); |
| 164 printf("%s(ParseRunTime): %.f ms\n", benchmark.c_str(), parse_total); |
| 165 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), |
| 166 preparse_total + parse_total); |
| 167 } |
| 168 } |
| 169 v8::V8::Dispose(); |
| 170 return 0; |
| 171 } |
OLD | NEW |