• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/cef_v8.h"
6 #include "tests/cefclient/renderer/performance_test.h"
7 #include "tests/cefclient/renderer/performance_test_setup.h"
8 
9 namespace client {
10 namespace performance_test {
11 
12 namespace {
13 
14 // Test function implementations.
15 
PERF_TEST_FUNC(V8NullCreate)16 PERF_TEST_FUNC(V8NullCreate) {
17   PERF_ITERATIONS_START()
18   CefRefPtr<CefV8Value> value = CefV8Value::CreateNull();
19   PERF_ITERATIONS_END()
20 }
21 
PERF_TEST_FUNC(V8BoolCreate)22 PERF_TEST_FUNC(V8BoolCreate) {
23   PERF_ITERATIONS_START()
24   CefRefPtr<CefV8Value> value = CefV8Value::CreateBool(true);
25   PERF_ITERATIONS_END()
26 }
27 
PERF_TEST_FUNC(V8IntCreate)28 PERF_TEST_FUNC(V8IntCreate) {
29   PERF_ITERATIONS_START()
30   CefRefPtr<CefV8Value> value = CefV8Value::CreateInt(-5);
31   PERF_ITERATIONS_END()
32 }
33 
PERF_TEST_FUNC(V8UIntCreate)34 PERF_TEST_FUNC(V8UIntCreate) {
35   PERF_ITERATIONS_START()
36   CefRefPtr<CefV8Value> value = CefV8Value::CreateUInt(10);
37   PERF_ITERATIONS_END()
38 }
39 
PERF_TEST_FUNC(V8DoubleCreate)40 PERF_TEST_FUNC(V8DoubleCreate) {
41   PERF_ITERATIONS_START()
42   CefRefPtr<CefV8Value> value = CefV8Value::CreateDouble(12.432);
43   PERF_ITERATIONS_END()
44 }
45 
PERF_TEST_FUNC(V8DateCreate)46 PERF_TEST_FUNC(V8DateCreate) {
47   static cef_time_t time = {2012, 1, 0, 1};
48 
49   PERF_ITERATIONS_START()
50   CefRefPtr<CefV8Value> value = CefV8Value::CreateDate(time);
51   PERF_ITERATIONS_END()
52 }
53 
PERF_TEST_FUNC(V8StringCreate)54 PERF_TEST_FUNC(V8StringCreate) {
55   CefString str = "test string";
56 
57   PERF_ITERATIONS_START()
58   CefRefPtr<CefV8Value> value = CefV8Value::CreateString(str);
59   PERF_ITERATIONS_END()
60 }
61 
PERF_TEST_FUNC(V8ArrayCreate)62 PERF_TEST_FUNC(V8ArrayCreate) {
63   PERF_ITERATIONS_START()
64   CefRefPtr<CefV8Value> value = CefV8Value::CreateArray(1);
65   PERF_ITERATIONS_END()
66 }
67 
PERF_TEST_FUNC(V8ArraySetValue)68 PERF_TEST_FUNC(V8ArraySetValue) {
69   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
70   CefRefPtr<CefV8Value> array = CefV8Value::CreateArray(1);
71   array->SetValue(0, val);
72 
73   PERF_ITERATIONS_START()
74   array->SetValue(0, val);
75   PERF_ITERATIONS_END()
76 }
77 
PERF_TEST_FUNC(V8ArrayGetValue)78 PERF_TEST_FUNC(V8ArrayGetValue) {
79   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
80   CefRefPtr<CefV8Value> array = CefV8Value::CreateArray(1);
81   array->SetValue(0, val);
82 
83   PERF_ITERATIONS_START()
84   CefRefPtr<CefV8Value> ret = array->GetValue(0);
85   PERF_ITERATIONS_END()
86 }
87 
PERF_TEST_FUNC(V8FunctionCreate)88 PERF_TEST_FUNC(V8FunctionCreate) {
89   class Handler : public CefV8Handler {
90    public:
91     Handler() {}
92     virtual bool Execute(const CefString& name,
93                          CefRefPtr<CefV8Value> object,
94                          const CefV8ValueList& arguments,
95                          CefRefPtr<CefV8Value>& retval,
96                          CefString& exception) override {
97       return false;
98     }
99     IMPLEMENT_REFCOUNTING(Handler);
100   };
101 
102   CefString name = "name";
103   CefRefPtr<CefV8Handler> handler = new Handler();
104 
105   PERF_ITERATIONS_START()
106   CefRefPtr<CefV8Value> value = CefV8Value::CreateFunction(name, handler);
107   PERF_ITERATIONS_END()
108 }
109 
PERF_TEST_FUNC(V8FunctionExecute)110 PERF_TEST_FUNC(V8FunctionExecute) {
111   class Handler : public CefV8Handler {
112    public:
113     Handler() {}
114     virtual bool Execute(const CefString& name,
115                          CefRefPtr<CefV8Value> object,
116                          const CefV8ValueList& arguments,
117                          CefRefPtr<CefV8Value>& retval,
118                          CefString& exception) override {
119       return true;
120     }
121     IMPLEMENT_REFCOUNTING(Handler);
122   };
123 
124   CefString name = "name";
125   CefRefPtr<CefV8Handler> handler = new Handler();
126   CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction(name, handler);
127   CefRefPtr<CefV8Value> obj = CefV8Context::GetCurrentContext()->GetGlobal();
128   CefV8ValueList args;
129 
130   PERF_ITERATIONS_START()
131   func->ExecuteFunction(obj, args);
132   PERF_ITERATIONS_END()
133 }
134 
PERF_TEST_FUNC(V8FunctionExecuteWithContext)135 PERF_TEST_FUNC(V8FunctionExecuteWithContext) {
136   class Handler : public CefV8Handler {
137    public:
138     Handler() {}
139     virtual bool Execute(const CefString& name,
140                          CefRefPtr<CefV8Value> object,
141                          const CefV8ValueList& arguments,
142                          CefRefPtr<CefV8Value>& retval,
143                          CefString& exception) override {
144       return true;
145     }
146     IMPLEMENT_REFCOUNTING(Handler);
147   };
148 
149   CefString name = "name";
150   CefRefPtr<CefV8Handler> handler = new Handler();
151   CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction(name, handler);
152   CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
153   CefRefPtr<CefV8Value> obj = context->GetGlobal();
154   CefV8ValueList args;
155 
156   PERF_ITERATIONS_START()
157   func->ExecuteFunctionWithContext(context, obj, args);
158   PERF_ITERATIONS_END()
159 }
160 
PERF_TEST_FUNC(V8ObjectCreate)161 PERF_TEST_FUNC(V8ObjectCreate) {
162   PERF_ITERATIONS_START()
163   CefRefPtr<CefV8Value> value = CefV8Value::CreateObject(nullptr, nullptr);
164   PERF_ITERATIONS_END()
165 }
166 
PERF_TEST_FUNC(V8ObjectCreateWithAccessor)167 PERF_TEST_FUNC(V8ObjectCreateWithAccessor) {
168   class Accessor : public CefV8Accessor {
169    public:
170     Accessor() {}
171     virtual bool Get(const CefString& name,
172                      const CefRefPtr<CefV8Value> object,
173                      CefRefPtr<CefV8Value>& retval,
174                      CefString& exception) override {
175       return true;
176     }
177     virtual bool Set(const CefString& name,
178                      const CefRefPtr<CefV8Value> object,
179                      const CefRefPtr<CefV8Value> value,
180                      CefString& exception) override {
181       return true;
182     }
183     IMPLEMENT_REFCOUNTING(Accessor);
184   };
185 
186   CefRefPtr<CefV8Accessor> accessor = new Accessor();
187 
188   PERF_ITERATIONS_START()
189   CefRefPtr<CefV8Value> value = CefV8Value::CreateObject(accessor, nullptr);
190   PERF_ITERATIONS_END()
191 }
192 
PERF_TEST_FUNC(V8ObjectCreateWithInterceptor)193 PERF_TEST_FUNC(V8ObjectCreateWithInterceptor) {
194   class Interceptor : public CefV8Interceptor {
195    public:
196     Interceptor() {}
197     virtual bool Get(const CefString& name,
198                      const CefRefPtr<CefV8Value> object,
199                      CefRefPtr<CefV8Value>& retval,
200                      CefString& exception) override {
201       return true;
202     }
203     virtual bool Get(int index,
204                      const CefRefPtr<CefV8Value> object,
205                      CefRefPtr<CefV8Value>& retval,
206                      CefString& exception) override {
207       return true;
208     }
209     virtual bool Set(const CefString& name,
210                      const CefRefPtr<CefV8Value> object,
211                      const CefRefPtr<CefV8Value> value,
212                      CefString& exception) override {
213       return true;
214     }
215     virtual bool Set(int index,
216                      const CefRefPtr<CefV8Value> object,
217                      const CefRefPtr<CefV8Value> value,
218                      CefString& exception) override {
219       return true;
220     }
221     IMPLEMENT_REFCOUNTING(Interceptor);
222   };
223 
224   CefRefPtr<CefV8Interceptor> interceptor = new Interceptor();
225 
226   PERF_ITERATIONS_START()
227   CefRefPtr<CefV8Value> value = CefV8Value::CreateObject(nullptr, interceptor);
228   PERF_ITERATIONS_END()
229 }
230 
PERF_TEST_FUNC(V8ObjectSetValue)231 PERF_TEST_FUNC(V8ObjectSetValue) {
232   CefString name = "name";
233   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
234   CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(nullptr, nullptr);
235   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
236 
237   PERF_ITERATIONS_START()
238   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
239   PERF_ITERATIONS_END()
240 }
241 
PERF_TEST_FUNC(V8ObjectGetValue)242 PERF_TEST_FUNC(V8ObjectGetValue) {
243   CefString name = "name";
244   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
245   CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(nullptr, nullptr);
246   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
247 
248   PERF_ITERATIONS_START()
249   CefRefPtr<CefV8Value> ret = obj->GetValue(name);
250   PERF_ITERATIONS_END()
251 }
252 
PERF_TEST_FUNC(V8ObjectSetValueWithAccessor)253 PERF_TEST_FUNC(V8ObjectSetValueWithAccessor) {
254   class Accessor : public CefV8Accessor {
255    public:
256     Accessor() {}
257     virtual bool Get(const CefString& name,
258                      const CefRefPtr<CefV8Value> object,
259                      CefRefPtr<CefV8Value>& retval,
260                      CefString& exception) override {
261       return true;
262     }
263     virtual bool Set(const CefString& name,
264                      const CefRefPtr<CefV8Value> object,
265                      const CefRefPtr<CefV8Value> value,
266                      CefString& exception) override {
267       val_ = value;
268       return true;
269     }
270     CefRefPtr<CefV8Value> val_;
271     IMPLEMENT_REFCOUNTING(Accessor);
272   };
273 
274   CefRefPtr<CefV8Accessor> accessor = new Accessor();
275 
276   CefString name = "name";
277   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
278   CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(accessor, nullptr);
279   obj->SetValue(name, V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
280   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
281 
282   PERF_ITERATIONS_START()
283   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
284   PERF_ITERATIONS_END()
285 }
286 
PERF_TEST_FUNC(V8ObjectGetValueWithAccessor)287 PERF_TEST_FUNC(V8ObjectGetValueWithAccessor) {
288   class Accessor : public CefV8Accessor {
289    public:
290     Accessor() : val_(CefV8Value::CreateBool(true)) {}
291     virtual bool Get(const CefString& name,
292                      const CefRefPtr<CefV8Value> object,
293                      CefRefPtr<CefV8Value>& retval,
294                      CefString& exception) override {
295       retval = val_;
296       return true;
297     }
298     virtual bool Set(const CefString& name,
299                      const CefRefPtr<CefV8Value> object,
300                      const CefRefPtr<CefV8Value> value,
301                      CefString& exception) override {
302       return true;
303     }
304     CefRefPtr<CefV8Value> val_;
305     IMPLEMENT_REFCOUNTING(Accessor);
306   };
307 
308   CefRefPtr<CefV8Accessor> accessor = new Accessor();
309 
310   CefString name = "name";
311   CefRefPtr<CefV8Value> val = CefV8Value::CreateBool(true);
312   CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(accessor, nullptr);
313   obj->SetValue(name, V8_ACCESS_CONTROL_DEFAULT, V8_PROPERTY_ATTRIBUTE_NONE);
314   obj->SetValue(name, val, V8_PROPERTY_ATTRIBUTE_NONE);
315 
316   PERF_ITERATIONS_START()
317   CefRefPtr<CefV8Value> ret = obj->GetValue(name);
318   PERF_ITERATIONS_END()
319 }
320 
PERF_TEST_FUNC(V8ArrayBufferCreate)321 PERF_TEST_FUNC(V8ArrayBufferCreate) {
322   class ReleaseCallback : public CefV8ArrayBufferReleaseCallback {
323    public:
324     void ReleaseBuffer(void* buffer) override { std::free(buffer); }
325     IMPLEMENT_REFCOUNTING(ReleaseCallback);
326   };
327 
328   size_t len = 1;
329   size_t byte_len = len * sizeof(float);
330   CefRefPtr<CefV8ArrayBufferReleaseCallback> callback = new ReleaseCallback();
331 
332   PERF_ITERATIONS_START()
333   float* buffer = (float*)std::malloc(byte_len);
334   CefRefPtr<CefV8Value> ret =
335       CefV8Value::CreateArrayBuffer(buffer, byte_len, callback);
336   PERF_ITERATIONS_END()
337 }
338 
PERF_TEST_FUNC(V8ContextEnterExit)339 PERF_TEST_FUNC(V8ContextEnterExit) {
340   CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
341 
342   PERF_ITERATIONS_START()
343   context->Enter();
344   context->Exit();
345   PERF_ITERATIONS_END()
346 }
347 
PERF_TEST_FUNC(V8ContextEval)348 PERF_TEST_FUNC(V8ContextEval) {
349   CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
350   CefString jsCode = "var i = 0;";
351   CefRefPtr<CefV8Value> retval;
352   CefRefPtr<CefV8Exception> exception;
353 
354   PERF_ITERATIONS_START()
355   context->Eval(jsCode, CefString(), 0, retval, exception);
356   PERF_ITERATIONS_END()
357 }
358 
359 }  // namespace
360 
361 // Test function entries.
362 
363 const PerfTestEntry kPerfTests[] = {
364     PERF_TEST_ENTRY(V8NullCreate),
365     PERF_TEST_ENTRY(V8BoolCreate),
366     PERF_TEST_ENTRY(V8IntCreate),
367     PERF_TEST_ENTRY(V8UIntCreate),
368     PERF_TEST_ENTRY(V8DoubleCreate),
369     PERF_TEST_ENTRY(V8DateCreate),
370     PERF_TEST_ENTRY(V8StringCreate),
371     PERF_TEST_ENTRY(V8ArrayCreate),
372     PERF_TEST_ENTRY(V8ArraySetValue),
373     PERF_TEST_ENTRY(V8ArrayGetValue),
374     PERF_TEST_ENTRY(V8FunctionCreate),
375     PERF_TEST_ENTRY(V8FunctionExecute),
376     PERF_TEST_ENTRY(V8FunctionExecuteWithContext),
377     PERF_TEST_ENTRY(V8ObjectCreate),
378     PERF_TEST_ENTRY(V8ObjectCreateWithAccessor),
379     PERF_TEST_ENTRY(V8ObjectCreateWithInterceptor),
380     PERF_TEST_ENTRY(V8ObjectSetValue),
381     PERF_TEST_ENTRY(V8ObjectGetValue),
382     PERF_TEST_ENTRY(V8ObjectSetValueWithAccessor),
383     PERF_TEST_ENTRY(V8ObjectGetValueWithAccessor),
384     PERF_TEST_ENTRY(V8ArrayBufferCreate),
385     PERF_TEST_ENTRY(V8ContextEnterExit),
386     PERF_TEST_ENTRY(V8ContextEval),
387 };
388 
389 const int kPerfTestsCount = (sizeof(kPerfTests) / sizeof(kPerfTests[0]));
390 
391 }  // namespace performance_test
392 }  // namespace client
393