Index: apps/benchmark/args.cc |
diff --git a/apps/benchmark/args.cc b/apps/benchmark/args.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..98044b7b06bb177fa4d3119fbbb77a24bde5f8ed |
--- /dev/null |
+++ b/apps/benchmark/args.cc |
@@ -0,0 +1,92 @@ |
+// 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 "apps/benchmark/args.h" |
+ |
+#include <algorithm> |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_split.h" |
+ |
+namespace benchmark { |
+namespace { |
+ |
+bool GetArgValue(const std::vector<std::string>& args, |
+ std::string prefix, |
+ std::string* result) { |
+ auto matches = [&prefix](const std::string& s) -> bool { |
+ return s.substr(0, prefix.size()) == prefix; |
+ }; |
+ auto it = std::find_if(args.begin(), args.end(), matches); |
+ if (it == args.end()) |
+ return false; |
+ *result = it->substr(prefix.size()); |
+ return true; |
+} |
+ |
+bool GetMeasurement(const std::string& measurement_spec, Measurement* result) { |
+ // Measurements are described in the format: |
+ // <measurement type>/<event category>/<event name>. |
+ std::vector<std::string> parts; |
+ base::SplitString(measurement_spec, '/', &parts); |
+ if (parts.size() != 3) { |
+ LOG(ERROR) << "Could not parse the measurement description."; |
+ return false; |
+ } |
+ |
+ if (parts[0] == "time_until") { |
+ result->type = Measurement::TIME_UNTIL; |
+ } else if (parts[0] == "avg_duration") { |
+ result->type = Measurement::AVG_DURATION; |
+ } else { |
+ LOG(ERROR) << "Could not recognize the measurement type: " << parts[0]; |
+ return false; |
+ } |
+ |
+ result->target_event.category = parts[1]; |
+ result->target_event.name = parts[2]; |
+ result->spec = measurement_spec; |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+Args::Args() {} |
+ |
+Args::~Args() {} |
+ |
+bool GetArgs(const std::vector<std::string>& input_args, Args* result) { |
+ if (!GetArgValue(input_args, "--app=", &result->app)) { |
+ LOG(ERROR) << "Could not find the required --app argument."; |
+ return false; |
+ } |
+ |
+ std::string duration_str; |
+ if (!GetArgValue(input_args, "--duration=", &duration_str)) { |
+ LOG(ERROR) << "Could not find the required --duration argument."; |
+ return false; |
+ } |
+ result->duration = base::TimeDelta::FromSeconds(std::stoi(duration_str)); |
+ |
+ // Every argument that doesn't start with "--" and is not the first argument |
+ // (url of the benchmark app) is a measurement to run. |
+ bool first = true; |
+ for (const std::string& arg : input_args) { |
+ if (first) { |
+ first = false; |
+ continue; |
+ } |
+ if (arg.substr(0, 2) == "--") { |
+ continue; |
+ } |
+ Measurement measurement; |
+ if (!GetMeasurement(arg, &measurement)) { |
+ return false; |
+ } |
+ result->measurements.push_back(measurement); |
+ } |
+ return true; |
+} |
+ |
+} // namespace benchmark |