Add ability to specify random seed when creating built it network emulation

Bug: webrtc:12340
Change-Id: Iffd054928249099866ef4527b911b1e358e26f5a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/200805
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32920}
This commit is contained in:
Artem Titov 2021-01-07 15:49:31 +01:00 committed by Commit Bot
parent 82e1875c7d
commit ec9b281bbc
5 changed files with 25 additions and 13 deletions

View File

@ -56,18 +56,20 @@ NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length(
}
NetworkEmulationManager::SimulatedNetworkNode
NetworkEmulationManager::SimulatedNetworkNode::Builder::Build() const {
NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
uint64_t random_seed) const {
RTC_CHECK(net_);
return Build(net_);
return Build(net_, random_seed);
}
NetworkEmulationManager::SimulatedNetworkNode
NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
NetworkEmulationManager* net) const {
NetworkEmulationManager* net,
uint64_t random_seed) const {
RTC_CHECK(net);
RTC_CHECK(net_ == nullptr || net_ == net);
SimulatedNetworkNode res;
auto behavior = std::make_unique<SimulatedNetwork>(config_);
auto behavior = std::make_unique<SimulatedNetwork>(config_, random_seed);
res.simulation = behavior.get();
res.node = net->CreateEmulatedNode(std::move(behavior));
return res;

View File

@ -152,8 +152,9 @@ class NetworkEmulationManager {
Builder& capacity_Mbps(int link_capacity_Mbps);
Builder& loss(double loss_rate);
Builder& packet_queue_length(int max_queue_length_in_packets);
SimulatedNetworkNode Build() const;
SimulatedNetworkNode Build(NetworkEmulationManager* net) const;
SimulatedNetworkNode Build(uint64_t random_seed = 1) const;
SimulatedNetworkNode Build(NetworkEmulationManager* net,
uint64_t random_seed = 1) const;
private:
NetworkEmulationManager* const net_;
@ -165,9 +166,15 @@ class NetworkEmulationManager {
virtual TimeController* time_controller() = 0;
// Creates an emulated network node, which represents single network in
// the emulated network layer.
// the emulated network layer. Uses default implementation on network behavior
// which can be configured with |config|. |random_seed| can be provided to
// alter randomization behavior.
virtual EmulatedNetworkNode* CreateEmulatedNode(
BuiltInNetworkBehaviorConfig config) = 0;
BuiltInNetworkBehaviorConfig config,
uint64_t random_seed = 1) = 0;
// Creates an emulated network node, which represents single network in
// the emulated network layer. |network_behavior| determines how created node
// will forward incoming packets to the next receiver.
virtual EmulatedNetworkNode* CreateEmulatedNode(
std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;

View File

@ -66,7 +66,8 @@ class Random {
double Exponential(double lambda);
private:
// Outputs a nonzero 64-bit random number.
// Outputs a nonzero 64-bit random number using Xorshift algorithm.
// https://en.wikipedia.org/wiki/Xorshift
uint64_t NextOutput() {
state_ ^= state_ >> 12;
state_ ^= state_ << 25;

View File

@ -63,8 +63,10 @@ NetworkEmulationManagerImpl::~NetworkEmulationManagerImpl() {
}
EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode(
BuiltInNetworkBehaviorConfig config) {
return CreateEmulatedNode(std::make_unique<SimulatedNetwork>(config));
BuiltInNetworkBehaviorConfig config,
uint64_t random_seed) {
return CreateEmulatedNode(
std::make_unique<SimulatedNetwork>(config, random_seed));
}
EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode(

View File

@ -44,8 +44,8 @@ class NetworkEmulationManagerImpl : public NetworkEmulationManager {
explicit NetworkEmulationManagerImpl(TimeMode mode);
~NetworkEmulationManagerImpl();
EmulatedNetworkNode* CreateEmulatedNode(
BuiltInNetworkBehaviorConfig config) override;
EmulatedNetworkNode* CreateEmulatedNode(BuiltInNetworkBehaviorConfig config,
uint64_t random_seed = 1) override;
EmulatedNetworkNode* CreateEmulatedNode(
std::unique_ptr<NetworkBehaviorInterface> network_behavior) override;