OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "Cache.h" | 6 #include "Cache.h" |
| 7 #include "base/stats_table.h" |
7 #include "webkit/extensions/v8/benchmarking_extension.h" | 8 #include "webkit/extensions/v8/benchmarking_extension.h" |
8 #include "webkit/glue/webkit_glue.h" | 9 #include "webkit/glue/webkit_glue.h" |
9 | 10 |
10 namespace extensions_v8 { | 11 namespace extensions_v8 { |
11 | 12 |
12 const char* kBenchmarkingExtensionName = "v8/Benchmarking"; | 13 const char* kBenchmarkingExtensionName = "v8/Benchmarking"; |
13 | 14 |
14 class BenchmarkingWrapper : public v8::Extension { | 15 class BenchmarkingWrapper : public v8::Extension { |
15 public: | 16 public: |
16 BenchmarkingWrapper() : | 17 BenchmarkingWrapper() : |
17 v8::Extension(kBenchmarkingExtensionName, | 18 v8::Extension(kBenchmarkingExtensionName, |
18 "if (typeof(chromium) == 'undefined') {" | 19 "if (typeof(chromium) == 'undefined') {" |
19 " chromium = {};" | 20 " chromium = {};" |
20 "};" | 21 "};" |
21 "if (typeof(chromium.benchmarking) == 'undefined') {" | 22 "if (typeof(chromium.benchmarking) == 'undefined') {" |
22 " chromium.benchmarking = {};" | 23 " chromium.benchmarking = {};" |
23 "};" | 24 "};" |
24 "chromium.benchmarking.clearCache = function() {" | 25 "chromium.benchmarking.clearCache = function() {" |
25 " native function ClearCache();" | 26 " native function ClearCache();" |
26 " ClearCache();" | 27 " ClearCache();" |
27 "};" | 28 "};" |
28 "chromium.benchmarking.closeConnections = function() {" | 29 "chromium.benchmarking.closeConnections = function() {" |
29 " native function CloseConnections();" | 30 " native function CloseConnections();" |
30 " CloseConnections();" | 31 " CloseConnections();" |
31 "};") {} | 32 "};" |
| 33 "chromium.benchmarking.counter = function(name) {" |
| 34 " native function GetCounter();" |
| 35 " return GetCounter(name);" |
| 36 "};" |
| 37 ) {} |
32 | 38 |
33 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 39 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
34 v8::Handle<v8::String> name) { | 40 v8::Handle<v8::String> name) { |
35 if (name->Equals(v8::String::New("CloseConnections"))) { | 41 if (name->Equals(v8::String::New("CloseConnections"))) { |
36 return v8::FunctionTemplate::New(CloseConnections); | 42 return v8::FunctionTemplate::New(CloseConnections); |
37 } else if (name->Equals(v8::String::New("ClearCache"))) { | 43 } else if (name->Equals(v8::String::New("ClearCache"))) { |
38 return v8::FunctionTemplate::New(ClearCache); | 44 return v8::FunctionTemplate::New(ClearCache); |
| 45 } else if (name->Equals(v8::String::New("GetCounter"))) { |
| 46 return v8::FunctionTemplate::New(GetCounter); |
39 } | 47 } |
40 return v8::Handle<v8::FunctionTemplate>(); | 48 return v8::Handle<v8::FunctionTemplate>(); |
41 } | 49 } |
42 | 50 |
43 static v8::Handle<v8::Value> CloseConnections(const v8::Arguments& args) { | 51 static v8::Handle<v8::Value> CloseConnections(const v8::Arguments& args) { |
44 webkit_glue::CloseIdleConnections(); | 52 webkit_glue::CloseIdleConnections(); |
45 return v8::Undefined(); | 53 return v8::Undefined(); |
46 } | 54 } |
47 | 55 |
48 static v8::Handle<v8::Value> ClearCache(const v8::Arguments& args) { | 56 static v8::Handle<v8::Value> ClearCache(const v8::Arguments& args) { |
49 // TODO(mbelshe): should be enable/disable? | 57 // TODO(mbelshe): should be enable/disable? |
50 webkit_glue::SetCacheMode(false); | 58 webkit_glue::SetCacheMode(false); |
51 | 59 |
52 // Disabling and re-enabling the cache forces it to flush. | 60 // Disabling and re-enabling the cache forces it to flush. |
53 WebCore::cache()->setDisabled(true); | 61 WebCore::cache()->setDisabled(true); |
54 WebCore::cache()->setDisabled(false); | 62 WebCore::cache()->setDisabled(false); |
55 return v8::Undefined(); | 63 return v8::Undefined(); |
56 } | 64 } |
| 65 |
| 66 static v8::Handle<v8::Value> GetCounter(const v8::Arguments& args) { |
| 67 if (!args.Length() || !args[0]->IsString()) |
| 68 return v8::Undefined(); |
| 69 |
| 70 // Extract the name argument |
| 71 char name[256]; |
| 72 name[0] = 'c'; |
| 73 name[1] = ':'; |
| 74 args[0]->ToString()->WriteAscii(&name[2], 0, sizeof(name) - 3); |
| 75 |
| 76 int counter = StatsTable::current()->GetCounterValue(name); |
| 77 return v8::Integer::New(counter); |
| 78 } |
57 }; | 79 }; |
58 | 80 |
59 v8::Extension* BenchmarkingExtension::Get() { | 81 v8::Extension* BenchmarkingExtension::Get() { |
60 return new BenchmarkingWrapper(); | 82 return new BenchmarkingWrapper(); |
61 } | 83 } |
62 | 84 |
63 } // namespace extensions_v8 | 85 } // namespace extensions_v8 |
OLD | NEW |