2012-10-26 22:42:41 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/renderer/performance_test.h"
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
|
2016-09-01 13:24:30 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2012-10-26 22:42:41 +02:00
|
|
|
#include "include/wrapper/cef_stream_resource_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/renderer/performance_test_setup.h"
|
2012-10-26 22:42:41 +02:00
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
namespace client::performance_test {
|
2012-10-26 22:42:41 +02:00
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
// Use more interations for a Release build.
|
2016-09-01 13:24:30 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2013-08-14 23:45:22 +02:00
|
|
|
const int kDefaultIterations = 100000;
|
2012-10-29 22:46:02 +01:00
|
|
|
#else
|
2013-08-14 23:45:22 +02:00
|
|
|
const int kDefaultIterations = 10000;
|
2012-10-29 22:46:02 +01:00
|
|
|
#endif
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kGetPerfTests[] = "GetPerfTests";
|
|
|
|
const char kRunPerfTest[] = "RunPerfTest";
|
2013-06-27 00:18:30 +02:00
|
|
|
const char kPerfTestReturnValue[] = "PerfTestReturnValue";
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
class V8Handler : public CefV8Handler {
|
|
|
|
public:
|
2024-01-20 03:22:56 +01:00
|
|
|
V8Handler() = default;
|
2012-10-26 22:42:41 +02:00
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
bool Execute(const CefString& name,
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
|
|
|
CefString& exception) override {
|
2012-10-26 22:42:41 +02:00
|
|
|
if (name == kRunPerfTest) {
|
|
|
|
if (arguments.size() == 1 && arguments[0]->IsString()) {
|
|
|
|
// Run the specified perf test.
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
std::string test = arguments[0]->GetStringValue();
|
2013-08-14 23:45:22 +02:00
|
|
|
for (int i = 0; i < kPerfTestsCount; ++i) {
|
2012-10-26 22:42:41 +02:00
|
|
|
if (test == kPerfTests[i].name) {
|
|
|
|
// Execute the test.
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t delta = kPerfTests[i].test(kPerfTests[i].iterations);
|
2012-10-26 22:42:41 +02:00
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
retval = CefV8Value::CreateInt(static_cast<int32_t>(delta));
|
2012-10-26 22:42:41 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
std::string msg = "Unknown test: ";
|
|
|
|
msg.append(test);
|
|
|
|
exception = msg;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exception = "Invalid function parameters";
|
|
|
|
}
|
|
|
|
} else if (name == kGetPerfTests) {
|
|
|
|
// Retrieve the list of perf tests.
|
|
|
|
retval = CefV8Value::CreateArray(kPerfTestsCount);
|
2013-08-14 23:45:22 +02:00
|
|
|
for (int i = 0; i < kPerfTestsCount; ++i) {
|
2012-10-26 22:42:41 +02:00
|
|
|
CefRefPtr<CefV8Value> val = CefV8Value::CreateArray(2);
|
|
|
|
val->SetValue(0, CefV8Value::CreateString(kPerfTests[i].name));
|
|
|
|
val->SetValue(1, CefV8Value::CreateUInt(kPerfTests[i].iterations));
|
|
|
|
retval->SetValue(i, val);
|
|
|
|
}
|
2013-06-27 00:18:30 +02:00
|
|
|
} else if (name == kPerfTestReturnValue) {
|
|
|
|
if (arguments.size() == 0) {
|
|
|
|
retval = CefV8Value::CreateInt(1);
|
|
|
|
} else if (arguments.size() == 1 && arguments[0]->IsInt()) {
|
2023-06-01 16:06:15 +02:00
|
|
|
int32_t type = arguments[0]->GetIntValue();
|
2013-06-27 00:18:30 +02:00
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
retval = CefV8Value::CreateUndefined();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
retval = CefV8Value::CreateNull();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
retval = CefV8Value::CreateBool(true);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
retval = CefV8Value::CreateInt(1);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
retval = CefV8Value::CreateUInt(1);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
retval = CefV8Value::CreateDouble(1.234);
|
|
|
|
break;
|
|
|
|
case 6:
|
2022-05-31 22:28:43 +02:00
|
|
|
retval = CefV8Value::CreateDate(CefBaseTime::Now());
|
2013-06-27 00:18:30 +02:00
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
retval = CefV8Value::CreateString("Hello, world!");
|
|
|
|
break;
|
|
|
|
case 8:
|
2020-01-15 15:28:12 +01:00
|
|
|
retval = CefV8Value::CreateObject(nullptr, nullptr);
|
2013-06-27 00:18:30 +02:00
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
retval = CefV8Value::CreateArray(8);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
// retval = CefV8Value::CreateFunction(...);
|
|
|
|
exception = "Not implemented";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exception = "Not supported";
|
|
|
|
}
|
|
|
|
}
|
2012-10-26 22:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(V8Handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle bindings in the render process.
|
2015-01-31 05:41:36 +01:00
|
|
|
class RenderDelegate : public ClientAppRenderer::Delegate {
|
2012-10-26 22:42:41 +02:00
|
|
|
public:
|
2024-01-20 03:22:56 +01:00
|
|
|
RenderDelegate() = default;
|
2012-10-26 22:42:41 +02:00
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
void OnContextCreated(CefRefPtr<ClientAppRenderer> app,
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefV8Context> context) override {
|
2012-10-26 22:42:41 +02:00
|
|
|
CefRefPtr<CefV8Value> object = context->GetGlobal();
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Handler> handler = new V8Handler();
|
|
|
|
|
|
|
|
// Bind test functions.
|
|
|
|
object->SetValue(kGetPerfTests,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefV8Value::CreateFunction(kGetPerfTests, handler),
|
|
|
|
V8_PROPERTY_ATTRIBUTE_READONLY);
|
2012-10-26 22:42:41 +02:00
|
|
|
object->SetValue(kRunPerfTest,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefV8Value::CreateFunction(kRunPerfTest, handler),
|
|
|
|
V8_PROPERTY_ATTRIBUTE_READONLY);
|
2013-06-27 00:18:30 +02:00
|
|
|
object->SetValue(kPerfTestReturnValue,
|
2017-05-17 11:29:28 +02:00
|
|
|
CefV8Value::CreateFunction(kPerfTestReturnValue, handler),
|
|
|
|
V8_PROPERTY_ATTRIBUTE_READONLY);
|
2012-10-26 22:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(RenderDelegate);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
void CreateDelegates(ClientAppRenderer::DelegateSet& delegates) {
|
2012-10-26 22:42:41 +02:00
|
|
|
delegates.insert(new RenderDelegate);
|
|
|
|
}
|
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
} // namespace client::performance_test
|