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 "apps/benchmark/args.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/strings/string_split.h" | |
11 | |
12 namespace benchmark { | |
13 namespace { | |
14 | |
15 bool GetArgValue(const std::vector<std::string>& args, | |
16 std::string prefix, | |
17 std::string* result) { | |
18 auto matches = [&prefix](const std::string& s) -> bool { | |
19 return s.substr(0, prefix.size()) == prefix; | |
20 }; | |
21 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
| |
22 if (it == args.end()) | |
23 return false; | |
24 *result = it->substr(prefix.size()); | |
25 return true; | |
26 } | |
27 | |
28 bool GetMeasurement(const std::string& measurement_spec, Measurement* result) { | |
29 // Measurements are described in the format: | |
30 // <measurement type>/<event category>/<event name>. | |
31 std::vector<std::string> parts; | |
32 base::SplitString(measurement_spec, '/', &parts); | |
33 if (parts.size() != 3) { | |
34 LOG(ERROR) << "Could not parse the measurement description."; | |
35 return false; | |
36 } | |
37 | |
38 if (parts[0] == "time_until") { | |
39 result->type = MeasurementType::TIME_UNTIL; | |
40 } else if (parts[0] == "avg_duration") { | |
41 result->type = MeasurementType::AVG_DURATION; | |
42 } else { | |
43 LOG(ERROR) << "Could not recognize the measurement type: " << parts[0]; | |
44 return false; | |
45 } | |
46 | |
47 result->target_event.category = parts[1]; | |
48 result->target_event.name = parts[2]; | |
49 result->spec = measurement_spec; | |
50 return true; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 Args::Args() {} | |
56 | |
57 Args::~Args() {} | |
58 | |
59 bool GetArgs(const std::vector<std::string>& input_args, Args* result) { | |
60 if (!GetArgValue(input_args, "--app=", &result->app)) { | |
61 LOG(ERROR) << "Could not find the required --app argument."; | |
62 return false; | |
63 } | |
64 | |
65 std::string duration_str; | |
66 if (!GetArgValue(input_args, "--duration=", &duration_str)) { | |
67 LOG(ERROR) << "Could not find the required --duration argument."; | |
68 return false; | |
69 } | |
70 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).
| |
71 | |
72 // Every argument that doesn't start with "--" and is not the first argument | |
73 // (url of the benchmark app) is a measurement to run. | |
74 bool first = true; | |
75 for (const std::string& arg : input_args) { | |
76 if (first) { | |
77 first = false; | |
78 continue; | |
79 } | |
80 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).
| |
81 continue; | |
82 } | |
83 Measurement measurement; | |
84 if (!GetMeasurement(arg, &measurement)) { | |
85 return false; | |
86 } | |
87 result->measurements.push_back(measurement); | |
88 } | |
89 return true; | |
90 } | |
91 | |
92 } // namespace benchmark | |
OLD | NEW |