介绍
为了模拟弱网环境,在测试的时候需要使用弱网工具制造各种各样的弱网场景。常见的弱网工具有TC等。toy_rtc里面简单的实现了一个弱网模拟器。
包含的功能
- 模拟丢包
- 模拟延迟
- 模拟抖动
- 模拟带宽限制
实现
随机数产生器
#ifndef RANDOMGENERATOR_H
#define RANDOMGENERATOR_H
/*
* 随机数产生器
*/
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
#include <time.h>
class RandomGenerator{
public:
enum{
kNormalDistribution,
kRandomDistribution ,
};
RandomGenerator(){
this->mode = kNormalDistribution;
SetNormalDistributionParam(700,300);
SetRandomDistributionParam(400,1000);
}
void SetMode(int mode){
this->mode = mode;
}
void SetNormalDistributionParam(int avg,int mse){
engine tmp_gen(GetSeed());
normal_gen = tmp_gen;
std::normal_distribution<double> tmp_normal(avg,mse);
normal_dist = tmp_normal;
}
void SetRandomDistributionParam(int low,int up){
engine tmp_gen(GetSeed());
random_gen = tmp_gen;
std::uniform_int_distribution<int> tmp_random(low,up);
random_dist = tmp_random;
}
int RandomNum(){
if(mode == kNormalDistribution){
return normal_dist(normal_gen);
}
else{
return random_dist(random_gen);
}
}
static void Test(int mode,int first,int second){
RandomGenerator g;
g.SetMode(mode);
g.SetNormalDistributionParam(first,second);
g.SetRandomDistributionParam(first,second);
if(mode == RandomGenerator::kNormalDistribution){
std::map<int, int> hist;
for(int n=0; n<10000; ++n) {
++hist[g.RandomNum()];
}
for(auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/10, '*') << '\n';
}
}
else{
for(int n=0; n<10; ++n)
std::cout << g.RandomNum() << ' ';
std::cout << '\n';
}
}
private:
int64_t GetSeed(){
return rd();
//return time(NULL);
}
// typedef std::default_random_engine engine ;
typedef std::mt19937 engine ;
private:
std::random_device rd; // 种子产生器
engine normal_gen; // 随机数引擎,用于产生随机数
engine random_gen;
std::normal_distribution<double> normal_dist; // 分布模型,让引擎产生的随机数符合某一个分布
std::uniform_int_distribution