blob: 810c5b9d174c5d25497ed3c96edd1705fe67af99 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039#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"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044#include "tools/shell-utils.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045
46using namespace v8::internal;
47
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
49 public:
50 virtual void* Allocate(size_t length) {
51 void* data = AllocateUninitialized(length);
52 return data == NULL ? data : memset(data, 0, length);
53 }
54 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
55 virtual void Free(void* data, size_t) { free(data); }
56};
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058class StringResource8 : public v8::String::ExternalOneByteStringResource {
59 public:
60 StringResource8(const char* data, int length)
61 : data_(data), length_(length) { }
62 virtual size_t length() const { return length_; }
63 virtual const char* data() const { return data_; }
64
65 private:
66 const char* data_;
67 int length_;
68};
69
70std::pair<v8::base::TimeDelta, v8::base::TimeDelta> RunBaselineParser(
71 const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 v8::Local<v8::Context> context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 int length = 0;
74 const byte* source = ReadFileAndRepeat(fname, &length, repeat);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 v8::Local<v8::String> source_handle;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 switch (encoding) {
77 case UTF8: {
78 source_handle = v8::String::NewFromUtf8(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 isolate, reinterpret_cast<const char*>(source),
80 v8::NewStringType::kNormal).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 break;
82 }
83 case UTF16: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 source_handle =
85 v8::String::NewFromTwoByte(
86 isolate, reinterpret_cast<const uint16_t*>(source),
87 v8::NewStringType::kNormal, length / 2).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 break;
89 }
90 case LATIN1: {
91 StringResource8* string_resource =
92 new StringResource8(reinterpret_cast<const char*>(source), length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 source_handle = v8::String::NewExternalOneByte(isolate, string_resource)
94 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 break;
96 }
97 }
98 v8::base::TimeDelta parse_time1, parse_time2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 Handle<Script> script =
100 reinterpret_cast<i::Isolate*>(isolate)->factory()->NewScript(
101 v8::Utils::OpenHandle(*source_handle));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 i::ScriptData* cached_data_impl = NULL;
103 // First round of parsing (produce data to cache).
104 {
Ben Murdochda12d292016-06-02 14:46:10 +0100105 Zone zone(reinterpret_cast<i::Isolate*>(isolate)->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 ParseInfo info(&zone, script);
107 info.set_global();
108 info.set_cached_data(&cached_data_impl);
109 info.set_compile_options(v8::ScriptCompiler::kProduceParserCache);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 v8::base::ElapsedTimer timer;
111 timer.Start();
112 // Allow lazy parsing; otherwise we won't produce cached data.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 info.set_allow_lazy_parsing();
114 bool success = Parser::ParseStatic(&info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 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 {
Ben Murdochda12d292016-06-02 14:46:10 +0100123 Zone zone(reinterpret_cast<i::Isolate*>(isolate)->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 ParseInfo info(&zone, script);
125 info.set_global();
126 info.set_cached_data(&cached_data_impl);
127 info.set_compile_options(v8::ScriptCompiler::kConsumeParserCache);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 v8::base::ElapsedTimer timer;
129 timer.Start();
130 // Allow lazy parsing; otherwise cached data won't help.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 info.set_allow_lazy_parsing();
132 bool success = Parser::ParseStatic(&info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 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
143int main(int argc, char* argv[]) {
144 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100145 v8::V8::InitializeICUDefaultLocation(argv[0]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
147 v8::V8::InitializePlatform(platform);
148 v8::V8::Initialize();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 v8::V8::InitializeExternalStartupData(argv[0]);
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 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 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 {
176 v8::Isolate::Scope isolate_scope(isolate);
177 v8::HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 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}