blob: 955cbd52e657813e45d56656a71b9558071ec2c1 [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 {
cpu50bc7ea2015-02-12 00:46:2256 if (name->Equals(v8::String::NewFromUtf8(isolate, "IsSingleProcess"))) {
[email protected]d48ef282013-11-28 15:42:4557 return v8::FunctionTemplate::New(isolate, IsSingleProcess);
58 } else if (name->Equals(v8::String::NewFromUtf8(isolate, "HiResTime"))) {
59 return v8::FunctionTemplate::New(isolate, HiResTime);
[email protected]b07f29092009-06-05 07:33:2160 }
[email protected]efc593a52010-08-27 05:12:5661
deepak.s6d9098d2015-04-30 07:31:4862 return v8::Local<v8::FunctionTemplate>();
[email protected]b07f29092009-06-05 07:33:2163 }
64
[email protected]187a3532013-06-13 20:25:0165 static void IsSingleProcess(const v8::FunctionCallbackInfo<v8::Value>& args) {
avi79bf9132014-12-25 17:48:0566 args.GetReturnValue().Set(base::CommandLine::ForCurrentProcess()->HasSwitch(
67 switches::kSingleProcess));
[email protected]46210f02010-07-02 18:03:4168 }
[email protected]efc593a52010-08-27 05:12:5669
[email protected]187a3532013-06-13 20:25:0170 static void HiResTime(const v8::FunctionCallbackInfo<v8::Value>& args) {
71 args.GetReturnValue().Set(
charliea3be839702015-01-26 17:35:4172 static_cast<double>(base::TimeTicks::Now().ToInternalValue()));
[email protected]efc593a52010-08-27 05:12:5673 }
[email protected]b07f29092009-06-05 07:33:2174};
75
76v8::Extension* BenchmarkingExtension::Get() {
77 return new BenchmarkingWrapper();
78}
79
80} // namespace extensions_v8