From 5bdddf91d300299a8d40f153b842dba61db58c60 Mon Sep 17 00:00:00 2001 From: solenberg Date: Thu, 15 Oct 2015 05:10:30 -0700 Subject: [PATCH] Move PRNG from BWE test framework to webrtc/test. BUG= Review URL: https://codereview.webrtc.org/1404953002 Cr-Commit-Position: refs/heads/master@{#10285} --- .../overuse_detector_unittest.cc | 4 +- .../remote_bitrate_estimator.gypi | 2 - .../remote_bitrate_estimator/test/bwe_test.cc | 4 +- .../test/bwe_test_framework.cc | 37 +------------------ .../test/bwe_test_framework.h | 34 ++--------------- .../test/bwe_test_framework_unittest.cc | 2 +- .../test/random.cc | 18 ++++++++- .../test/random.h | 15 ++++++-- webrtc/test/webrtc_test_common.gyp | 2 + 9 files changed, 41 insertions(+), 77 deletions(-) rename webrtc/{modules/remote_bitrate_estimator => }/test/random.cc (77%) rename webrtc/{modules/remote_bitrate_estimator => }/test/random.h (76%) diff --git a/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc b/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc index c7d129718f..dcad04b5f6 100644 --- a/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc +++ b/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc @@ -20,8 +20,8 @@ #include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h" #include "webrtc/modules/remote_bitrate_estimator/overuse_estimator.h" #include "webrtc/modules/remote_bitrate_estimator/rate_statistics.h" -#include "webrtc/modules/remote_bitrate_estimator/test/random.h" #include "webrtc/test/field_trial.h" +#include "webrtc/test/random.h" #include "webrtc/test/testsupport/gtest_disable.h" namespace webrtc { @@ -114,7 +114,7 @@ class OveruseDetectorTest : public ::testing::Test { rtc::scoped_ptr overuse_detector_; rtc::scoped_ptr overuse_estimator_; rtc::scoped_ptr inter_arrival_; - Random random_; + test::Random random_; }; TEST_F(OveruseDetectorTest, GaussianRandom) { diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator.gypi b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator.gypi index dbc5882456..d2af81e2f0 100644 --- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator.gypi +++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator.gypi @@ -85,8 +85,6 @@ 'test/packet_sender.cc', 'test/packet_sender.h', 'test/packet.h', - 'test/random.cc', - 'test/random.h', 'test/estimators/nada.cc', 'test/estimators/nada.h', 'test/estimators/remb.cc', diff --git a/webrtc/modules/remote_bitrate_estimator/test/bwe_test.cc b/webrtc/modules/remote_bitrate_estimator/test/bwe_test.cc index 72283b98c7..9d71323d01 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/bwe_test.cc +++ b/webrtc/modules/remote_bitrate_estimator/test/bwe_test.cc @@ -947,7 +947,7 @@ std::vector BweTest::GetFileSizesBytes(int num_files) { const int kMinKbytes = 100; const int kMaxKbytes = 1000; - Random random(0x12345678); + test::Random random(0x12345678); std::vector tcp_file_sizes_bytes; while (num_files-- > 0) { @@ -960,7 +960,7 @@ std::vector BweTest::GetFileSizesBytes(int num_files) { std::vector BweTest::GetStartingTimesMs(int num_files) { // OFF state behaves as an exp. distribution with mean = 10 seconds. const float kMeanMs = 10000.0f; - Random random(0x12345678); + test::Random random(0x12345678); std::vector tcp_starting_times_ms; diff --git a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.cc b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.cc index d4201933e7..4574d3d8a1 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.cc +++ b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.cc @@ -92,41 +92,6 @@ double RateCounter::BitrateWindowS() const { return static_cast(window_size_us_) / (1000 * 1000); } -Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { -} - -float Random::Rand() { - const float kScale = 1.0f / 0xffffffff; - float result = kScale * b_; - a_ ^= b_; - b_ += a_; - return result; -} - -int Random::Rand(int low, int high) { - float uniform = Rand() * (high - low + 1) + low; - return static_cast(uniform); -} - -int Random::Gaussian(int mean, int standard_deviation) { - // Creating a Normal distribution variable from two independent uniform - // variables based on the Box-Muller transform, which is defined on the - // interval (0, 1], hence the mask+add below. - const double kPi = 3.14159265358979323846; - const double kScale = 1.0 / 0x80000000ul; - double u1 = kScale * ((a_ & 0x7ffffffful) + 1); - double u2 = kScale * ((b_ & 0x7ffffffful) + 1); - a_ ^= b_; - b_ += a_; - return static_cast( - mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); -} - -int Random::Exponential(float lambda) { - float uniform = Rand(); - return static_cast(-log(uniform) / lambda); -} - Packet::Packet() : flow_id_(0), creation_time_us_(-1), @@ -426,7 +391,7 @@ void JitterFilter::SetMaxJitter(int64_t max_jitter_ms) { } namespace { -inline int64_t TruncatedNSigmaGaussian(Random* const random, +inline int64_t TruncatedNSigmaGaussian(test::Random* const random, int64_t mean, int64_t std_dev) { int64_t gaussian_random = random->Gaussian(mean, std_dev); diff --git a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h index 77b03fe7b1..1362c87037 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h +++ b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h @@ -29,9 +29,9 @@ #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" #include "webrtc/modules/remote_bitrate_estimator/test/packet.h" -#include "webrtc/modules/remote_bitrate_estimator/test/random.h" #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" #include "webrtc/system_wrappers/interface/clock.h" +#include "webrtc/test/random.h" namespace webrtc { @@ -173,32 +173,6 @@ template class Stats { T max_; }; -class Random { - public: - explicit Random(uint32_t seed); - - // Return pseudo random number in the interval [0.0, 1.0]. - float Rand(); - - // Return pseudo rounded random number in interval [low, high]. - int Rand(int low, int high); - - // Normal Distribution. - int Gaussian(int mean, int standard_deviation); - - // Exponential Distribution. - int Exponential(float lambda); - - // TODO(solenberg): Random from histogram. - // template int Distribution(const std::vector histogram) { - - private: - uint32_t a_; - uint32_t b_; - - RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); -}; - bool IsTimeSorted(const Packets& packets); class PacketProcessor; @@ -291,7 +265,7 @@ class LossFilter : public PacketProcessor { virtual void RunFor(int64_t time_ms, Packets* in_out); private: - Random random_; + test::Random random_; float loss_fraction_; RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(LossFilter); @@ -325,7 +299,7 @@ class JitterFilter : public PacketProcessor { int64_t MeanUs(); private: - Random random_; + test::Random random_; int64_t stddev_jitter_us_; int64_t last_send_time_us_; bool reordering_; // False by default. @@ -344,7 +318,7 @@ class ReorderFilter : public PacketProcessor { virtual void RunFor(int64_t time_ms, Packets* in_out); private: - Random random_; + test::Random random_; float reorder_fraction_; RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(ReorderFilter); diff --git a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework_unittest.cc b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework_unittest.cc index 6cd6ee7efe..627260678b 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework_unittest.cc +++ b/webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework_unittest.cc @@ -30,7 +30,7 @@ TEST(BweTestFramework_RandomTest, Gaussian) { kStddev = 10 }; - Random random(0x12345678); + test::Random random(0x12345678); int buckets[kBuckets] = {0}; for (int i = 0; i < kN; ++i) { diff --git a/webrtc/modules/remote_bitrate_estimator/test/random.cc b/webrtc/test/random.cc similarity index 77% rename from webrtc/modules/remote_bitrate_estimator/test/random.cc rename to webrtc/test/random.cc index d803be0636..8877ed4c20 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/random.cc +++ b/webrtc/test/random.cc @@ -8,12 +8,16 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "webrtc/modules/remote_bitrate_estimator/test/random.h" +#include "webrtc/test/random.h" #include +#include "webrtc/base/checks.h" + namespace webrtc { +namespace test { + Random::Random(uint32_t seed) : a_(0x531FDB97 ^ seed), b_(0x6420ECA8 + seed) { } @@ -25,6 +29,12 @@ float Random::Rand() { return result; } +int Random::Rand(int low, int high) { + RTC_DCHECK(low <= high); + float uniform = Rand() * (high - low + 1) + low; + return static_cast(uniform); +} + int Random::Gaussian(int mean, int standard_deviation) { // Creating a Normal distribution variable from two independent uniform // variables based on the Box-Muller transform, which is defined on the @@ -38,4 +48,10 @@ int Random::Gaussian(int mean, int standard_deviation) { return static_cast( mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2)); } + +int Random::Exponential(float lambda) { + float uniform = Rand(); + return static_cast(-log(uniform) / lambda); +} +} // namespace test } // namespace webrtc diff --git a/webrtc/modules/remote_bitrate_estimator/test/random.h b/webrtc/test/random.h similarity index 76% rename from webrtc/modules/remote_bitrate_estimator/test/random.h rename to webrtc/test/random.h index 31c1ec142e..9a0bc9e953 100644 --- a/webrtc/modules/remote_bitrate_estimator/test/random.h +++ b/webrtc/test/random.h @@ -8,14 +8,16 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_RANDOM_H_ -#define WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_RANDOM_H_ +#ifndef WEBRTC_TEST_RANDOM_H_ +#define WEBRTC_TEST_RANDOM_H_ #include "webrtc/typedefs.h" #include "webrtc/base/constructormagic.h" namespace webrtc { +namespace test { + class Random { public: explicit Random(uint32_t seed); @@ -23,9 +25,15 @@ class Random { // Return pseudo-random number in the interval [0.0, 1.0]. float Rand(); + // Return pseudo rounded random number in interval [low, high]. + int Rand(int low, int high); + // Normal Distribution. int Gaussian(int mean, int standard_deviation); + // Exponential Distribution. + int Exponential(float lambda); + // TODO(solenberg): Random from histogram. // template int Distribution(const std::vector histogram) { @@ -35,6 +43,7 @@ class Random { RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Random); }; +} // namespace test } // namespace webrtc -#endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_TEST_RANDOM_H_ +#endif // WEBRTC_TEST_RANDOM_H_ diff --git a/webrtc/test/webrtc_test_common.gyp b/webrtc/test/webrtc_test_common.gyp index 17f4551113..f8d33658c4 100644 --- a/webrtc/test/webrtc_test_common.gyp +++ b/webrtc/test/webrtc_test_common.gyp @@ -37,6 +37,8 @@ 'mock_transport.h', 'null_transport.cc', 'null_transport.h', + 'random.cc', + 'random.h', 'rtp_rtcp_observer.h', 'run_loop.cc', 'run_loop.h',