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..2dc05c253 --- /dev/null +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/CpuUsageCheckerTest.java @@ -0,0 +1,47 @@ +package io.dongtai.iast.agent.fallback.checker; + +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..9eb6e0df1 --- /dev/null +++ b/dongtai-agent/src/test/java/io/dongtai/iast/agent/fallback/checker/MemUsageCheckerTest.java @@ -0,0 +1,44 @@ +package io.dongtai.iast.agent.fallback.checker; + +import io.dongtai.iast.agent.IastProperties; +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)); + } +}