[email protected] | 2ced3a3 | 2011-09-09 21:07:41 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 6f08af8 | 2011-09-15 01:19:03 | [diff] [blame] | 5 | #include "chrome/renderer/benchmarking_extension.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 6 | |
[email protected] | bcd2c65a | 2011-09-20 21:54:11 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 3164fa82 | 2013-06-28 15:32:41 | [diff] [blame] | 8 | #include "base/time/time.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 9 | #include "content/public/common/content_switches.h" |
[email protected] | bd868f4 | 2011-10-12 00:51:00 | [diff] [blame] | 10 | #include "content/public/renderer/render_thread.h" |
[email protected] | 2ced3a3 | 2011-09-09 21:07:41 | [diff] [blame] | 11 | #include "v8/include/v8.h" |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 12 | |
[email protected] | 2ced3a3 | 2011-09-09 21:07:41 | [diff] [blame] | 13 | const char kBenchmarkingExtensionName[] = "v8/Benchmarking"; |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 14 | |
[email protected] | 2ced3a3 | 2011-09-09 21:07:41 | [diff] [blame] | 15 | namespace extensions_v8 { |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 16 | |
17 | class BenchmarkingWrapper : public v8::Extension { | ||||
18 | public: | ||||
19 | BenchmarkingWrapper() : | ||||
20 | v8::Extension(kBenchmarkingExtensionName, | ||||
[email protected] | 31d9ec8 | 2009-07-17 21:30:06 | [diff] [blame] | 21 | "if (typeof(chrome) == 'undefined') {" |
22 | " chrome = {};" | ||||
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 23 | "};" |
[email protected] | 31d9ec8 | 2009-07-17 21:30:06 | [diff] [blame] | 24 | "if (typeof(chrome.benchmarking) == 'undefined') {" |
25 | " chrome.benchmarking = {};" | ||||
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 26 | "};" |
[email protected] | 46210f0 | 2010-07-02 18:03:41 | [diff] [blame] | 27 | "chrome.benchmarking.isSingleProcess = function() {" |
28 | " native function IsSingleProcess();" | ||||
29 | " return IsSingleProcess();" | ||||
30 | "};" | ||||
[email protected] | efc593a5 | 2010-08-27 05:12:56 | [diff] [blame] | 31 | "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] | 31d9ec8 | 2009-07-17 21:30:06 | [diff] [blame] | 51 | ) {} |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 52 | |
deepak.s | 6d9098d | 2015-04-30 07:31:48 | [diff] [blame^] | 53 | v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
[email protected] | d48ef28 | 2013-11-28 15:42:45 | [diff] [blame] | 54 | v8::Isolate* isolate, |
deepak.s | 6d9098d | 2015-04-30 07:31:48 | [diff] [blame^] | 55 | v8::Local<v8::String> name) override { |
cpu | 50bc7ea | 2015-02-12 00:46:22 | [diff] [blame] | 56 | if (name->Equals(v8::String::NewFromUtf8(isolate, "IsSingleProcess"))) { |
[email protected] | d48ef28 | 2013-11-28 15:42:45 | [diff] [blame] | 57 | 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] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 60 | } |
[email protected] | efc593a5 | 2010-08-27 05:12:56 | [diff] [blame] | 61 | |
deepak.s | 6d9098d | 2015-04-30 07:31:48 | [diff] [blame^] | 62 | return v8::Local<v8::FunctionTemplate>(); |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 63 | } |
64 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 65 | static void IsSingleProcess(const v8::FunctionCallbackInfo<v8::Value>& args) { |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 66 | args.GetReturnValue().Set(base::CommandLine::ForCurrentProcess()->HasSwitch( |
67 | switches::kSingleProcess)); | ||||
[email protected] | 46210f0 | 2010-07-02 18:03:41 | [diff] [blame] | 68 | } |
[email protected] | efc593a5 | 2010-08-27 05:12:56 | [diff] [blame] | 69 | |
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 70 | static void HiResTime(const v8::FunctionCallbackInfo<v8::Value>& args) { |
71 | args.GetReturnValue().Set( | ||||
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 72 | static_cast<double>(base::TimeTicks::Now().ToInternalValue())); |
[email protected] | efc593a5 | 2010-08-27 05:12:56 | [diff] [blame] | 73 | } |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 74 | }; |
75 | |||||
76 | v8::Extension* BenchmarkingExtension::Get() { | ||||
77 | return new BenchmarkingWrapper(); | ||||
78 | } | ||||
79 | |||||
80 | } // namespace extensions_v8 |