OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 #include <set> |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "apps/benchmark/event.h" |
| 11 #include "apps/benchmark/measurements.h" |
| 12 #include "apps/benchmark/run_args.h" |
| 13 #include "apps/benchmark/trace_collector_client.h" |
| 14 #include "base/bind.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/strings/string_util.h" |
| 18 #include "base/time/time.h" |
| 19 #include "base/trace_event/trace_event.h" |
| 20 #include "mojo/application/application_runner_chromium.h" |
| 21 #include "mojo/public/c/system/main.h" |
| 22 #include "mojo/public/cpp/application/application_connection.h" |
| 23 #include "mojo/public/cpp/application/application_delegate.h" |
| 24 #include "mojo/public/cpp/application/application_impl.h" |
| 25 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" |
| 26 |
| 27 namespace benchmark { |
| 28 namespace { |
| 29 |
| 30 class BenchmarkApp : public mojo::ApplicationDelegate, |
| 31 public TraceCollectorClient::Receiver { |
| 32 public: |
| 33 BenchmarkApp() {} |
| 34 ~BenchmarkApp() override {} |
| 35 |
| 36 // mojo:ApplicationDelegate: |
| 37 void Initialize(mojo::ApplicationImpl* app) override { |
| 38 // Parse command-line arguments. |
| 39 if (!GetRunArgs(app->args(), &args_)) { |
| 40 LOG(ERROR) << "Failed to parse the input arguments."; |
| 41 mojo::ApplicationImpl::Terminate(); |
| 42 return; |
| 43 } |
| 44 |
| 45 // Calculate a list of trace categories we want to collect: a union of all |
| 46 // categories targeted in measurements. |
| 47 std::set<std::string> category_set; |
| 48 for (const Measurement& measurement : args_.measurements) { |
| 49 category_set.insert(measurement.target_event.category); |
| 50 } |
| 51 std::vector<std::string> unique_categories(category_set.begin(), |
| 52 category_set.end()); |
| 53 std::string categories_str = JoinString(unique_categories, ','); |
| 54 |
| 55 // Connect to trace collector, which will fetch the trace events produced by |
| 56 // the app being benchmarked. |
| 57 tracing::TraceCollectorPtr trace_collector; |
| 58 app->ConnectToService("mojo:tracing", &trace_collector); |
| 59 trace_collector_client_.reset( |
| 60 new TraceCollectorClient(this, trace_collector.Pass())); |
| 61 trace_collector_client_->Start(categories_str); |
| 62 |
| 63 // Record the time origin for measurements just before connecting to the app |
| 64 // being benchmarked. |
| 65 time_origin_ = base::TimeTicks::FromInternalValue(MojoGetTimeTicksNow()); |
| 66 traced_app_connection_ = app->ConnectToApplication(args_.app); |
| 67 |
| 68 // Post task to stop tracing when the time is up. |
| 69 base::MessageLoop::current()->PostDelayedTask( |
| 70 FROM_HERE, |
| 71 base::Bind(&BenchmarkApp::StopTracing, base::Unretained(this)), |
| 72 args_.duration); |
| 73 } |
| 74 |
| 75 void StopTracing() { |
| 76 // Request the trace collector to send back the data. When the data is ready |
| 77 // we will be called at OnTraceCollected(). |
| 78 trace_collector_client_->Stop(); |
| 79 } |
| 80 |
| 81 // TraceCollectorClient::Receiver: |
| 82 void OnTraceCollected(std::string trace_data) override { |
| 83 // Parse trace events. |
| 84 std::vector<Event> events; |
| 85 if (!GetEvents(trace_data, &events)) { |
| 86 LOG(ERROR) << "Failed to parse the trace data"; |
| 87 mojo::ApplicationImpl::Terminate(); |
| 88 return; |
| 89 } |
| 90 |
| 91 // Calculate and print the results. |
| 92 bool succeeded = true; |
| 93 Measurements measurements(events, time_origin_); |
| 94 for (const Measurement& measurement : args_.measurements) { |
| 95 double result = measurements.Measure(measurement); |
| 96 if (result >= 0.0) { |
| 97 printf("measurement: %s %lf\n", measurement.spec.c_str(), result); |
| 98 } else { |
| 99 succeeded = false; |
| 100 printf("measurement: %s FAILED\n", measurement.spec.c_str()); |
| 101 } |
| 102 } |
| 103 |
| 104 // Scripts that run benchmarks can pick this up as a signal that the run |
| 105 // succeeded, as shell exit code is 0 even if an app exits early due to an |
| 106 // error. |
| 107 if (succeeded) { |
| 108 printf("benchmark succeeded\n"); |
| 109 } else { |
| 110 printf("some measurements failed\n"); |
| 111 } |
| 112 mojo::ApplicationImpl::Terminate(); |
| 113 } |
| 114 |
| 115 private: |
| 116 RunArgs args_; |
| 117 mojo::ApplicationConnection* traced_app_connection_; |
| 118 scoped_ptr<TraceCollectorClient> trace_collector_client_; |
| 119 base::TimeTicks time_origin_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(BenchmarkApp); |
| 122 }; |
| 123 } // namespace |
| 124 } // namespace benchmark |
| 125 |
| 126 MojoResult MojoMain(MojoHandle application_request) { |
| 127 mojo::ApplicationRunnerChromium runner(new benchmark::BenchmarkApp); |
| 128 auto ret = runner.Run(application_request); |
| 129 fflush(nullptr); |
| 130 return ret; |
| 131 } |
OLD | NEW |