Commit 254c58e1 authored by wutu's avatar wutu

更新TC流控接口

parent 4df52336
......@@ -5,6 +5,8 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
......@@ -29,6 +31,7 @@ import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class InDockerTests {
private static final Logger LOG = LoggerFactory.getLogger(InDockerTests.class);
@Autowired
DockerService dockerService;
......@@ -125,6 +128,44 @@ public class InDockerTests {
System.out.println(result);
}
@Test
public void testTcDelay() {
// 查看未加限制的网络情况
LOG.info("未加任何限制的网络:\n" + pingTest());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc add dev eth1 root netem delay 200ms");
LOG.info("添加200ms固定延迟的延迟:\n" + pingTest());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc change dev eth1 root netem delay 100ms 10ms");
LOG.info("添加100ms延时, 并添加10ms的随机延迟" + pingTest());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc change dev eth1 root netem delay 100ms 10ms 25%");
LOG.info("添加100ms延时, 并添加10ms的随机延迟, 25%相关性" + pingTest());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc change dev eth1 netem delay 100ms 20ms distribution normal");
LOG.info("正态分布" + pingTest());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc change dev eth1 root netem loss 10%");
LOG.info("10%丢包率:" + pingTest());
}
@Test
public void testTcQos() {
// 测试网络带宽
LOG.info("为做任何限制的网络:\n" + basicQos());
dockerService.execInDocker(dockerContainer1.getId(), "tc qdisc add dev eth1 root tbf rate 1mbit burst 32kbit latency 400ms");
LOG.info("限制带宽为1mbit的网络:\n" + basicQos());
}
public String basicQos() {
String result = dockerService.execInDocker(dockerContainer2.getId(), "iperf -c 10.1.100.2 -i -t 2".split(" "));
return result;
}
public String pingTest() {
return dockerService.execInDocker(dockerContainer1.getId(), "ping 10.1.100.3 -c 100");
}
@Test
public void testParseIptablesDetail() {
/**
......
......@@ -7,4 +7,15 @@ package top.ninwoo.utils.util;
*/
public interface TcUtils {
boolean addQos(String containerId, String maxRate, String minRate);
boolean delQos(String containerId);
boolean addDelay(String containerId, int delay);
boolean addDelay(String containerId, int delay, int randomDelay);
boolean addDelay(String containerId, int delay, int randomDelay, int correlation);
boolean addDelay(String contianerId, int delay, int randomDelay, String policy);
}
......@@ -4,6 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import top.ninwoo.utils.util.DockerUtils;
import top.ninwoo.utils.util.TcUtils;
import java.util.HashSet;
import java.util.Set;
/**
* @Author joliu
* @Description
......@@ -14,34 +17,37 @@ public class TcUtilsImpl implements TcUtils {
@Autowired
DockerUtils dockerUtils;
Set<String> supportPolicies = new HashSet<>();
{
supportPolicies.add("distribution");
}
/**
* 这是个
* latency
* @param containerId
* @param maxRate
* @param minRate
* @param maxRate 1mbit
* @param latency 500ms 超过这个阈值的将被直接丢弃
* @return
*/
public boolean addQos(String containerId, String maxRate, String minRate) {
String resul = "";
// 开启限速
dockerUtils.execInDocker(containerId, "tc qdisc add dev eth1 root handle 1: htb default 20");
// 下行总速率5Mbit
dockerUtils.execInDocker(containerId, "tc class add dev eth1 parent 1:0 classid 1:1 htb rate " + maxRate);
// 1 号通道的下行速度,最小3Mbit 最大5 Mbit (还可以添加优先级)
dockerUtils.execInDocker(containerId, "tc class add dev eth1 parent 1:1 classid 1:20 htb rate " + minRate + " ceil " + maxRate);
// 使用了一个公平侧率
dockerUtils.execInDocker(containerId, "tc qdisc add dev eth1 parent 1:20 handle 20: sfq perturb 10");
// 设置了一个过滤器--TODO: 这个好像没有生效
dockerUtils.execInDocker(containerId, "tc filter add dev eth1 parent 1:20 protocol ip u32 match ip sport 5002 0xffff classid 1:20");
@Override
public boolean addQos(String containerId, String maxRate, String latency) {
String res = dockerUtils.execInDocker(containerId, "tc qdisc add dev eth1 root tbf rate " + maxRate + " burst 32kbit latency " + latency);
if(!"".equals(res)) {
return false;
}
return true;
}
/**
* 解除限速
* @param containerId
* @return
*/
@Override
public boolean delQos(String containerId) {
String res = dockerUtils.execInDocker(containerId, "tc qdisc del dev eth1 root");
if(!"".equals(res)) {
......@@ -49,4 +55,61 @@ public class TcUtilsImpl implements TcUtils {
}
return true;
}
@Override
public boolean addDelay(String containerId, int delay) {
return addDelay(containerId, delay, 0, 0);
}
@Override
public boolean addDelay(String containerId, int delay, int randomDelay) {
return addDelay(containerId, delay, randomDelay, 0);
}
/**
* 添加时延
* @param containerId
* @param delay 添加的延迟
* @param randomDelay 随机时延范围
* @param correlation 相关系数
* @return
*/
@Override
public boolean addDelay(String containerId, int delay, int randomDelay, int correlation) {
String cmd = "tc qdisc add dev eth1 root netem delay " + delay + "ms ";
if(randomDelay > 0) {
cmd += randomDelay + "ms ";
}
if(correlation >0 && correlation < 100) {
cmd += correlation + "%";
}
String res = dockerUtils.execInDocker(containerId, cmd);
if("".equals(res)) {
return true;
}
return false;
}
/**
* 正太分布的随机延时
* @param contianerId
* @param delay
* @param randomDelay
* @param policy distribution是目前支持的
* @return
*/
@Override
public boolean addDelay(String contianerId, int delay, int randomDelay, String policy) {
if(!supportPolicies.contains(policy)) {
return false;
}
String cmd = "tc qdisc change dev eth1 netem delay " + delay + "ms " + randomDelay + "ms " + policy + " normal";
String res = dockerUtils.execInDocker(contianerId, cmd);
if("".equals(res)) {
return true;
}
return false;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment