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.
4
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_proxy_test.h"
10#include "ppapi/proxy/serialized_var.h"
11#include "ppapi/shared_impl/ppapi_globals.h"
12#include "ppapi/shared_impl/var.h"
13#include "ppapi/shared_impl/var_tracker.h"
14
15namespace ppapi {
16namespace proxy {
17namespace {
18
19base::WaitableEvent handle_message_called(false, false);
20
21void HandleMessage(PP_Instance /* instance */, PP_Var message_data) {
22  StringVar* string_var = StringVar::FromPPVar(message_data);
23  DCHECK(string_var);
24  // Retrieve the string to make sure the proxy can't "optimize away" sending
25  // the actual contents of the string (e.g., by doing a lazy retrieve or
26  // something). Note that this test is for performance only, and assumes
27  // other tests check for correctness.
28  std::string s = string_var->value();
29  // Do something simple with the string so the compiler won't complain.
30  if (s.length() > 0)
31    s[0] = 'a';
32  PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(message_data);
33  handle_message_called.Signal();
34}
35
36PPP_Messaging ppp_messaging_mock = {
37  &HandleMessage
38};
39
40class PppMessagingPerfTest : public TwoWayTest {
41 public:
42  PppMessagingPerfTest() : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) {
43    plugin().RegisterTestInterface(PPP_MESSAGING_INTERFACE,
44                                   &ppp_messaging_mock);
45  }
46};
47
48}  // namespace
49
50// Tests the performance of sending strings through the proxy.
51TEST_F(PppMessagingPerfTest, StringPerformance) {
52  // Grab the host-side proxy of ppp_messaging.
53  const PPP_Messaging* ppp_messaging = static_cast<const PPP_Messaging*>(
54      host().host_dispatcher()->GetProxiedInterface(
55          PPP_MESSAGING_INTERFACE));
56  const PP_Instance kTestInstance = pp_instance();
57  int seed = 123;
58  int string_count = 1000;
59  int max_string_size = 1000000;
60  CommandLine* command_line = CommandLine::ForCurrentProcess();
61  if (command_line) {
62    if (command_line->HasSwitch("seed")) {
63      base::StringToInt(command_line->GetSwitchValueASCII("seed"),
64                        &seed);
65    }
66    if (command_line->HasSwitch("string_count")) {
67      base::StringToInt(command_line->GetSwitchValueASCII("string_count"),
68                        &string_count);
69    }
70    if (command_line->HasSwitch("max_string_size")) {
71      base::StringToInt(command_line->GetSwitchValueASCII("max_string_size"),
72                        &max_string_size);
73    }
74  }
75  srand(seed);
76  base::PerfTimeLogger logger("PppMessagingPerfTest.StringPerformance");
77  for (int i = 0; i < string_count; ++i) {
78    const std::string test_string(rand() % max_string_size, 'a');
79    PP_Var host_string = StringVar::StringToPPVar(test_string);
80    ppp_messaging->HandleMessage(kTestInstance, host_string);
81    handle_message_called.Wait();
82    PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(host_string);
83  }
84}
85
86}  // namespace proxy
87}  // namespace ppapi
88
89