1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/benchmarking_extension.h"
7 #include "base/command_line.h"
8 #include "base/time/time.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "v8/include/v8.h"
13 const char kBenchmarkingExtensionName
[] = "v8/Benchmarking";
15 namespace extensions_v8
{
17 class BenchmarkingWrapper
: public v8::Extension
{
19 BenchmarkingWrapper() :
20 v8::Extension(kBenchmarkingExtensionName
,
21 "if (typeof(chrome) == 'undefined') {"
24 "if (typeof(chrome.benchmarking) == 'undefined') {"
25 " chrome.benchmarking = {};"
27 "chrome.benchmarking.isSingleProcess = function() {"
28 " native function IsSingleProcess();"
29 " return IsSingleProcess();"
31 "chrome.Interval = function() {"
34 " native function HiResTime();"
35 " this.start = function() {"
37 " start_ = HiResTime();"
39 " this.stop = function() {"
40 " stop_ = HiResTime();"
44 " this.microseconds = function() {"
46 " if (stop == 0 && start_ != 0)"
47 " stop = HiResTime();"
48 " return Math.ceil(stop - start_);"
53 v8::Local
<v8::FunctionTemplate
> GetNativeFunctionTemplate(
55 v8::Local
<v8::String
> name
) override
{
56 if (name
->Equals(v8::String::NewFromUtf8(isolate
, "IsSingleProcess"))) {
57 return v8::FunctionTemplate::New(isolate
, IsSingleProcess
);
58 } else if (name
->Equals(v8::String::NewFromUtf8(isolate
, "HiResTime"))) {
59 return v8::FunctionTemplate::New(isolate
, HiResTime
);
62 return v8::Local
<v8::FunctionTemplate
>();
65 static void IsSingleProcess(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
66 args
.GetReturnValue().Set(base::CommandLine::ForCurrentProcess()->HasSwitch(
67 switches::kSingleProcess
));
70 static void HiResTime(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
71 args
.GetReturnValue().Set(
72 static_cast<double>(base::TimeTicks::Now().ToInternalValue()));
76 v8::Extension
* BenchmarkingExtension::Get() {
77 return new BenchmarkingWrapper();
80 } // namespace extensions_v8