Move PRNG from BWE test framework to webrtc/test.

BUG=

Review URL: https://codereview.webrtc.org/1404953002

Cr-Commit-Position: refs/heads/master@{#10285}
This commit is contained in:
solenberg
2015-10-15 05:10:30 -07:00
committed by Commit bot
parent 411d234987
commit 5bdddf91d3
9 changed files with 41 additions and 77 deletions

View File

@ -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<OveruseDetector> overuse_detector_;
rtc::scoped_ptr<OveruseEstimator> overuse_estimator_;
rtc::scoped_ptr<InterArrival> inter_arrival_;
Random random_;
test::Random random_;
};
TEST_F(OveruseDetectorTest, GaussianRandom) {

View File

@ -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',

View File

@ -947,7 +947,7 @@ std::vector<int> BweTest::GetFileSizesBytes(int num_files) {
const int kMinKbytes = 100;
const int kMaxKbytes = 1000;
Random random(0x12345678);
test::Random random(0x12345678);
std::vector<int> tcp_file_sizes_bytes;
while (num_files-- > 0) {
@ -960,7 +960,7 @@ std::vector<int> BweTest::GetFileSizesBytes(int num_files) {
std::vector<int64_t> 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<int64_t> tcp_starting_times_ms;

View File

@ -92,41 +92,6 @@ double RateCounter::BitrateWindowS() const {
return static_cast<double>(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<int>(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<int>(
mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2));
}
int Random::Exponential(float lambda) {
float uniform = Rand();
return static_cast<int>(-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);

View File

@ -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<typename T> 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<typename T> int Distribution(const std::vector<T> 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);

View File

@ -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) {

View File

@ -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 <math.h>
#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<int>(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<int>(
mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2));
}
int Random::Exponential(float lambda) {
float uniform = Rand();
return static_cast<int>(-log(uniform) / lambda);
}
} // namespace test
} // namespace webrtc

View File

@ -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<typename T> int Distribution(const std::vector<T> 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_

View File

@ -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',