Chromium Code Reviews| Index: apps/benchmark/args.cc |
| diff --git a/apps/benchmark/args.cc b/apps/benchmark/args.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3eb71bf1cebb9e0b98d570784688e922a3459b88 |
| --- /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); |
|
jamesr
2015/08/27 21:12:38
is there a particular reason to avoid base's comma
ppi
2015/08/28 12:15:11
Done and done (switched to base::command_line). Do
|
| + 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 = MeasurementType::TIME_UNTIL; |
| + } else if (parts[0] == "avg_duration") { |
| + result->type = MeasurementType::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)); |
|
jamesr
2015/08/27 21:12:38
what if duration_str doesn't contain a valid int?
ppi
2015/08/28 12:15:11
Done (used the converter in base).
|
| + |
| + // 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) == "--") { |
|
jamesr
2015/08/27 21:12:38
i believe this crashes if the arg length is 0 or 1
ppi
2015/08/28 12:15:11
Done (n/a with switch to base::command_line).
|
| + continue; |
| + } |
| + Measurement measurement; |
| + if (!GetMeasurement(arg, &measurement)) { |
| + return false; |
| + } |
| + result->measurements.push_back(measurement); |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace benchmark |