deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / tools / parser-shell.cc
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 "src/v8.h"
35
36 #include "include/libplatform/libplatform.h"
37 #include "src/api.h"
38 #include "src/compiler.h"
39 #include "src/scanner-character-streams.h"
40 #include "tools/shell-utils.h"
41 #include "src/parser.h"
42 #include "src/preparse-data-format.h"
43 #include "src/preparse-data.h"
44 #include "src/preparser.h"
45
46 using namespace v8::internal;
47
48 class StringResource8 : public v8::String::ExternalOneByteStringResource {
49  public:
50   StringResource8(const char* data, int length)
51       : data_(data), length_(length) { }
52   virtual size_t length() const { return length_; }
53   virtual const char* data() const { return data_; }
54
55  private:
56   const char* data_;
57   int length_;
58 };
59
60 std::pair<v8::base::TimeDelta, v8::base::TimeDelta> RunBaselineParser(
61     const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate,
62     v8::Handle<v8::Context> context) {
63   int length = 0;
64   const byte* source = ReadFileAndRepeat(fname, &length, repeat);
65   v8::Handle<v8::String> source_handle;
66   switch (encoding) {
67     case UTF8: {
68       source_handle = v8::String::NewFromUtf8(
69           isolate, reinterpret_cast<const char*>(source));
70       break;
71     }
72     case UTF16: {
73       source_handle = v8::String::NewFromTwoByte(
74           isolate, reinterpret_cast<const uint16_t*>(source),
75           v8::String::kNormalString, length / 2);
76       break;
77     }
78     case LATIN1: {
79       StringResource8* string_resource =
80           new StringResource8(reinterpret_cast<const char*>(source), length);
81       source_handle = v8::String::NewExternal(isolate, string_resource);
82       break;
83     }
84   }
85   v8::base::TimeDelta parse_time1, parse_time2;
86   Handle<Script> script = Isolate::Current()->factory()->NewScript(
87       v8::Utils::OpenHandle(*source_handle));
88   i::ScriptData* cached_data_impl = NULL;
89   // First round of parsing (produce data to cache).
90   {
91     Zone zone;
92     ParseInfo info(&zone, script);
93     info.set_global();
94     info.set_cached_data(&cached_data_impl);
95     info.set_compile_options(v8::ScriptCompiler::kProduceParserCache);
96     v8::base::ElapsedTimer timer;
97     timer.Start();
98     // Allow lazy parsing; otherwise we won't produce cached data.
99     info.set_allow_lazy_parsing();
100     bool success = Parser::ParseStatic(&info);
101     parse_time1 = timer.Elapsed();
102     if (!success) {
103       fprintf(stderr, "Parsing failed\n");
104       return std::make_pair(v8::base::TimeDelta(), v8::base::TimeDelta());
105     }
106   }
107   // Second round of parsing (consume cached data).
108   {
109     Zone zone;
110     ParseInfo info(&zone, script);
111     info.set_global();
112     info.set_cached_data(&cached_data_impl);
113     info.set_compile_options(v8::ScriptCompiler::kConsumeParserCache);
114     v8::base::ElapsedTimer timer;
115     timer.Start();
116     // Allow lazy parsing; otherwise cached data won't help.
117     info.set_allow_lazy_parsing();
118     bool success = Parser::ParseStatic(&info);
119     parse_time2 = timer.Elapsed();
120     if (!success) {
121       fprintf(stderr, "Parsing failed\n");
122       return std::make_pair(v8::base::TimeDelta(), v8::base::TimeDelta());
123     }
124   }
125   return std::make_pair(parse_time1, parse_time2);
126 }
127
128
129 int main(int argc, char* argv[]) {
130   v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
131   v8::V8::InitializeICU();
132   v8::Platform* platform = v8::platform::CreateDefaultPlatform();
133   v8::V8::InitializePlatform(platform);
134   v8::V8::Initialize();
135   Encoding encoding = LATIN1;
136   std::vector<std::string> fnames;
137   std::string benchmark;
138   int repeat = 1;
139   for (int i = 0; i < argc; ++i) {
140     if (strcmp(argv[i], "--latin1") == 0) {
141       encoding = LATIN1;
142     } else if (strcmp(argv[i], "--utf8") == 0) {
143       encoding = UTF8;
144     } else if (strcmp(argv[i], "--utf16") == 0) {
145       encoding = UTF16;
146     } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
147       benchmark = std::string(argv[i]).substr(12);
148     } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
149       std::string repeat_str = std::string(argv[i]).substr(9);
150       repeat = atoi(repeat_str.c_str());
151     } else if (i > 0 && argv[i][0] != '-') {
152       fnames.push_back(std::string(argv[i]));
153     }
154   }
155   v8::Isolate* isolate = v8::Isolate::New();
156   {
157     v8::Isolate::Scope isolate_scope(isolate);
158     v8::HandleScope handle_scope(isolate);
159     v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
160     v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
161     DCHECK(!context.IsEmpty());
162     {
163       v8::Context::Scope scope(context);
164       double first_parse_total = 0;
165       double second_parse_total = 0;
166       for (size_t i = 0; i < fnames.size(); i++) {
167         std::pair<v8::base::TimeDelta, v8::base::TimeDelta> time =
168             RunBaselineParser(fnames[i].c_str(), encoding, repeat, isolate,
169                               context);
170         first_parse_total += time.first.InMillisecondsF();
171         second_parse_total += time.second.InMillisecondsF();
172       }
173       if (benchmark.empty()) benchmark = "Baseline";
174       printf("%s(FirstParseRunTime): %.f ms\n", benchmark.c_str(),
175              first_parse_total);
176       printf("%s(SecondParseRunTime): %.f ms\n", benchmark.c_str(),
177              second_parse_total);
178     }
179   }
180   v8::V8::Dispose();
181   v8::V8::ShutdownPlatform();
182   delete platform;
183   return 0;
184 }