| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "examples/echo/echo.mojom.h" | 10 #include "examples/echo/echo.mojom.h" |
| 11 #include "mojo/application/application_runner_chromium.h" | |
| 12 #include "mojo/common/tracing_impl.h" | 11 #include "mojo/common/tracing_impl.h" |
| 12 #include "mojo/environment/scoped_chromium_init.h" |
| 13 #include "mojo/public/c/system/main.h" | 13 #include "mojo/public/c/system/main.h" |
| 14 #include "mojo/public/cpp/application/application_delegate.h" | 14 #include "mojo/public/cpp/application/application_impl_base.h" |
| 15 #include "mojo/public/cpp/application/application_impl.h" | |
| 16 #include "mojo/public/cpp/application/connect.h" | 15 #include "mojo/public/cpp/application/connect.h" |
| 16 #include "mojo/public/cpp/application/run_application.h" |
| 17 #include "mojo/public/cpp/utility/run_loop.h" | 17 #include "mojo/public/cpp/utility/run_loop.h" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 namespace examples { | 20 namespace examples { |
| 21 | 21 |
| 22 static const base::TimeDelta kWarmupTime = | 22 static const base::TimeDelta kWarmupTime = |
| 23 base::TimeDelta::FromMilliseconds(1000); | 23 base::TimeDelta::FromMilliseconds(1000); |
| 24 | 24 |
| 25 static const base::TimeDelta kDelayTime = base::TimeDelta::FromMicroseconds(50); | 25 static const base::TimeDelta kDelayTime = base::TimeDelta::FromMicroseconds(50); |
| 26 | 26 |
| 27 class EchoClientDelegate; | 27 class EchoClientApp; |
| 28 | 28 |
| 29 class EchoResponse { | 29 class EchoResponse { |
| 30 public: | 30 public: |
| 31 EchoResponse(EchoClientDelegate* echo, int idx, bool traced) | 31 EchoResponse(EchoClientApp* echo, int idx, bool traced) |
| 32 : echoDelegate_(echo), idx_(idx), traced_(traced) {} | 32 : echo_client_app_(echo), idx_(idx), traced_(traced) {} |
| 33 | 33 |
| 34 void Run(const String& value) const; | 34 void Run(const String& value) const; |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 EchoClientDelegate* echoDelegate_; | 37 EchoClientApp* echo_client_app_; |
| 38 int idx_; | 38 int idx_; |
| 39 bool traced_; | 39 bool traced_; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class EchoClientDelegate : public ApplicationDelegate { | 42 class EchoClientApp : public ApplicationImplBase { |
| 43 public: | 43 public: |
| 44 EchoClientDelegate() | 44 EchoClientApp() |
| 45 : warmup_(true), | 45 : warmup_(true), |
| 46 num_clients_(1), | 46 num_clients_(1), |
| 47 num_active_clients_(1), | 47 num_active_clients_(1), |
| 48 use_dart_server_(false), | 48 use_dart_server_(false), |
| 49 ending_(false), | 49 ending_(false), |
| 50 benchmark_duration_(base::TimeDelta::FromSeconds(10)) {} | 50 benchmark_duration_(base::TimeDelta::FromSeconds(10)) {} |
| 51 | 51 |
| 52 void Initialize(ApplicationImpl* app) override { | 52 void OnInitialize() override { |
| 53 tracing_.Initialize(app->shell(), &app->args()); | 53 tracing_.Initialize(shell(), &args()); |
| 54 | 54 |
| 55 ParseArguments(app); | 55 ParseArguments(); |
| 56 | 56 |
| 57 for (int i = 0; i < num_clients_; i++) { | 57 for (int i = 0; i < num_clients_; i++) { |
| 58 EchoPtr echo; | 58 EchoPtr echo; |
| 59 if (use_dart_server_) { | 59 if (use_dart_server_) { |
| 60 ConnectToService(app->shell(), "mojo:dart_echo_server", | 60 ConnectToService(shell(), "mojo:dart_echo_server", GetProxy(&echo)); |
| 61 GetProxy(&echo)); | |
| 62 } else { | 61 } else { |
| 63 ConnectToService(app->shell(), "mojo:echo_server", GetProxy(&echo)); | 62 ConnectToService(shell(), "mojo:echo_server", GetProxy(&echo)); |
| 64 } | 63 } |
| 65 echoClients_.push_back(echo.Pass()); | 64 echoClients_.push_back(echo.Pass()); |
| 66 } | 65 } |
| 67 BeginEcho(0); | 66 BeginEcho(0); |
| 68 base::MessageLoop::current()->PostDelayedTask( | 67 base::MessageLoop::current()->PostDelayedTask( |
| 69 FROM_HERE, | 68 FROM_HERE, |
| 70 base::Bind(&EchoClientDelegate::EndWarmup, base::Unretained(this)), | 69 base::Bind(&EchoClientApp::EndWarmup, base::Unretained(this)), |
| 71 kWarmupTime); | 70 kWarmupTime); |
| 72 } | 71 } |
| 73 | 72 |
| 74 void BeginEcho(int idx) { | 73 void BeginEcho(int idx) { |
| 75 if (ending_) return; | 74 if (ending_) return; |
| 76 if (idx == num_active_clients_) { | 75 if (idx == num_active_clients_) { |
| 77 idx = 0; | 76 idx = 0; |
| 78 } | 77 } |
| 79 base::MessageLoop::current()->PostDelayedTask( | 78 base::MessageLoop::current()->PostDelayedTask( |
| 80 FROM_HERE, | 79 FROM_HERE, base::Bind(&EchoClientApp::Run, base::Unretained(this), idx), |
| 81 base::Bind(&EchoClientDelegate::Run, base::Unretained(this), idx), | |
| 82 kDelayTime); | 80 kDelayTime); |
| 83 base::MessageLoop::current()->PostDelayedTask( | 81 base::MessageLoop::current()->PostDelayedTask( |
| 84 FROM_HERE, | 82 FROM_HERE, base::Bind(&EchoClientApp::EndRun, base::Unretained(this)), |
| 85 base::Bind(&EchoClientDelegate::EndRun, base::Unretained(this)), | |
| 86 benchmark_duration_); | 83 benchmark_duration_); |
| 87 } | 84 } |
| 88 | 85 |
| 89 void EndEcho(int idx, bool traced) { | 86 void EndEcho(int idx, bool traced) { |
| 90 if (traced) { | 87 if (traced) { |
| 91 TRACE_EVENT_ASYNC_END0("echo_benchmark", "ping", echoClients_[idx].get()); | 88 TRACE_EVENT_ASYNC_END0("echo_benchmark", "ping", echoClients_[idx].get()); |
| 92 } | 89 } |
| 93 } | 90 } |
| 94 | 91 |
| 95 private: | 92 private: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 122 if (start_pos != std::string::npos) { | 119 if (start_pos != std::string::npos) { |
| 123 size_t equals_pos = argument.find("=", start_pos); | 120 size_t equals_pos = argument.find("=", start_pos); |
| 124 if (equals_pos != std::string::npos) { | 121 if (equals_pos != std::string::npos) { |
| 125 base::StringToInt(argument.substr(equals_pos + 1), value); | 122 base::StringToInt(argument.substr(equals_pos + 1), value); |
| 126 return true; | 123 return true; |
| 127 } | 124 } |
| 128 } | 125 } |
| 129 return false; | 126 return false; |
| 130 } | 127 } |
| 131 | 128 |
| 132 void ParseArguments(ApplicationImpl* app) { | 129 void ParseArguments() { |
| 133 int benchmark_duration_seconds = 0; | 130 int benchmark_duration_seconds = 0; |
| 134 for (size_t i = 0; i < app->args().size(); i++) { | 131 for (size_t i = 0; i < args().size(); i++) { |
| 135 const std::string& argument = app->args()[i]; | 132 const std::string& argument = args()[i]; |
| 136 if (GetBoolArgument(argument, "--dart-server", &use_dart_server_)) { | 133 if (GetBoolArgument(argument, "--dart-server", &use_dart_server_)) { |
| 137 continue; | 134 continue; |
| 138 } | 135 } |
| 139 if (GetIntegerArgument(argument, "--num-clients", &num_clients_)) { | 136 if (GetIntegerArgument(argument, "--num-clients", &num_clients_)) { |
| 140 continue; | 137 continue; |
| 141 } | 138 } |
| 142 if (GetIntegerArgument(argument, "--num-active-clients", | 139 if (GetIntegerArgument(argument, "--num-active-clients", |
| 143 &num_active_clients_)) { | 140 &num_active_clients_)) { |
| 144 continue; | 141 continue; |
| 145 } | 142 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 160 int num_clients_; | 157 int num_clients_; |
| 161 int num_active_clients_; | 158 int num_active_clients_; |
| 162 bool use_dart_server_; | 159 bool use_dart_server_; |
| 163 bool ending_; | 160 bool ending_; |
| 164 base::TimeDelta benchmark_duration_; | 161 base::TimeDelta benchmark_duration_; |
| 165 std::vector<EchoPtr> echoClients_; | 162 std::vector<EchoPtr> echoClients_; |
| 166 mojo::TracingImpl tracing_; | 163 mojo::TracingImpl tracing_; |
| 167 }; | 164 }; |
| 168 | 165 |
| 169 void EchoResponse::Run(const String& value) const { | 166 void EchoResponse::Run(const String& value) const { |
| 170 echoDelegate_->EndEcho(idx_, traced_); | 167 echo_client_app_->EndEcho(idx_, traced_); |
| 171 echoDelegate_->BeginEcho(idx_ + 1); | 168 echo_client_app_->BeginEcho(idx_ + 1); |
| 172 } | 169 } |
| 173 | 170 |
| 174 } // namespace examples | 171 } // namespace examples |
| 175 } // namespace mojo | 172 } // namespace mojo |
| 176 | 173 |
| 177 MojoResult MojoMain(MojoHandle application_request) { | 174 MojoResult MojoMain(MojoHandle application_request) { |
| 178 mojo::ApplicationRunnerChromium runner( | 175 mojo::ScopedChromiumInit init; |
| 179 new mojo::examples::EchoClientDelegate); | 176 mojo::examples::EchoClientApp echo_client_app; |
| 180 return runner.Run(application_request); | 177 return mojo::RunApplication(application_request, &echo_client_app); |
| 181 } | 178 } |
| OLD | NEW |