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

org.glowroot.agent.impl.MutableTimer Maven / Gradle / Ivy

There is a newer version: 0.9.28
Show newest version
/*
 * Copyright 2014-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.glowroot.agent.impl;

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

import org.glowroot.agent.shaded.google.common.collect.Lists;

import org.glowroot.agent.model.TimerImpl;
import org.glowroot.wire.api.model.AggregateOuterClass.Aggregate;

class MutableTimer {

    private final String name;
    private final boolean extended;
    // aggregates use double instead of long to avoid (unlikely) 292 year nanosecond rollover
    private double totalNanos;
    private long count;
    private final List childTimers;

    static MutableTimer createRootTimer(String name, boolean extended) {
        return new MutableTimer(name, extended, 0, 0, new ArrayList());
    }

    private MutableTimer(String name, boolean extended, double totalNanos, long count,
            List nestedTimers) {
        this.name = name;
        this.extended = extended;
        this.totalNanos = totalNanos;
        this.count = count;
        this.childTimers = Lists.newArrayList(nestedTimers);
    }

    String getName() {
        return name;
    }

    void merge(TimerImpl timer) {
        count += timer.getCount();
        totalNanos += timer.getTotalNanos();
        Iterator i = timer.getChildTimers();
        while (i.hasNext()) {
            TimerImpl toBeMergedChildTimer = i.next();
            String toBeMergedChildTimerName = toBeMergedChildTimer.getName();
            boolean extended = toBeMergedChildTimer.isExtended();
            MutableTimer matchingChildTimer = null;
            for (MutableTimer childTimer : childTimers) {
                if (toBeMergedChildTimerName.equals(childTimer.getName())
                        && extended == childTimer.extended) {
                    matchingChildTimer = childTimer;
                    break;
                }
            }
            if (matchingChildTimer == null) {
                matchingChildTimer = new MutableTimer(toBeMergedChildTimer.getName(),
                        toBeMergedChildTimer.isExtended(), 0, 0, new ArrayList());
                childTimers.add(matchingChildTimer);
            }
            matchingChildTimer.merge(toBeMergedChildTimer);
        }
    }

    Aggregate.Timer toProtobuf() {
        Aggregate.Timer.Builder builder = Aggregate.Timer.newBuilder()
                .setName(name)
                .setExtended(extended)
                .setTotalNanos(totalNanos)
                .setCount(count);
        for (MutableTimer childTimer : childTimers) {
            builder.addChildTimer(childTimer.toProtobuf());
        }
        return builder.build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy