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"
10 #include "chrome/common/benchmarking_messages.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h"
14 #include "v8/include/v8.h"
16 using WebKit::WebCache
;
18 const char kBenchmarkingExtensionName
[] = "v8/Benchmarking";
20 namespace extensions_v8
{
22 class BenchmarkingWrapper
: public v8::Extension
{
24 BenchmarkingWrapper() :
25 v8::Extension(kBenchmarkingExtensionName
,
26 "if (typeof(chrome) == 'undefined') {"
29 "if (typeof(chrome.benchmarking) == 'undefined') {"
30 " chrome.benchmarking = {};"
32 "chrome.benchmarking.clearCache = function(preserve_ssl_entries) {"
33 " native function ClearCache();"
34 " ClearCache(preserve_ssl_entries);"
36 "chrome.benchmarking.clearHostResolverCache = function() {"
37 " native function ClearHostResolverCache();"
38 " ClearHostResolverCache();"
40 "chrome.benchmarking.clearPredictorCache = function() {"
41 " native function ClearPredictorCache();"
42 " ClearPredictorCache();"
44 "chrome.benchmarking.closeConnections = function() {"
45 " native function CloseConnections();"
46 " CloseConnections();"
48 "chrome.benchmarking.counter = function(name) {"
49 " native function GetCounter();"
50 " return GetCounter(name);"
52 "chrome.benchmarking.enableSpdy = function(name) {"
53 " native function EnableSpdy();"
56 "chrome.benchmarking.isSingleProcess = function() {"
57 " native function IsSingleProcess();"
58 " return IsSingleProcess();"
60 "chrome.Interval = function() {"
63 " native function HiResTime();"
64 " this.start = function() {"
66 " start_ = HiResTime();"
68 " this.stop = function() {"
69 " stop_ = HiResTime();"
73 " this.microseconds = function() {"
75 " if (stop == 0 && start_ != 0)"
76 " stop = HiResTime();"
77 " return Math.ceil(stop - start_);"
82 virtual v8::Handle
<v8::FunctionTemplate
> GetNativeFunction(
83 v8::Handle
<v8::String
> name
) {
84 if (name
->Equals(v8::String::New("CloseConnections"))) {
85 return v8::FunctionTemplate::New(CloseConnections
);
86 } else if (name
->Equals(v8::String::New("ClearCache"))) {
87 return v8::FunctionTemplate::New(ClearCache
);
88 } else if (name
->Equals(v8::String::New("ClearHostResolverCache"))) {
89 return v8::FunctionTemplate::New(ClearHostResolverCache
);
90 } else if (name
->Equals(v8::String::New("ClearPredictorCache"))) {
91 return v8::FunctionTemplate::New(ClearPredictorCache
);
92 } else if (name
->Equals(v8::String::New("EnableSpdy"))) {
93 return v8::FunctionTemplate::New(EnableSpdy
);
94 } else if (name
->Equals(v8::String::New("GetCounter"))) {
95 return v8::FunctionTemplate::New(GetCounter
);
96 } else if (name
->Equals(v8::String::New("IsSingleProcess"))) {
97 return v8::FunctionTemplate::New(IsSingleProcess
);
98 } else if (name
->Equals(v8::String::New("HiResTime"))) {
99 return v8::FunctionTemplate::New(HiResTime
);
102 return v8::Handle
<v8::FunctionTemplate
>();
105 static v8::Handle
<v8::Value
> CloseConnections(const v8::Arguments
& args
) {
106 content::RenderThread::Get()->Send(
107 new ChromeViewHostMsg_CloseCurrentConnections());
108 return v8::Undefined();
111 static v8::Handle
<v8::Value
> ClearCache(const v8::Arguments
& args
) {
112 bool preserve_ssl_host_entries
= false;
113 if (args
.Length() && args
[0]->IsBoolean())
114 preserve_ssl_host_entries
= args
[0]->BooleanValue();
116 content::RenderThread::Get()->Send(new ChromeViewHostMsg_ClearCache(
117 preserve_ssl_host_entries
, &rv
));
119 return v8::Undefined();
122 static v8::Handle
<v8::Value
> ClearHostResolverCache(
123 const v8::Arguments
& args
) {
125 content::RenderThread::Get()->Send(
126 new ChromeViewHostMsg_ClearHostResolverCache(&rv
));
127 return v8::Undefined();
130 static v8::Handle
<v8::Value
> ClearPredictorCache(
131 const v8::Arguments
& args
) {
133 content::RenderThread::Get()->Send(
134 new ChromeViewHostMsg_ClearPredictorCache(&rv
));
135 return v8::Undefined();
138 static v8::Handle
<v8::Value
> EnableSpdy(const v8::Arguments
& args
) {
139 if (!args
.Length() || !args
[0]->IsBoolean())
140 return v8::Undefined();
142 content::RenderThread::Get()->Send(new ChromeViewHostMsg_EnableSpdy(
143 args
[0]->BooleanValue()));
144 return v8::Undefined();
147 static v8::Handle
<v8::Value
> GetCounter(const v8::Arguments
& args
) {
148 if (!args
.Length() || !args
[0]->IsString() || !base::StatsTable::current())
149 return v8::Undefined();
151 // Extract the name argument
155 args
[0]->ToString()->WriteAscii(&name
[2], 0, sizeof(name
) - 3);
157 int counter
= base::StatsTable::current()->GetCounterValue(name
);
158 return v8::Integer::New(counter
);
161 static v8::Handle
<v8::Value
> IsSingleProcess(const v8::Arguments
& args
) {
162 return v8::Boolean::New(
163 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
));
166 static v8::Handle
<v8::Value
> HiResTime(const v8::Arguments
& args
) {
167 return v8::Number::New(
168 static_cast<double>(base::TimeTicks::HighResNow().ToInternalValue()));
172 v8::Extension
* BenchmarkingExtension::Get() {
173 return new BenchmarkingWrapper();
176 } // namespace extensions_v8