blob: 68ed3660df7d9669d978e34754201af565ba65fd [file] [log] [blame]
[email protected]2ced3a32011-09-09 21:07:411// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b07f29092009-06-05 07:33:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6f08af82011-09-15 01:19:035#include "chrome/renderer/benchmarking_extension.h"
[email protected]835d7c82010-10-14 04:38:386
[email protected]bcd2c65a2011-09-20 21:54:117#include "base/command_line.h"
[email protected]3164fa822013-06-28 15:32:418#include "base/time/time.h"
[email protected]c08950d22011-10-13 22:20:299#include "content/public/common/content_switches.h"
[email protected]bd868f42011-10-12 00:51:0010#include "content/public/renderer/render_thread.h"
[email protected]2ced3a32011-09-09 21:07:4111#include "v8/include/v8.h"
[email protected]b07f29092009-06-05 07:33:2112
[email protected]2ced3a32011-09-09 21:07:4113const char kBenchmarkingExtensionName[] = "v8/Benchmarking";
[email protected]b07f29092009-06-05 07:33:2114
[email protected]2ced3a32011-09-09 21:07:4115namespace extensions_v8 {
[email protected]b07f29092009-06-05 07:33:2116
17class BenchmarkingWrapper : public v8::Extension {
18 public:
19 BenchmarkingWrapper() :
20 v8::Extension(kBenchmarkingExtensionName,
[email protected]31d9ec82009-07-17 21:30:0621 "if (typeof(chrome) == 'undefined') {"
22 " chrome = {};"
[email protected]b07f29092009-06-05 07:33:2123 "};"
[email protected]31d9ec82009-07-17 21:30:0624 "if (typeof(chrome.benchmarking) == 'undefined') {"
25 " chrome.benchmarking = {};"
[email protected]b07f29092009-06-05 07:33:2126 "};"
[email protected]46210f02010-07-02 18:03:4127 "chrome.benchmarking.isSingleProcess = function() {"
28 " native function IsSingleProcess();"
29 " return IsSingleProcess();"
30 "};"
[email protected]efc593a52010-08-27 05:12:5631 "chrome.Interval = function() {"
32 " var start_ = 0;"
33 " var stop_ = 0;"
34 " native function HiResTime();"
35 " this.start = function() {"
36 " stop_ = 0;"
37 " start_ = HiResTime();"
38 " };"
39 " this.stop = function() {"
40 " stop_ = HiResTime();"
41 " if (start_ == 0)"
42 " stop_ = 0;"
43 " };"
44 " this.microseconds = function() {"
45 " var stop = stop_;"
46 " if (stop == 0 && start_ != 0)"
47 " stop = HiResTime();"
48 " return Math.ceil(stop - start_);"
49 " };"
50 "}"
[email protected]31d9ec82009-07-17 21:30:0651 ) {}
[email protected]b07f29092009-06-05 07:33:2152
deepak.s6d9098d2015-04-30 07:31:4853 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
[email protected]d48ef282013-11-28 15:42:4554 v8::Isolate* isolate,
deepak.s6d9098d2015-04-30 07:31:4855 v8::Local<v8::String> name) override {
Dan Elphicke55317832018-08-02 20:33:3456 if (name->StringEquals(
Yang Guo1d9f5a62018-09-25 19:17:3757 v8::String::NewFromUtf8(isolate, "IsSingleProcess",
58 v8::NewStringType::kInternalized)
59 .ToLocalChecked())) {
[email protected]d48ef282013-11-28 15:42:4560 return v8::FunctionTemplate::New(isolate, IsSingleProcess);
Dan Elphicke55317832018-08-02 20:33:3461 } else if (name->StringEquals(
Yang Guo1d9f5a62018-09-25 19:17:3762 v8::String::NewFromUtf8(isolate, "HiResTime",
63 v8::NewStringType::kInternalized)
64 .ToLocalChecked())) {
[email protected]d48ef282013-11-28 15:42:4565 return v8::FunctionTemplate::New(isolate, HiResTime);
[email protected]b07f29092009-06-05 07:33:2166 }
[email protected]efc593a52010-08-27 05:12:5667
deepak.s6d9098d2015-04-30 07:31:4868 return v8::Local<v8::FunctionTemplate>();
[email protected]b07f29092009-06-05 07:33:2169 }
70
[email protected]187a3532013-06-13 20:25:0171 static void IsSingleProcess(const v8::FunctionCallbackInfo<v8::Value>& args) {
avi79bf9132014-12-25 17:48:0572 args.GetReturnValue().Set(base::CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kSingleProcess));
[email protected]46210f02010-07-02 18:03:4174 }
[email protected]efc593a52010-08-27 05:12:5675
[email protected]187a3532013-06-13 20:25:0176 static void HiResTime(const v8::FunctionCallbackInfo<v8::Value>& args) {
77 args.GetReturnValue().Set(
charliea3be839702015-01-26 17:35:4178 static_cast<double>(base::TimeTicks::Now().ToInternalValue()));
[email protected]efc593a52010-08-27 05:12:5679 }
[email protected]b07f29092009-06-05 07:33:2180};
81
Clemens Hammacher38b6a762019-02-05 13:07:2282std::unique_ptr<v8::Extension> BenchmarkingExtension::Get() {
83 return std::make_unique<BenchmarkingWrapper>();
[email protected]b07f29092009-06-05 07:33:2184}
85
86} // namespace extensions_v8