1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/cefclient/renderer/performance_test.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "include/base/cef_logging.h"
11 #include "include/wrapper/cef_stream_resource_handler.h"
12 #include "tests/cefclient/renderer/performance_test_setup.h"
13
14 namespace client {
15 namespace performance_test {
16
17 // Use more interations for a Release build.
18 #if DCHECK_IS_ON()
19 const int kDefaultIterations = 100000;
20 #else
21 const int kDefaultIterations = 10000;
22 #endif
23
24 namespace {
25
26 const char kGetPerfTests[] = "GetPerfTests";
27 const char kRunPerfTest[] = "RunPerfTest";
28 const char kPerfTestReturnValue[] = "PerfTestReturnValue";
29
30 class V8Handler : public CefV8Handler {
31 public:
V8Handler()32 V8Handler() {}
33
Execute(const CefString & name,CefRefPtr<CefV8Value> object,const CefV8ValueList & arguments,CefRefPtr<CefV8Value> & retval,CefString & exception)34 virtual bool Execute(const CefString& name,
35 CefRefPtr<CefV8Value> object,
36 const CefV8ValueList& arguments,
37 CefRefPtr<CefV8Value>& retval,
38 CefString& exception) override {
39 if (name == kRunPerfTest) {
40 if (arguments.size() == 1 && arguments[0]->IsString()) {
41 // Run the specified perf test.
42 bool found = false;
43
44 std::string test = arguments[0]->GetStringValue();
45 for (int i = 0; i < kPerfTestsCount; ++i) {
46 if (test == kPerfTests[i].name) {
47 // Execute the test.
48 int64 delta = kPerfTests[i].test(kPerfTests[i].iterations);
49
50 retval = CefV8Value::CreateInt(delta);
51 found = true;
52 break;
53 }
54 }
55
56 if (!found) {
57 std::string msg = "Unknown test: ";
58 msg.append(test);
59 exception = msg;
60 }
61 } else {
62 exception = "Invalid function parameters";
63 }
64 } else if (name == kGetPerfTests) {
65 // Retrieve the list of perf tests.
66 retval = CefV8Value::CreateArray(kPerfTestsCount);
67 for (int i = 0; i < kPerfTestsCount; ++i) {
68 CefRefPtr<CefV8Value> val = CefV8Value::CreateArray(2);
69 val->SetValue(0, CefV8Value::CreateString(kPerfTests[i].name));
70 val->SetValue(1, CefV8Value::CreateUInt(kPerfTests[i].iterations));
71 retval->SetValue(i, val);
72 }
73 } else if (name == kPerfTestReturnValue) {
74 if (arguments.size() == 0) {
75 retval = CefV8Value::CreateInt(1);
76 } else if (arguments.size() == 1 && arguments[0]->IsInt()) {
77 int32 type = arguments[0]->GetIntValue();
78 CefTime date;
79 switch (type) {
80 case 0:
81 retval = CefV8Value::CreateUndefined();
82 break;
83 case 1:
84 retval = CefV8Value::CreateNull();
85 break;
86 case 2:
87 retval = CefV8Value::CreateBool(true);
88 break;
89 case 3:
90 retval = CefV8Value::CreateInt(1);
91 break;
92 case 4:
93 retval = CefV8Value::CreateUInt(1);
94 break;
95 case 5:
96 retval = CefV8Value::CreateDouble(1.234);
97 break;
98 case 6:
99 date.Now();
100 retval = CefV8Value::CreateDate(date);
101 break;
102 case 7:
103 retval = CefV8Value::CreateString("Hello, world!");
104 break;
105 case 8:
106 retval = CefV8Value::CreateObject(nullptr, nullptr);
107 break;
108 case 9:
109 retval = CefV8Value::CreateArray(8);
110 break;
111 case 10:
112 // retval = CefV8Value::CreateFunction(...);
113 exception = "Not implemented";
114 break;
115 default:
116 exception = "Not supported";
117 }
118 }
119 }
120
121 return true;
122 }
123
124 private:
125 IMPLEMENT_REFCOUNTING(V8Handler);
126 };
127
128 // Handle bindings in the render process.
129 class RenderDelegate : public ClientAppRenderer::Delegate {
130 public:
RenderDelegate()131 RenderDelegate() {}
132
OnContextCreated(CefRefPtr<ClientAppRenderer> app,CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,CefRefPtr<CefV8Context> context)133 virtual void OnContextCreated(CefRefPtr<ClientAppRenderer> app,
134 CefRefPtr<CefBrowser> browser,
135 CefRefPtr<CefFrame> frame,
136 CefRefPtr<CefV8Context> context) override {
137 CefRefPtr<CefV8Value> object = context->GetGlobal();
138
139 CefRefPtr<CefV8Handler> handler = new V8Handler();
140
141 // Bind test functions.
142 object->SetValue(kGetPerfTests,
143 CefV8Value::CreateFunction(kGetPerfTests, handler),
144 V8_PROPERTY_ATTRIBUTE_READONLY);
145 object->SetValue(kRunPerfTest,
146 CefV8Value::CreateFunction(kRunPerfTest, handler),
147 V8_PROPERTY_ATTRIBUTE_READONLY);
148 object->SetValue(kPerfTestReturnValue,
149 CefV8Value::CreateFunction(kPerfTestReturnValue, handler),
150 V8_PROPERTY_ATTRIBUTE_READONLY);
151 }
152
153 private:
154 IMPLEMENT_REFCOUNTING(RenderDelegate);
155 };
156
157 } // namespace
158
CreateDelegates(ClientAppRenderer::DelegateSet & delegates)159 void CreateDelegates(ClientAppRenderer::DelegateSet& delegates) {
160 delegates.insert(new RenderDelegate);
161 }
162
163 } // namespace performance_test
164 } // namespace client
165