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/objects-inl.h"
40 #include "src/parsing/parse-info.h"
41 #include "src/parsing/parsing.h"
42 #include "src/parsing/preparse-data-format.h"
43 #include "src/parsing/preparse-data.h"
44 #include "src/parsing/preparser.h"
45 #include "src/parsing/scanner-character-streams.h"
46 #include "tools/shell-utils.h"
47
48 using namespace v8::internal;
49
50 class StringResource8 : public v8::String::ExternalOneByteStringResource {
51 public:
StringResource8(const char * data,int length)52 StringResource8(const char* data, int length)
53 : data_(data), length_(length) { }
length() const54 virtual size_t length() const { return length_; }
data() const55 virtual const char* data() const { return data_; }
56
57 private:
58 const char* data_;
59 int length_;
60 };
61
RunBaselineParser(const char * fname,Encoding encoding,int repeat,v8::Isolate * isolate,v8::Local<v8::Context> context)62 std::pair<v8::base::TimeDelta, v8::base::TimeDelta> RunBaselineParser(
63 const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate,
64 v8::Local<v8::Context> context) {
65 int length = 0;
66 const byte* source = ReadFileAndRepeat(fname, &length, repeat);
67 v8::Local<v8::String> source_handle;
68 switch (encoding) {
69 case UTF8: {
70 source_handle = v8::String::NewFromUtf8(
71 isolate, reinterpret_cast<const char*>(source),
72 v8::NewStringType::kNormal).ToLocalChecked();
73 break;
74 }
75 case UTF16: {
76 source_handle =
77 v8::String::NewFromTwoByte(
78 isolate, reinterpret_cast<const uint16_t*>(source),
79 v8::NewStringType::kNormal, length / 2).ToLocalChecked();
80 break;
81 }
82 case LATIN1: {
83 StringResource8* string_resource =
84 new StringResource8(reinterpret_cast<const char*>(source), length);
85 source_handle = v8::String::NewExternalOneByte(isolate, string_resource)
86 .ToLocalChecked();
87 break;
88 }
89 }
90 v8::base::TimeDelta parse_time1, parse_time2;
91 Handle<Script> script =
92 reinterpret_cast<i::Isolate*>(isolate)->factory()->NewScript(
93 v8::Utils::OpenHandle(*source_handle));
94 i::ScriptData* cached_data_impl = NULL;
95 // First round of parsing (produce data to cache).
96 {
97 ParseInfo info(script);
98 info.set_cached_data(&cached_data_impl);
99 info.set_compile_options(v8::ScriptCompiler::kProduceParserCache);
100 v8::base::ElapsedTimer timer;
101 timer.Start();
102 bool success = parsing::ParseProgram(&info);
103 parse_time1 = timer.Elapsed();
104 if (!success) {
105 fprintf(stderr, "Parsing failed\n");
106 return std::make_pair(v8::base::TimeDelta(), v8::base::TimeDelta());
107 }
108 }
109 // Second round of parsing (consume cached data).
110 {
111 ParseInfo info(script);
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 bool success = parsing::ParseProgram(&info);
117 parse_time2 = timer.Elapsed();
118 if (!success) {
119 fprintf(stderr, "Parsing failed\n");
120 return std::make_pair(v8::base::TimeDelta(), v8::base::TimeDelta());
121 }
122 }
123 return std::make_pair(parse_time1, parse_time2);
124 }
125
126
main(int argc,char * argv[])127 int main(int argc, char* argv[]) {
128 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
129 v8::V8::InitializeICUDefaultLocation(argv[0]);
130 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
131 v8::V8::InitializePlatform(platform);
132 v8::V8::Initialize();
133 v8::V8::InitializeExternalStartupData(argv[0]);
134
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::CreateParams create_params;
156 create_params.array_buffer_allocator =
157 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
158 v8::Isolate* isolate = v8::Isolate::New(create_params);
159 {
160 v8::Isolate::Scope isolate_scope(isolate);
161 v8::HandleScope handle_scope(isolate);
162 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
163 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
164 DCHECK(!context.IsEmpty());
165 {
166 v8::Context::Scope scope(context);
167 double first_parse_total = 0;
168 double second_parse_total = 0;
169 for (size_t i = 0; i < fnames.size(); i++) {
170 std::pair<v8::base::TimeDelta, v8::base::TimeDelta> time =
171 RunBaselineParser(fnames[i].c_str(), encoding, repeat, isolate,
172 context);
173 first_parse_total += time.first.InMillisecondsF();
174 second_parse_total += time.second.InMillisecondsF();
175 }
176 if (benchmark.empty()) benchmark = "Baseline";
177 printf("%s(FirstParseRunTime): %.f ms\n", benchmark.c_str(),
178 first_parse_total);
179 printf("%s(SecondParseRunTime): %.f ms\n", benchmark.c_str(),
180 second_parse_total);
181 }
182 }
183 v8::V8::Dispose();
184 v8::V8::ShutdownPlatform();
185 delete platform;
186 delete create_params.array_buffer_allocator;
187 return 0;
188 }
189