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/args.h" | |
11 #include "apps/benchmark/event.h" | |
12 #include "apps/benchmark/measurements.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 (!GetArgs(app->args(), &args_)) { | |
40 mojo::ApplicationImpl::Terminate(); | |
jamesr
2015/08/27 21:12:38
maybe let the user know that the arguments didn't
ppi
2015/08/28 12:15:11
Done.
| |
41 return; | |
42 } | |
43 | |
44 // Calculate a list of trace categories we want to collect: a union of all | |
45 // categories targeted in measurements. | |
46 std::set<std::string> category_set; | |
47 for (const Measurement& measurement : args_.measurements) { | |
48 category_set.insert(measurement.target_event.category); | |
49 } | |
50 std::vector<std::string> unique_categories(category_set.begin(), | |
51 category_set.end()); | |
52 std::string categories_str = JoinString(unique_categories, ','); | |
jamesr
2015/08/27 21:12:38
hmm, this uses base::'s string utilities (which se
ppi
2015/08/28 12:15:11
Done.
| |
53 | |
54 // Connect to trace collector, which will fetch the trace events produced by | |
55 // the app being benchmarked. | |
56 tracing::TraceCollectorPtr trace_collector; | |
57 app->ConnectToService("mojo:tracing", &trace_collector); | |
58 trace_collector_client_.reset( | |
59 new TraceCollectorClient(this, trace_collector.Pass())); | |
60 trace_collector_client_->Start(categories_str); | |
61 | |
62 // Record the time origin for measurements just before connecting to the app | |
63 // being benchmarked. | |
64 time_origin_ = base::TimeTicks::FromInternalValue(MojoGetTimeTicksNow()); | |
65 traced_app_connection_ = app->ConnectToApplication(args_.app); | |
66 | |
67 // Post task to stop tracing when the time is up. | |
68 base::MessageLoop::current()->PostDelayedTask( | |
69 FROM_HERE, | |
70 base::Bind(&BenchmarkApp::StopTracing, base::Unretained(this)), | |
71 args_.duration); | |
72 } | |
73 | |
74 void StopTracing() { | |
75 // Request the trace collector to send back the data. When the data is ready | |
76 // we will be called at OnTraceCollected(). | |
77 trace_collector_client_->Stop(); | |
78 } | |
79 | |
80 // TraceCollectorClient::Receiver: | |
81 void OnTraceCollected(std::string trace_data) override { | |
82 // Parse trace events. | |
83 std::vector<Event> events; | |
84 if (!GetEvents(trace_data, &events)) { | |
85 LOG(ERROR) << "Failed to parse the trace data"; | |
86 mojo::ApplicationImpl::Terminate(); | |
87 return; | |
88 } | |
89 | |
90 // Calculate and print the results. | |
91 bool succeeded = true; | |
92 Measurements measurements(events, time_origin_); | |
93 for (const Measurement& measurement : args_.measurements) { | |
94 double result = measurements.Measure(measurement); | |
95 if (result >= 0.0) { | |
96 printf("measurement: %s %lf\n", measurement.spec.c_str(), result); | |
97 } else { | |
98 succeeded = false; | |
99 printf("measurement: %s FAILED\n", measurement.spec.c_str()); | |
100 } | |
101 } | |
102 | |
103 // Scripts that run benchmarks can pick this up as a signal that the run | |
104 // succeeded, as shell exit code is 0 even if an app exits early due to an | |
105 // error. | |
106 if (succeeded) { | |
107 printf("benchmark succeeded\n"); | |
108 } else { | |
109 printf("some measurements failed\n"); | |
110 } | |
111 mojo::ApplicationImpl::Terminate(); | |
112 } | |
113 | |
114 private: | |
115 Args args_; | |
116 mojo::ApplicationConnection* traced_app_connection_; | |
117 scoped_ptr<TraceCollectorClient> trace_collector_client_; | |
118 base::TimeTicks time_origin_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(BenchmarkApp); | |
121 }; | |
122 } // namespace | |
123 } // namespace benchmark | |
124 | |
125 MojoResult MojoMain(MojoHandle application_request) { | |
126 mojo::ApplicationRunnerChromium runner(new benchmark::BenchmarkApp); | |
127 auto ret = runner.Run(application_request); | |
128 fflush(nullptr); | |
129 return ret; | |
130 } | |
OLD | NEW |