Index: apps/benchmark/benchmark_app.cc |
diff --git a/apps/benchmark/benchmark_app.cc b/apps/benchmark/benchmark_app.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d72efdc241af7e736aebd5e8318fe17c4def709c |
--- /dev/null |
+++ b/apps/benchmark/benchmark_app.cc |
@@ -0,0 +1,150 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <memory> |
+#include <set> |
+#include <string> |
+#include <vector> |
+ |
+#include "apps/benchmark/args.h" |
+#include "apps/benchmark/event.h" |
+#include "apps/benchmark/measurements.h" |
+#include "apps/benchmark/trace_collector_client.h" |
+#include "base/bind.h" |
+#include "base/json/json_reader.h" |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string_util.h" |
+#include "base/time/time.h" |
+#include "base/trace_event/trace_event.h" |
+#include "base/values.h" |
+#include "mojo/application/application_runner_chromium.h" |
+#include "mojo/common/tracing_impl.h" |
+#include "mojo/public/c/system/main.h" |
+#include "mojo/public/cpp/application/application_connection.h" |
+#include "mojo/public/cpp/application/application_delegate.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/services/tracing/public/interfaces/tracing.mojom.h" |
+ |
+namespace benchmark { |
+namespace { |
+ |
+const char* kOriginEventCategory = "benchmark"; |
+const char* kOriginEventName = "time_origin"; |
+ |
+class BenchmarkApp : public mojo::ApplicationDelegate, |
+ public TraceCollectorClient::Receiver { |
+ public: |
+ BenchmarkApp() {} |
+ ~BenchmarkApp() override {} |
+ |
+ // mojo:ApplicationDelegate: |
+ void Initialize(mojo::ApplicationImpl* app) override { |
+ // Parse command-line arguments. |
+ if (!GetArgs(app->args(), &args_)) { |
+ mojo::ApplicationImpl::Terminate(); |
+ return; |
+ } |
+ |
+ // Initialize tracing in the benchmark app itself, so that we can record the |
+ // time origin trace event. |
jamesr
2015/08/21 22:56:16
i'm not sure why this is needed - isn't the tracin
ppi
2015/08/24 15:41:05
Done.
|
+ tracing_.Initialize(app); |
jamesr
2015/08/21 22:56:16
you would want to do this if you wanted this app t
ppi
2015/08/24 15:41:05
Done.
|
+ tracing_.set_tracing_already_started(true); |
+ base::trace_event::TraceLog::GetInstance()->SetEnabled( |
+ base::trace_event::TraceConfig(kOriginEventCategory, |
+ base::trace_event::RECORD_UNTIL_FULL), |
+ base::trace_event::TraceLog::RECORDING_MODE); |
+ |
+ // Calculate a list of trace categories we want to collect: a union of all |
+ // categories targeted in measurements and kOriginEventCategory. |
+ std::set<std::string> category_set; |
+ category_set.insert(kOriginEventCategory); |
+ for (const Measurement& measurement : args_.measurements) { |
+ category_set.insert(measurement.target_event.category); |
+ } |
+ std::vector<std::string> unique_categories(category_set.begin(), |
+ category_set.end()); |
+ std::string categories_str = JoinString(unique_categories, ','); |
+ |
+ // Connect to trace collector. It will fetch trace events produced both by |
+ // the app being benchmarked and by the benchmark app. |
+ tracing::TraceCollectorPtr trace_collector; |
+ app->ConnectToService("mojo:tracing", &trace_collector); |
+ trace_collector_client_.reset( |
+ new TraceCollectorClient(this, trace_collector.Pass())); |
+ trace_collector_client_->Start(categories_str); |
+ |
+ // Record reference event serving as the time origin for measurements. This |
+ // happens just before connecting to the app being benchmarked. |
+ TRACE_EVENT_INSTANT0(kOriginEventCategory, kOriginEventName, |
+ TRACE_EVENT_SCOPE_THREAD); |
+ traced_app_connection_ = app->ConnectToApplication(args_.app); |
+ |
+ // Post task to stop tracing when the time is up. |
+ base::MessageLoop::current()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BenchmarkApp::StopTracing, base::Unretained(this)), |
+ args_.duration); |
+ } |
+ |
+ void StopTracing() { |
+ // Request the trace collector to send back the data. When the data is ready |
+ // we will be called at OnTraceCollected(). |
+ trace_collector_client_->Stop(); |
+ } |
+ |
+ // TraceCollectorClient::Receiver: |
+ void OnTraceCollected(std::string trace_data) override { |
+ // Parse the JSON string describing the events. |
+ base::JSONReader reader; |
+ scoped_ptr<base::Value> parsed_trace_data = reader.ReadToValue(trace_data); |
+ if (!parsed_trace_data) { |
+ LOG(ERROR) << "Failed to parse trace data (bad format): " |
+ << reader.GetErrorMessage(); |
+ mojo::ApplicationImpl::Terminate(); |
+ return; |
+ } |
+ |
+ // Parse the events from the JSON tree. |
+ std::vector<Event> events; |
+ if (!GetEvents(parsed_trace_data.Pass(), &events)) { |
+ LOG(ERROR) << "Failed to parse the trace data (bad content)"; |
+ mojo::ApplicationImpl::Terminate(); |
+ return; |
+ } |
+ |
+ // Calculate and print the results. |
+ EventSpec time_origin_spec = |
+ EventSpec({.name = kOriginEventName, .category = kOriginEventCategory}); |
+ Measurements measurements(events, time_origin_spec); |
+ for (const Measurement& measurement : args_.measurements) { |
+ double result = measurements.Measure(measurement); |
+ printf("measurement: %s %lf\n", measurement.spec.c_str(), result); |
+ } |
+ |
+ // Scripts that run benchmarks can pick this up as a signal that the run |
+ // succeeded (as shell exit code is still 0 if we exit early due to an |
+ // error). |
+ printf("benchmark succeeded\n"); |
+ mojo::ApplicationImpl::Terminate(); |
+ } |
+ |
+ private: |
+ Args args_; |
+ mojo::ApplicationConnection* traced_app_connection_; |
+ scoped_ptr<TraceCollectorClient> trace_collector_client_; |
+ mojo::TracingImpl tracing_; |
+ EventSpec time_origin_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BenchmarkApp); |
+}; |
+} // namespace |
+} // namespace benchmark |
+ |
+MojoResult MojoMain(MojoHandle application_request) { |
+ mojo::ApplicationRunnerChromium runner(new benchmark::BenchmarkApp); |
+ auto ret = runner.Run(application_request); |
+ fflush(NULL); |
jamesr
2015/08/21 22:56:16
use 'nullptr', this is c++ code
ppi
2015/08/24 15:41:05
Done.
|
+ return ret; |
+} |