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

org.elasticsearch.search.profile.AbstractProfileBreakdown Maven / Gradle / Ivy

There is a newer version: 9.0.0-beta1
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.search.profile;

import org.elasticsearch.common.util.Maps;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;

/**
 * A record of timings for the various operations that may happen during query execution.
 * A node's time may be composed of several internal attributes (rewriting, weighting,
 * scoring, etc).
 */
public abstract class AbstractProfileBreakdown> {

    /**
     * The accumulated timings for this query node
     */
    private final Map> timings;

    /** Sole constructor. */
    public AbstractProfileBreakdown(Class clazz) {
        T[] enumConstants = clazz.getEnumConstants();
        timings = new HashMap<>(enumConstants.length, 1.0f);
        for (int i = 0; i < enumConstants.length; ++i) {
            Collection listOfTimers = Collections.synchronizedCollection(new ArrayList<>());
            timings.put(enumConstants[i], listOfTimers);
        }
    }

    /**
     * @param timingType the timing type to create a new {@link Timer} for
     * @return a new {@link Timer} instance
     */
    public Timer getNewTimer(T timingType) {
        Timer timer = new Timer();
        timings.get(timingType).add(timer);
        return timer;
    }

    /**
     * Build a timing count breakdown.
     * If multiple timers where requested from different locations or threads from this profile breakdown,
     * the approximation will contain the sum of each timers approximate time and count.
     */
    public final Map toBreakdownMap() {
        Map map = Maps.newMapWithExpectedSize(timings.keySet().size() * 2);
        this.timings.forEach((timingType, timers) -> {
            map.put(timingType.toString(), timers.stream().map(Timer::getApproximateTiming).mapToLong(Long::valueOf).sum());
            map.put(timingType + "_count", timers.stream().map(Timer::getCount).mapToLong(Long::valueOf).sum());
        });
        return Collections.unmodifiableMap(map);
    }

    /**
     * Fetch extra debugging information.
     */
    protected Map toDebugMap() {
        return emptyMap();
    }

    /**
     * @return the total sum of timers approximate times across all timing types
     */
    public final long toNodeTime() {
        long total = 0;
        for (Collection timings : timings.values()) {
            total += timings.stream().map(Timer::getApproximateTiming).mapToLong(Long::valueOf).sum();
        }
        return total;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy