1 // Copyright (c) 2012 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 "base/command_line.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/test/perf_time_logger.h"
8 #include "ppapi/c/ppp_messaging.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/proxy/ppapi_proxy_test.h"
11 #include "ppapi/proxy/serialized_var.h"
12 #include "ppapi/shared_impl/ppapi_globals.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/var.h"
15 #include "ppapi/shared_impl/var_tracker.h"
21 base::WaitableEvent
handle_message_called(false, false);
23 void HandleMessage(PP_Instance
/* instance */, PP_Var message_data
) {
24 ppapi::ProxyAutoLock lock
;
25 StringVar
* string_var
= StringVar::FromPPVar(message_data
);
27 // Retrieve the string to make sure the proxy can't "optimize away" sending
28 // the actual contents of the string (e.g., by doing a lazy retrieve or
29 // something). Note that this test is for performance only, and assumes
30 // other tests check for correctness.
31 std::string s
= string_var
->value();
32 // Do something simple with the string so the compiler won't complain.
35 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(message_data
);
36 handle_message_called
.Signal();
39 PPP_Messaging ppp_messaging_mock
= {
43 class PppMessagingPerfTest
: public TwoWayTest
{
45 PppMessagingPerfTest() : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE
) {
46 plugin().RegisterTestInterface(PPP_MESSAGING_INTERFACE
,
53 // Tests the performance of sending strings through the proxy.
54 TEST_F(PppMessagingPerfTest
, StringPerformance
) {
55 const PP_Instance kTestInstance
= pp_instance();
57 int string_count
= 1000;
58 int max_string_size
= 1000000;
59 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
61 if (command_line
->HasSwitch("seed")) {
62 base::StringToInt(command_line
->GetSwitchValueASCII("seed"),
65 if (command_line
->HasSwitch("string_count")) {
66 base::StringToInt(command_line
->GetSwitchValueASCII("string_count"),
69 if (command_line
->HasSwitch("max_string_size")) {
70 base::StringToInt(command_line
->GetSwitchValueASCII("max_string_size"),
75 base::PerfTimeLogger
logger("PppMessagingPerfTest.StringPerformance");
76 for (int i
= 0; i
< string_count
; ++i
) {
77 const std::string
test_string(rand() % max_string_size
, 'a');
78 PP_Var host_string
= StringVar::StringToPPVar(test_string
);
79 // We don't have a host-side PPP_Messaging interface; just send the message
80 // directly like the proxy does.
81 host().host_dispatcher()->Send(new PpapiMsg_PPPMessaging_HandleMessage(
82 ppapi::API_ID_PPP_MESSAGING
,
84 ppapi::proxy::SerializedVarSendInput(host().host_dispatcher(),
86 handle_message_called
.Wait();
87 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(host_string
);