All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dabogadog.utils.ManageCpuData Maven / Gradle / Ivy

package io.github.dabogadog.utils;

import com.google.gson.reflect.TypeToken;
import io.github.dabogadog.models.CpuMem.ConsolidadoTestCpuMem;
import io.github.dabogadog.models.CpuMem.Cpu;
import io.restassured.RestAssured;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static io.github.dabogadog.httpUtils.ApiConfig.BASE_URL;

public class ManageCpuData {
    private static Logger logger = Logger.getLogger(ManageCpuData.class.getName());

    public static ConsolidadoTestCpuMem getCpuMetrics(String[] sessionIdsArray, String type) {
        List listaConsolidadoTestCpuMem = getCpuMemMetricsFromEachTest(sessionIdsArray, type);
        return getConsolidatedCpuMemMetrics(listaConsolidadoTestCpuMem);
    }

    public static ConsolidadoTestCpuMem getMemMetrics(String[] sessionIdsArray, String type) {
        List listaConsolidadoTestCpuMem = getCpuMemMetricsFromEachTest(sessionIdsArray, type);
        return getConsolidatedCpuMemMetrics(listaConsolidadoTestCpuMem);
    }

    public static List getCpuMemMetricsFromEachTest(String[] sessionIdList, String type) {
        List listaConsolidadoTestCpuMem = new ArrayList<>();

        for (String sessionID : sessionIdList) {
            logger.log(Level.INFO, "Getting Memory And Cpu data");
            List listCpuMem = getAllCpuMemDataFromBuild(sessionID, type);
            ConsolidadoTestCpuMem metricsFromEachTest = getMetricForEachTest(listCpuMem);
            listaConsolidadoTestCpuMem.add(metricsFromEachTest);
        }
        logger.log(Level.INFO, "Cpu done");

        return listaConsolidadoTestCpuMem;
    }

    public static ConsolidadoTestCpuMem getMetricForEachTest(List listCpuMem) {

        return ConsolidadoTestCpuMem
                .builder()
                .sysMax(getSysMax(listCpuMem))
                .sysMin(getSysMin(listCpuMem))
                .sysAvg(getSysAvg(listCpuMem))
                .appMax(getAppMax(listCpuMem))
                .appMin(getAppMin(listCpuMem))
                .appAvg(getAppAvg(listCpuMem))
                .build();
    }

    public static ConsolidadoTestCpuMem getConsolidatedCpuMemMetrics(List listaConsolidadoTest) {
        double appMax = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.appMax)
                .max().orElse(0.0);
        double appMin = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.appMin)
                .max().orElse(0.0);
        double appAvg = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.appAvg)
                .max().orElse(0.0);
        double sysMax = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.sysMax)
                .max().orElse(0.0);
        double sysMin = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.sysMin)
                .max().orElse(0.0);
        double sysAvg = listaConsolidadoTest.stream()
                .mapToDouble(e -> e.sysAvg)
                .max().orElse(0.0);

        return ConsolidadoTestCpuMem.builder()
                .sysMax(sysMax)
                .sysMin(sysMin)
                .sysAvg(sysAvg)
                .appMax(appMax)
                .appMin(appMin)
                .appAvg(appAvg)
                .build();
    }


    public static List getAllCpuMemDataFromBuild(String sessionID, String type) {
        Type listType = new TypeToken>() {
        }.getType();
        return RestAssured.given().when().get(BASE_URL + sessionID + "/log/appprofile?metricstype=" + type)
                .then().extract().as(listType);
    }
    public static double getAppMax(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.app)
                .max()
                .orElse(0.0);
    }

    public static double getAppMin(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.app)
                .filter(e -> e != 0.0)
                .min()
                .orElse(0.0);
    }

    public static double getAppAvg(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.app)
                .average()
                .orElse(0.0);
    }

    public static double getSysMax(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.sys)
                .max()
                .orElse(0.0);
    }

    public static double getSysMin(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.sys)
                .filter(e -> e != 0.0)
                .min()
                .orElse(0.0);
    }

    public static double getSysAvg(List listCpu) {
        return listCpu.stream()
                .mapToDouble(e -> e.data.sys)
                .average()
                .orElse(0.0);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy