parser-shell.cc 6.05 KB
Newer Older
marja@chromium.org's avatar
marja@chromium.org committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
34
#include "src/v8.h"
marja@chromium.org's avatar
marja@chromium.org committed
35

36
#include "include/libplatform/libplatform.h"
37 38
#include "src/api.h"
#include "src/compiler.h"
39
#include "src/objects-inl.h"
40
#include "src/parsing/parse-info.h"
41
#include "src/parsing/parsing.h"
42 43
#include "src/parsing/preparse-data.h"
#include "src/parsing/preparser.h"
44
#include "src/parsing/scanner-character-streams.h"
45
#include "tools/shell-utils.h"
marja@chromium.org's avatar
marja@chromium.org committed
46 47 48

using namespace v8::internal;

49
class StringResource8 : public v8::String::ExternalOneByteStringResource {
50 51 52 53 54 55 56 57 58 59 60
 public:
  StringResource8(const char* data, int length)
      : data_(data), length_(length) { }
  virtual size_t length() const { return length_; }
  virtual const char* data() const { return data_; }

 private:
  const char* data_;
  int length_;
};

61 62 63
v8::base::TimeDelta RunBaselineParser(const char* fname, Encoding encoding,
                                      int repeat, v8::Isolate* isolate,
                                      v8::Local<v8::Context> context) {
marja@chromium.org's avatar
marja@chromium.org committed
64 65
  int length = 0;
  const byte* source = ReadFileAndRepeat(fname, &length, repeat);
66
  v8::Local<v8::String> source_handle;
marja@chromium.org's avatar
marja@chromium.org committed
67 68 69
  switch (encoding) {
    case UTF8: {
      source_handle = v8::String::NewFromUtf8(
70 71
                          isolate, reinterpret_cast<const char*>(source),
                          v8::NewStringType::kNormal).ToLocalChecked();
marja@chromium.org's avatar
marja@chromium.org committed
72 73 74
      break;
    }
    case UTF16: {
75 76 77 78
      source_handle =
          v8::String::NewFromTwoByte(
              isolate, reinterpret_cast<const uint16_t*>(source),
              v8::NewStringType::kNormal, length / 2).ToLocalChecked();
marja@chromium.org's avatar
marja@chromium.org committed
79 80 81
      break;
    }
    case LATIN1: {
82 83
      StringResource8* string_resource =
          new StringResource8(reinterpret_cast<const char*>(source), length);
84 85
      source_handle = v8::String::NewExternalOneByte(isolate, string_resource)
                          .ToLocalChecked();
marja@chromium.org's avatar
marja@chromium.org committed
86 87 88
      break;
    }
  }
89
  v8::base::TimeDelta parse_time1;
90 91 92
  Handle<Script> script =
      reinterpret_cast<i::Isolate*>(isolate)->factory()->NewScript(
          v8::Utils::OpenHandle(*source_handle));
93 94 95 96 97 98 99 100 101
  ParseInfo info(script);
  v8::base::ElapsedTimer timer;
  timer.Start();
  bool success =
      parsing::ParseProgram(&info, reinterpret_cast<i::Isolate*>(isolate));
  parse_time1 = timer.Elapsed();
  if (!success) {
    fprintf(stderr, "Parsing failed\n");
    return v8::base::TimeDelta();
marja@chromium.org's avatar
marja@chromium.org committed
102
  }
103
  return parse_time1;
marja@chromium.org's avatar
marja@chromium.org committed
104 105 106 107
}


int main(int argc, char* argv[]) {
108
  v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
109
  v8::V8::InitializeICUDefaultLocation(argv[0]);
110 111
  std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
  v8::V8::InitializePlatform(platform.get());
112
  v8::V8::Initialize();
vogelheim's avatar
vogelheim committed
113 114
  v8::V8::InitializeExternalStartupData(argv[0]);

marja@chromium.org's avatar
marja@chromium.org committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  Encoding encoding = LATIN1;
  std::vector<std::string> fnames;
  std::string benchmark;
  int repeat = 1;
  for (int i = 0; i < argc; ++i) {
    if (strcmp(argv[i], "--latin1") == 0) {
      encoding = LATIN1;
    } else if (strcmp(argv[i], "--utf8") == 0) {
      encoding = UTF8;
    } else if (strcmp(argv[i], "--utf16") == 0) {
      encoding = UTF16;
    } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
      benchmark = std::string(argv[i]).substr(12);
    } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
      std::string repeat_str = std::string(argv[i]).substr(9);
      repeat = atoi(repeat_str.c_str());
    } else if (i > 0 && argv[i][0] != '-') {
      fnames.push_back(std::string(argv[i]));
    }
  }
135
  v8::Isolate::CreateParams create_params;
136 137
  create_params.array_buffer_allocator =
      v8::ArrayBuffer::Allocator::NewDefaultAllocator();
138
  v8::Isolate* isolate = v8::Isolate::New(create_params);
marja@chromium.org's avatar
marja@chromium.org committed
139
  {
140
    v8::Isolate::Scope isolate_scope(isolate);
marja@chromium.org's avatar
marja@chromium.org committed
141
    v8::HandleScope handle_scope(isolate);
142
    v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
marja@chromium.org's avatar
marja@chromium.org committed
143
    v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
144
    DCHECK(!context.IsEmpty());
marja@chromium.org's avatar
marja@chromium.org committed
145 146
    {
      v8::Context::Scope scope(context);
147
      double first_parse_total = 0;
marja@chromium.org's avatar
marja@chromium.org committed
148
      for (size_t i = 0; i < fnames.size(); i++) {
149 150 151
        v8::base::TimeDelta time = RunBaselineParser(
            fnames[i].c_str(), encoding, repeat, isolate, context);
        first_parse_total += time.InMillisecondsF();
marja@chromium.org's avatar
marja@chromium.org committed
152 153
      }
      if (benchmark.empty()) benchmark = "Baseline";
154
      printf("%s(ParseRunTime): %.f ms\n", benchmark.c_str(),
155
             first_parse_total);
marja@chromium.org's avatar
marja@chromium.org committed
156 157 158
    }
  }
  v8::V8::Dispose();
159
  v8::V8::ShutdownPlatform();
160
  delete create_params.array_buffer_allocator;
marja@chromium.org's avatar
marja@chromium.org committed
161 162
  return 0;
}