Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4737)

Unified Diff: chrome/browser/metrics/perf/random_selector.cc

Issue 1392153003: PerfProvider: Get collection parameters from Finch (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@perf_commands
Patch Set: Address comments on PS3 Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/perf/random_selector.cc
diff --git a/chrome/browser/metrics/perf/random_selector.cc b/chrome/browser/metrics/perf/random_selector.cc
index e32c8a048d6c6c42d2ccdd9b858bd56ec08b29ac..e130a6377974730b8518b487e5ae8e8ee0e4c693 100644
--- a/chrome/browser/metrics/perf/random_selector.cc
+++ b/chrome/browser/metrics/perf/random_selector.cc
@@ -14,17 +14,24 @@ RandomSelector::RandomSelector() : sum_of_weights_(0) {}
RandomSelector::~RandomSelector() {}
+// static
double RandomSelector::SumWeights(const std::vector<WeightAndValue>& odds) {
double sum = 0.0;
for (const auto& odd : odds) {
+ if (odd.weight <= 0.0)
+ return -1.0;
sum += odd.weight;
}
return sum;
}
-void RandomSelector::SetOdds(const std::vector<WeightAndValue>& odds) {
+bool RandomSelector::SetOdds(const std::vector<WeightAndValue>& odds) {
+ double sum = SumWeights(odds);
+ if (sum <= 0.0)
+ return false;
odds_ = odds;
- sum_of_weights_ = SumWeights(odds_);
+ sum_of_weights_ = sum;
+ return true;
}
const std::string& RandomSelector::Select() {
@@ -49,3 +56,9 @@ const std::string& RandomSelector::GetValueFor(double random) {
NOTREACHED() << "Invalid value for key: " << random;
return base::EmptyString();
}
+
+// Print the value. Used for friendly test failure messages.
+::std::ostream& operator<<(
+ ::std::ostream& os, const RandomSelector::WeightAndValue& value) {
+ return os << "{" << value.weight << ", \"" << value.value << "\"}";
+}
« no previous file with comments | « chrome/browser/metrics/perf/random_selector.h ('k') | chrome/browser/metrics/perf/random_selector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698