Skip to content

Commit

Permalink
Merge pull request #581 from 15911075183ma/ma-beta
Browse files Browse the repository at this point in the history
feature:添加测试用例
  • Loading branch information
Nizernizer authored Sep 6, 2023
2 parents 276a7e5 + 0e6b5e6 commit 8ee4a41
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 8ee4a41

Please sign in to comment.