1// Copyright 2013 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/base/platform/platform.h"
39#include "src/messages.h"
40#include "src/runtime.h"
41#include "src/scanner-character-streams.h"
42#include "src/scopeinfo.h"
43#include "tools/shell-utils.h"
44#include "src/string-stream.h"
45#include "src/scanner.h"
46
47
48using namespace v8::internal;
49
50
51class BaselineScanner {
52 public:
53  BaselineScanner(const char* fname,
54                  Isolate* isolate,
55                  Encoding encoding,
56                  v8::base::ElapsedTimer* timer,
57                  int repeat)
58      : stream_(NULL) {
59    int length = 0;
60    source_ = ReadFileAndRepeat(fname, &length, repeat);
61    unicode_cache_ = new UnicodeCache();
62    scanner_ = new Scanner(unicode_cache_);
63    switch (encoding) {
64      case UTF8:
65        stream_ = new Utf8ToUtf16CharacterStream(source_, length);
66        break;
67      case UTF16: {
68        Handle<String> result = isolate->factory()->NewStringFromTwoByte(
69            Vector<const uint16_t>(
70                reinterpret_cast<const uint16_t*>(source_),
71                length / 2)).ToHandleChecked();
72        stream_ =
73            new GenericStringUtf16CharacterStream(result, 0, result->length());
74        break;
75      }
76      case LATIN1: {
77        Handle<String> result = isolate->factory()->NewStringFromOneByte(
78            Vector<const uint8_t>(source_, length)).ToHandleChecked();
79        stream_ =
80            new GenericStringUtf16CharacterStream(result, 0, result->length());
81        break;
82      }
83    }
84    timer->Start();
85    scanner_->Initialize(stream_);
86  }
87
88  ~BaselineScanner() {
89    delete scanner_;
90    delete stream_;
91    delete unicode_cache_;
92    delete[] source_;
93  }
94
95  Token::Value Next(int* beg_pos, int* end_pos) {
96    Token::Value res = scanner_->Next();
97    *beg_pos = scanner_->location().beg_pos;
98    *end_pos = scanner_->location().end_pos;
99    return res;
100  }
101
102 private:
103  UnicodeCache* unicode_cache_;
104  Scanner* scanner_;
105  const byte* source_;
106  BufferedUtf16CharacterStream* stream_;
107};
108
109
110struct TokenWithLocation {
111  Token::Value value;
112  size_t beg;
113  size_t end;
114  TokenWithLocation() : value(Token::ILLEGAL), beg(0), end(0) { }
115  TokenWithLocation(Token::Value value, size_t beg, size_t end) :
116      value(value), beg(beg), end(end) { }
117  bool operator==(const TokenWithLocation& other) {
118    return value == other.value && beg == other.beg && end == other.end;
119  }
120  bool operator!=(const TokenWithLocation& other) {
121    return !(*this == other);
122  }
123  void Print(const char* prefix) const {
124    printf("%s %11s at (%d, %d)\n",
125           prefix, Token::Name(value),
126           static_cast<int>(beg), static_cast<int>(end));
127  }
128};
129
130
131v8::base::TimeDelta RunBaselineScanner(const char* fname, Isolate* isolate,
132                                       Encoding encoding, bool dump_tokens,
133                                       std::vector<TokenWithLocation>* tokens,
134                                       int repeat) {
135  v8::base::ElapsedTimer timer;
136  BaselineScanner scanner(fname, isolate, encoding, &timer, repeat);
137  Token::Value token;
138  int beg, end;
139  do {
140    token = scanner.Next(&beg, &end);
141    if (dump_tokens) {
142      tokens->push_back(TokenWithLocation(token, beg, end));
143    }
144  } while (token != Token::EOS);
145  return timer.Elapsed();
146}
147
148
149void PrintTokens(const char* name,
150                 const std::vector<TokenWithLocation>& tokens) {
151  printf("No of tokens: %d\n",
152         static_cast<int>(tokens.size()));
153  printf("%s:\n", name);
154  for (size_t i = 0; i < tokens.size(); ++i) {
155    tokens[i].Print("=>");
156  }
157}
158
159
160v8::base::TimeDelta ProcessFile(
161    const char* fname,
162    Encoding encoding,
163    Isolate* isolate,
164    bool print_tokens,
165    int repeat) {
166  if (print_tokens) {
167    printf("Processing file %s\n", fname);
168  }
169  HandleScope handle_scope(isolate);
170  std::vector<TokenWithLocation> baseline_tokens;
171  v8::base::TimeDelta baseline_time;
172  baseline_time = RunBaselineScanner(
173      fname, isolate, encoding, print_tokens,
174      &baseline_tokens, repeat);
175  if (print_tokens) {
176    PrintTokens("Baseline", baseline_tokens);
177  }
178  return baseline_time;
179}
180
181
182int main(int argc, char* argv[]) {
183  v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
184  v8::V8::InitializeICU();
185  v8::Platform* platform = v8::platform::CreateDefaultPlatform();
186  v8::V8::InitializePlatform(platform);
187  v8::V8::Initialize();
188  Encoding encoding = LATIN1;
189  bool print_tokens = false;
190  std::vector<std::string> fnames;
191  std::string benchmark;
192  int repeat = 1;
193  for (int i = 0; i < argc; ++i) {
194    if (strcmp(argv[i], "--latin1") == 0) {
195      encoding = LATIN1;
196    } else if (strcmp(argv[i], "--utf8") == 0) {
197      encoding = UTF8;
198    } else if (strcmp(argv[i], "--utf16") == 0) {
199      encoding = UTF16;
200    } else if (strcmp(argv[i], "--print-tokens") == 0) {
201      print_tokens = true;
202    } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
203      benchmark = std::string(argv[i]).substr(12);
204    } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
205      std::string repeat_str = std::string(argv[i]).substr(9);
206      repeat = atoi(repeat_str.c_str());
207    } else if (i > 0 && argv[i][0] != '-') {
208      fnames.push_back(std::string(argv[i]));
209    }
210  }
211  v8::Isolate* isolate = v8::Isolate::New();
212  {
213    v8::Isolate::Scope isolate_scope(isolate);
214    v8::HandleScope handle_scope(isolate);
215    v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
216    v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
217    DCHECK(!context.IsEmpty());
218    {
219      v8::Context::Scope scope(context);
220      double baseline_total = 0;
221      for (size_t i = 0; i < fnames.size(); i++) {
222        v8::base::TimeDelta time;
223        time = ProcessFile(fnames[i].c_str(), encoding,
224                           reinterpret_cast<Isolate*>(isolate), print_tokens,
225                           repeat);
226        baseline_total += time.InMillisecondsF();
227      }
228      if (benchmark.empty()) benchmark = "Baseline";
229      printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total);
230    }
231  }
232  v8::V8::Dispose();
233  v8::V8::ShutdownPlatform();
234  delete platform;
235  return 0;
236}
237