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

org.apache.nifi.util.StopWatch Maven / Gradle / Ivy

Go to download

This nifi-utils module should be a general purpose place to store widely and generally useful functions that any component might want to leverage. NO DEPENDENCIES should be added. This module is likely to be leveraged by every extension and should not bring along any other dependencies. The only dependency intended is the nifi-api and even this is expected to be already provided in any case where it would be used. The typical place this util would be found is within a nar and all nars already have nifi-api as a parent dependency. The nifi-api can be thought of as a NiFi Application Container level dependency.

There is a newer version: 2.0.0-M4
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.nifi.util;

import java.util.concurrent.TimeUnit;

public final class StopWatch {

    private long startNanos = -1L;
    private long duration = -1L;

    /**
     * Creates a StopWatch but does not start it
     */
    public StopWatch() {
        this(false);
    }

    /**
     * @param autoStart whether or not the timer should be started automatically
     */
    public StopWatch(final boolean autoStart) {
        if (autoStart) {
            start();
        }
    }

    public void start() {
        this.startNanos = System.nanoTime();
        this.duration = -1L;
    }

    public void stop() {
        if (startNanos < 0) {
            throw new IllegalStateException("StopWatch has not been started");
        }
        this.duration = System.nanoTime() - startNanos;
        this.startNanos = -1L;
    }

    /**
     * Returns the amount of time that the StopWatch was running.
     *
     * @param timeUnit the unit for which the duration should be reported
     * @return the duration of the stopwatch in the specified unit
     *
     * @throws IllegalStateException if the StopWatch has not been stopped via {@link #stop()}
     */
    public long getDuration(final TimeUnit timeUnit) {
        if (duration < 0) {
            throw new IllegalStateException("Cannot get duration until StopWatch has been stopped");
        }
        return timeUnit.convert(duration, TimeUnit.NANOSECONDS);
    }

    /**
     * Returns the amount of time that has elapsed since the timer was started.
     *
     * @param timeUnit the unit for which the elapsed time should be computed
     * @return the elapsed time in the specified unit
     */
    public long getElapsed(final TimeUnit timeUnit) {
        return timeUnit.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
    }

    public String calculateDataRate(final long bytes) {
        final double seconds = (double) duration / 1000000000.0D;
        final long dataSize = (long) (bytes / seconds);
        return FormatUtils.formatDataSize(dataSize) + "/sec";
    }

    public String getDuration() {
        final StringBuilder sb = new StringBuilder();

        long duration = this.duration;
        final long minutes = (duration > 60000000000L) ? (duration / 60000000000L) : 0L;
        duration -= TimeUnit.NANOSECONDS.convert(minutes, TimeUnit.MINUTES);

        final long seconds = (duration > 1000000000L) ? (duration / 1000000000L) : 0L;
        duration -= TimeUnit.NANOSECONDS.convert(seconds, TimeUnit.SECONDS);

        final long millis = (duration > 1000000L) ? (duration / 1000000L) : 0L;
        duration -= TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS);

        final long nanos = duration % 1000000L;

        if (minutes > 0) {
            sb.append(minutes).append(" minutes");
        }

        if (seconds > 0) {
            if (minutes > 0) {
                sb.append(", ");
            }

            sb.append(seconds).append(" seconds");
        }

        if (millis > 0) {
            if (seconds > 0) {
                sb.append(", ");
            }

            sb.append(millis).append(" millis");
        }
        if (seconds == 0 && millis == 0) {
            sb.append(nanos).append(" nanos");
        }

        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy