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 "chrome/browser/metrics/perf/random_selector.h" | |
6 | |
7 #include <cmath> | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 // A small floating point number used to verify that the expected odds are equal | |
14 // to the odds set. | |
15 const double kEpsilon = 0.01; | |
16 | |
17 // A class that overrides RandDoubleUpTo() to not be random. The number | |
18 // generator will emulate a uniform distribution of numbers between 0.0 and | |
19 // |max| when called with the same |max| parameter and a whole multiple of | |
20 // |random_period| times. This allows better testing of the RandomSelector | |
21 // class. | |
22 class RandomSelectorWithCustomRNG : public RandomSelector { | |
23 public: | |
24 explicit RandomSelectorWithCustomRNG(unsigned int random_period) | |
25 : random_period_(random_period), current_index_(0) {} | |
26 | |
27 private: | |
28 // This function returns floats between 0.0 and |max| in an increasing | |
29 // fashion at regular intervals. | |
30 double RandDoubleUpTo(double max) override { | |
31 current_index_ = (current_index_ + 1) % random_period_; | |
32 return max * current_index_ / random_period_; | |
33 } | |
34 | |
35 // Period (number of calls) over which the fake RNG repeats. | |
36 const unsigned int random_period_; | |
37 | |
38 // Stores the current position we are at in the interval between 0.0 and | |
39 // |max|. See the function RandDoubleUpTo for details on how this is used. | |
40 int current_index_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(RandomSelectorWithCustomRNG); | |
43 }; | |
44 | |
45 // Use the random_selector to generate some values. The number of values to | |
46 // generate is |iterations|. | |
47 void GenerateResults(size_t iterations, | |
48 RandomSelector* random_selector, | |
49 std::map<std::string, int>* results) { | |
50 for (size_t i = 0; i < iterations; ++i) { | |
51 const std::string& next_value = random_selector->Select(); | |
52 (*results)[next_value]++; | |
53 } | |
54 } | |
55 | |
56 // This function tests whether the results are close enough to the odds (within | |
57 // 1%). | |
Alexei Svitkine (slow)
2015/09/15 15:31:18
Does this mean the test is expected to be flaky so
dhsharp
2015/09/15 18:21:32
No, not since we added the non-random "CustomRNG".
| |
58 void CheckResultsAgainstOdds( | |
59 const std::vector<RandomSelector::WeightAndValue>& odds, | |
60 const std::map<std::string, int>& results) { | |
61 EXPECT_EQ(odds.size(), results.size()); | |
62 | |
63 const double odds_sum = RandomSelector::SumWeights(odds); | |
64 int results_sum = 0; | |
65 for (const auto& item : results) { | |
66 results_sum += item.second; | |
67 } | |
68 | |
69 for (const auto& odd : odds) { | |
70 const auto result = results.find(odd.value); | |
71 EXPECT_NE(result, results.end()); | |
72 const double results_ratio = 1.0*result->second / results_sum; | |
73 const double odds_ratio = odd.weight / odds_sum; | |
74 const double abs_diff = std::abs(results_ratio - odds_ratio); | |
75 EXPECT_LT(abs_diff, kEpsilon); | |
76 } | |
77 } | |
78 | |
79 TEST(RandomSelector, SimpleAccessors) { | |
80 using WeightAndValue = RandomSelector::WeightAndValue; | |
81 std::vector<WeightAndValue> odds; | |
82 odds.push_back(WeightAndValue(1, "a 1")); | |
83 odds.push_back(WeightAndValue(3, "b --help")); | |
84 odds.push_back(WeightAndValue(107, "c bar")); | |
85 EXPECT_EQ(111.0L, RandomSelector::SumWeights(odds)); | |
86 RandomSelector random_selector; | |
87 random_selector.SetOdds(odds); | |
88 EXPECT_EQ(3UL, random_selector.num_values()); | |
89 } | |
90 | |
91 // Ensure RandomSelector is able to generate results from given odds. | |
92 TEST(RandomSelector, GenerateTest) { | |
93 using WeightAndValue = RandomSelector::WeightAndValue; | |
94 const int kLargeNumber = 2000; | |
95 std::vector<RandomSelector::WeightAndValue> odds; | |
96 odds.push_back(WeightAndValue(1, "a 1")); | |
97 odds.push_back(WeightAndValue(2, "b --help")); | |
98 odds.push_back(WeightAndValue(3, "c bar")); | |
99 RandomSelectorWithCustomRNG random_selector(kLargeNumber); | |
100 random_selector.SetOdds(odds); | |
101 // Generate a lot of values. | |
102 std::map<std::string, int> results; | |
103 GenerateResults(kLargeNumber, &random_selector, &results); | |
104 // Ensure the values and odds are related. | |
105 CheckResultsAgainstOdds(odds, results); | |
106 } | |
OLD | NEW |