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

be.bagofwords.application.status.perf.ThreadSamplesPrinter Maven / Gradle / Ivy

Go to download

Utility classes that are used in the count-db project and other bow-* projects

There is a newer version: 1.2.0
Show newest version
package be.bagofwords.application.status.perf;

import be.bagofwords.counts.Counter;
import be.bagofwords.util.NumUtils;

import java.util.ArrayList;
import java.util.List;

public class ThreadSamplesPrinter {

    private static final double MIN_FRACTION = 0.001;

    public static void printTopTraces(StringBuilder result, Counter traces) {
        List sortedTraces = new ArrayList<>(traces.sortedKeys());
        double total = 0;
        for (Trace trace : sortedTraces) {
            if (trace.getParent() == null) {
                total += traces.get(trace);
            }
        }
        for (int i = 0; i < sortedTraces.size(); i++) {
            Trace parentTrace = sortedTraces.get(i);
            if (parentTrace.getParent() == null) {
                printTrace(0, "", result, parentTrace, traces, total, sortedTraces, false);
            }
        }
    }

    private static void printTrace(int level, String combinedIndentation, StringBuilder result, Trace parentTrace, Counter traces, double total, List sortedTraces, boolean printHorizontalLine) {
        double fraction = traces.get(parentTrace) / total;
        if (fraction > MIN_FRACTION) {
            String indentation = combinedIndentation + (printHorizontalLine ? "\\" : " ");
            int numOfChildren = countNumberOfChildren(parentTrace, traces, sortedTraces, total);
            result.append(indentation + NumUtils.makeNicePercent(fraction) + "% " + parentTrace.getLine() + "\n");
            //Add subtraces
            int numOfChildrenPrinted = 0;
            for (Trace subtrace : sortedTraces) {
                if (subtrace.getParent() != null && subtrace.getParent().equals(parentTrace)) {
                    char trackingLine = level % 2 == 0 ? '|' : '!';
                    printTrace(level + 1, combinedIndentation + " " + (numOfChildrenPrinted < numOfChildren - 1 ? " " + trackingLine : "  "), result, subtrace, traces, total, sortedTraces, numOfChildren > 0);
                    numOfChildrenPrinted++;
                }
            }
        }
    }

    private static int countNumberOfChildren(Trace parentTrace, Counter traces, List sortedTraces, double total) {
        int numOfChildren = 0;
        for (Trace subtrace : sortedTraces) {
            if (subtrace.getParent() != null && subtrace.getParent().equals(parentTrace)) {
                double fraction = traces.get(subtrace) / total;
                if (fraction > MIN_FRACTION) {
                    numOfChildren++;
                }
            }
        }
        return numOfChildren;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy