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