-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...s-testing-features-api/src/main/java/com/aws/greengrass/testing/features/SystemSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.testing.features; | ||
|
||
import io.cucumber.guice.ScenarioScoped; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import oshi.SystemInfo; | ||
import oshi.hardware.CentralProcessor; | ||
import oshi.hardware.GlobalMemory; | ||
import oshi.hardware.HardwareAbstractionLayer; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
@ScenarioScoped | ||
public class SystemSteps { | ||
private static final Logger LOGGER = LogManager.getLogger(SystemSteps.class); | ||
private ArrayList<Double> ramList = new ArrayList<Double>(); | ||
private ArrayList<Double> cpuList = new ArrayList<Double>(); | ||
private SystemInfo si = new SystemInfo(); | ||
private HardwareAbstractionLayer hal = si.getHardware(); | ||
private CentralProcessor cpu = hal.getProcessor(); | ||
private GlobalMemory ram = hal.getMemory(); | ||
|
||
/** | ||
* Record the current CPU or RAM usage depending on the step. | ||
* | ||
* @param stat the statistic to record, must be CPU or RAM | ||
* @throws IllegalArgumentException when parameter is not CPU or RAM | ||
*/ | ||
@When("I record the device's {word} usage statistic") | ||
public void recordCpuOrRam(String stat) throws IllegalArgumentException { | ||
switch (stat) { | ||
case "CPU": | ||
double cpuLoad = cpu.getSystemCpuLoad(500) * 100; | ||
LOGGER.info("System CPU load recorded as: " + cpuLoad + "%"); | ||
cpuList.add(0, cpuLoad); | ||
break; | ||
case "RAM": | ||
double usedRam = (ram.getTotal() - ram.getAvailable()) / (1024 * 1024); | ||
LOGGER.info("Used RAM recorded as: " + usedRam + " MB"); | ||
ramList.add(0, usedRam); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Please specify either CPU or RAM to track"); | ||
} | ||
} | ||
|
||
/** | ||
* Check difference between last two records and verify it is below a threshold. | ||
* | ||
* @param stat the statistic to record, must be CPU or RAM | ||
* @param threshold the threshold to assert the difference is under, % for CPU and MB for RAM | ||
* @throws Exception when step fails due to exceeding the provided threshold or sample steps haven't run twice | ||
* @throws IllegalArgumentException when stat param is not CPU or RAM | ||
*/ | ||
@Then("the difference in the last two {word} samples is less than {int}") | ||
public void checkSampleDiff(String stat, int threshold) throws Exception, IllegalArgumentException { | ||
switch (stat) { | ||
case "CPU": | ||
if (cpuList.size() < 2) { | ||
throw new Exception("Need at least two CPU samples first."); | ||
} | ||
double cpuDiff = cpuList.get(0) - cpuList.get(1); | ||
if (cpuDiff >= threshold) { | ||
throw new Exception("CPU use was above the provided threshold."); | ||
} | ||
break; | ||
case "RAM": | ||
if (ramList.size() < 2) { | ||
throw new Exception("Need at least two RAM samples first."); | ||
} | ||
double ramDiff = ramList.get(0) - ramList.get(1); | ||
if (ramDiff >= threshold) { | ||
throw new Exception("RAM use was above the provided threshold"); | ||
} | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Please specify either CPU or RAM to check"); | ||
} | ||
} | ||
} |