From 95a15c7b9db39af34dfcb71dc677c3f6a400a060 Mon Sep 17 00:00:00 2001 From: mazepeng <2695212419@qq.com> Date: Tue, 5 Sep 2023 10:27:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=20=E6=B7=BB=E5=8A=A0=E4=BA=86CPU?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=8E=87=E6=A3=80=E6=9F=A5=E5=92=8C=E5=86=85?= =?UTF-8?q?=E5=AD=98=E4=BD=BF=E7=94=A8=E7=8E=87=E6=A3=80=E6=9F=A5=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fallback/checker/CpuUsageCheckerTest.java | 48 +++++++++++++++++++ .../fallback/checker/MemUsageCheckerTest.java | 46 ++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java create mode 100644 dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java diff --git a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java new file mode 100644 index 000000000..c7626ce9b --- /dev/null +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java @@ -0,0 +1,48 @@ +package io.dongtai.iast.agent.fallback.checker; + +import com.alibaba.fastjson2.JSONObject; +import io.dongtai.iast.agent.IastProperties; +import io.dongtai.iast.agent.fallback.checker.impl.CpuUsageChecker; +import io.dongtai.iast.common.entity.performance.PerformanceMetrics; +import io.dongtai.iast.common.entity.performance.metrics.CpuInfoMetrics; +import io.dongtai.iast.common.enums.MetricsKey; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Properties; + +/** + * 检查CPU使用率 + */ +public class CpuUsageCheckerTest { + + @Test + public void testIsPerformanceOverLimit() { + + + // 创建配置参数对象 + Properties cfg = new Properties(); + cfg.setProperty("iast.remoteSync.performanceLimit.maxThreshold.cpuUsage", "{\"cpuUsagePercentage\":80.0}"); + + //初始化临时目录 + IastProperties.initTmpDir(); + + // 创建测试用例对象 + CpuUsageChecker cpuUsageChecker = new CpuUsageChecker(); + // 创建模拟性能指标对象 + PerformanceMetrics nowMetrics = new PerformanceMetrics(); + CpuInfoMetrics cpuInfoMetrics = new CpuInfoMetrics(); + cpuInfoMetrics.setCpuUsagePercentage(81.0); + + nowMetrics.setMetricsKey(MetricsKey.CPU_USAGE); + nowMetrics.setMetricsValue(cpuInfoMetrics); + + // CPU使用率超过阈值,应该返回true + Assert.assertTrue(cpuUsageChecker.isPerformanceOverLimit(nowMetrics, cfg)); + + // 修改性能指标对象的CPU使用率为70% + cpuInfoMetrics.setCpuUsagePercentage(70.0); + // CPU使用率未超过阈值,应该返回false + Assert.assertFalse(cpuUsageChecker.isPerformanceOverLimit(nowMetrics, cfg)); + } +} diff --git a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java new file mode 100644 index 000000000..faf8dca75 --- /dev/null +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java @@ -0,0 +1,46 @@ +package io.dongtai.iast.agent.fallback.checker; + +import com.alibaba.fastjson2.JSONObject; +import io.dongtai.iast.agent.IastProperties; +import io.dongtai.iast.agent.fallback.FallbackConfig; +import io.dongtai.iast.agent.fallback.checker.impl.MemUsageChecker; +import io.dongtai.iast.common.entity.performance.PerformanceMetrics; +import io.dongtai.iast.common.entity.performance.metrics.MemoryUsageMetrics; +import io.dongtai.iast.common.enums.MetricsKey; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Properties; + +public class MemUsageCheckerTest { + + + /** + * 测试内存检查 + */ + @Test + public void testIsPerformanceOverLimit() { + // 配置参数 + Properties cfg = new Properties(); + // 设置配置内存阈值 + cfg.setProperty("iast.remoteSync.performanceLimit.maxThreshold.memoryUsage", "{\"committed\":1024," + + "\"init\":1024,\"max\":1024,\"memUsagePercentage\":80.0,\"systemMaxLimit\":-1," + + "\"trulyMaxMem\":1024,\"used\":1024}\n"); + //初始化临时目录 + IastProperties.initTmpDir(); + // 创建检查器对象 + MemUsageChecker memUsageChecker = new MemUsageChecker(); + // 创建模拟性能指标对象 + PerformanceMetrics nowMetrics = new PerformanceMetrics(); + nowMetrics.setMetricsKey(MetricsKey.MEM_USAGE); + nowMetrics.setMetricsValue(new MemoryUsageMetrics(1024L, 1024L, 1024L, 1024L)); + // 内存使用率超过阈值,应该返回true + Assert.assertTrue(memUsageChecker.isPerformanceOverLimit(nowMetrics, cfg)); + + + // 修改性能指标对象的内存使用率为70% + nowMetrics.setMetricsValue(new MemoryUsageMetrics(1024L, 500L, 1024L, 1024L)); + // 内存使用率未超过阈值,应该返回false + Assert.assertFalse(memUsageChecker.isPerformanceOverLimit(nowMetrics, cfg)); + } +} From 0e6b5e63567a3b10ef68f8c92860f25a0ffb151a Mon Sep 17 00:00:00 2001 From: mazepeng <2695212419@qq.com> Date: Tue, 5 Sep 2023 10:32:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=20=E6=B7=BB=E5=8A=A0=E4=BA=86CPU?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=8E=87=E6=A3=80=E6=9F=A5=E5=92=8C=E5=86=85?= =?UTF-8?q?=E5=AD=98=E4=BD=BF=E7=94=A8=E7=8E=87=E6=A3=80=E6=9F=A5=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iast/agent/fallback/checker/CpuUsageCheckerTest.java | 1 - .../iast/agent/fallback/checker/MemUsageCheckerTest.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java index c7626ce9b..2dc05c253 100644 --- a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java @@ -1,6 +1,5 @@ package io.dongtai.iast.agent.fallback.checker; -import com.alibaba.fastjson2.JSONObject; import io.dongtai.iast.agent.IastProperties; import io.dongtai.iast.agent.fallback.checker.impl.CpuUsageChecker; import io.dongtai.iast.common.entity.performance.PerformanceMetrics; diff --git a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java index faf8dca75..9eb6e0df1 100644 --- a/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java @@ -1,8 +1,6 @@ package io.dongtai.iast.agent.fallback.checker; -import com.alibaba.fastjson2.JSONObject; import io.dongtai.iast.agent.IastProperties; -import io.dongtai.iast.agent.fallback.FallbackConfig; import io.dongtai.iast.agent.fallback.checker.impl.MemUsageChecker; import io.dongtai.iast.common.entity.performance.PerformanceMetrics; import io.dongtai.iast.common.entity.performance.metrics.MemoryUsageMetrics;