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/metrics/stats_table.h"
9 #include "base/time/time.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "v8/include/v8.h"
14 const char kBenchmarkingExtensionName
[] = "v8/Benchmarking";
16 namespace extensions_v8
{
18 class BenchmarkingWrapper
: public v8::Extension
{
20 BenchmarkingWrapper() :
21 v8::Extension(kBenchmarkingExtensionName
,
22 "if (typeof(chrome) == 'undefined') {"
25 "if (typeof(chrome.benchmarking) == 'undefined') {"
26 " chrome.benchmarking = {};"
28 "chrome.benchmarking.counter = function(name) {"
29 " native function GetCounter();"
30 " return GetCounter(name);"
32 "chrome.benchmarking.isSingleProcess = function() {"
33 " native function IsSingleProcess();"
34 " return IsSingleProcess();"
36 "chrome.Interval = function() {"
39 " native function HiResTime();"
40 " this.start = function() {"
42 " start_ = HiResTime();"
44 " this.stop = function() {"
45 " stop_ = HiResTime();"
49 " this.microseconds = function() {"
51 " if (stop == 0 && start_ != 0)"
52 " stop = HiResTime();"
53 " return Math.ceil(stop - start_);"
58 virtual v8::Handle
<v8::FunctionTemplate
> GetNativeFunction(
59 v8::Handle
<v8::String
> name
) OVERRIDE
{
60 if (name
->Equals(v8::String::New("GetCounter"))) {
61 return v8::FunctionTemplate::New(GetCounter
);
62 } else if (name
->Equals(v8::String::New("IsSingleProcess"))) {
63 return v8::FunctionTemplate::New(IsSingleProcess
);
64 } else if (name
->Equals(v8::String::New("HiResTime"))) {
65 return v8::FunctionTemplate::New(HiResTime
);
68 return v8::Handle
<v8::FunctionTemplate
>();
71 static void GetCounter(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
72 if (!args
.Length() || !args
[0]->IsString() || !base::StatsTable::current())
75 // Extract the name argument
79 args
[0]->ToString()->WriteUtf8(&name
[2], sizeof(name
) - 3);
81 int counter
= base::StatsTable::current()->GetCounterValue(name
);
82 args
.GetReturnValue().Set(static_cast<int32_t>(counter
));
85 static void IsSingleProcess(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
86 args
.GetReturnValue().Set(
87 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
));
90 static void HiResTime(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
91 args
.GetReturnValue().Set(
92 static_cast<double>(base::TimeTicks::HighResNow().ToInternalValue()));
96 v8::Extension
* BenchmarkingExtension::Get() {
97 return new BenchmarkingWrapper();
100 } // namespace extensions_v8