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

io.github.linyimin0812.profiler.common.ui.StartupVO Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package io.github.linyimin0812.profiler.common.ui;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.github.linyimin0812.profiler.common.logger.LogFactory;
import io.github.linyimin0812.profiler.common.logger.Logger;
import io.github.linyimin0812.profiler.common.utils.GsonUtil;

import java.util.*;
import java.util.stream.Collectors;

import static java.lang.reflect.Modifier.TRANSIENT;

/**
 * @author linyimin
 **/
public class StartupVO {

    private static final Logger logger = LogFactory.getStartupLogger();

    private static final List beanInitResultList = new ArrayList<>();
    private static final List statisticsList = new ArrayList<>();
    private static final Map> unusedJarMap = new HashMap<>();
    private static final List methodInvokeDetailList = new ArrayList<>();

    private static final Gson GSON = GsonUtil.create();

    public static void addBeanInitResult(BeanInitResult beanInitResult) {
        beanInitResultList.add(beanInitResult);
    }

    public static void addStatistics(Statistics statistics) {
        statisticsList.add(statistics);
    }

    public static void addUnusedJar(Map.Entry> entry) {
        unusedJarMap.put(entry.getKey().toString(), entry.getValue());
    }

    public static void addMethodInvokeDetail(MethodInvokeDetail invokeDetail) {
        methodInvokeDetailList.add(invokeDetail);
    }

    public static List getBeanInitResultList() {
        return beanInitResultList;
    }

    public static List getStatisticsList() {
        return statisticsList;
    }

    public static String toJSONString() {
        Map map = new HashMap<>();



        map.put("statisticsList", GSON.toJson(statisticsList, new TypeToken>(){}.getType()));
        map.put("beanInitResultList", GSON.toJson(beanInitResultList));
        map.put("unusedJarMap", GSON.toJson(unusedJarMap));
        map.put("methodInvokeDetailList", GSON.toJson(calculateInvokeMetrics()));

        // fix Use JSONObject#toJSONString to serialize a Map. The Map contains a large string and OOM appears
        return GSON.toJson(map);
    }

    private static List calculateInvokeMetrics() {

        List metricsList = new ArrayList<>();

        try {
            Map> invokeMap = methodInvokeDetailList.stream().collect(Collectors.groupingBy(MethodInvokeDetail::getMethodQualifier));

            for (Map.Entry> entry : invokeMap.entrySet()) {

                long totalCost = entry.getValue().stream().mapToLong(MethodInvokeDetail::getDuration).sum();
                double averageCost = totalCost / (1.0 * entry.getValue().size());
                List top100 = entry.getValue().stream().sorted((o1, o2) -> (int) (o2.getDuration() - o1.getDuration())).limit(100).collect(Collectors.toList());

                metricsList.add(new MethodInvokeMetrics(entry.getKey(), entry.getValue().size(), totalCost, averageCost, top100));

            }
        } catch (Exception ex) {
            List copies = methodInvokeDetailList.stream().map(invokeDetail -> new MethodInvokeDetail(invokeDetail.getMethodQualifier(), invokeDetail.getStartMillis(), invokeDetail.getDuration())).collect(Collectors.toList());
            logger.error(StartupVO.class, "calculateInvokeMetrics error. methodInvokeDetailList: {}", GSON.toJson(copies), ex);
        }

        return metricsList;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy