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

com.palantir.tracing.Utils Maven / Gradle / Ivy

The newest version!
/*
 * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
 *
 * 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 com.palantir.tracing;

import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

final class Utils {
    private static final ImmutableMap LARGER_UNIT = ImmutableMap.builder()
            .put(TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS)
            .put(TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS)
            .put(TimeUnit.MILLISECONDS, TimeUnit.SECONDS)
            .buildOrThrow();

    private static final ImmutableMap ABBREVIATION = ImmutableMap.builder()
            .put(TimeUnit.NANOSECONDS, "ns")
            .put(TimeUnit.MICROSECONDS, "micros")
            .put(TimeUnit.MILLISECONDS, "ms")
            .put(TimeUnit.SECONDS, "s")
            .buildOrThrow();

    public static float percent(long numerator, long denominator) {
        return 100f * numerator / denominator;
    }

    public static String renderDuration(double amount, TimeUnit timeUnit) {
        TimeUnit bigger = LARGER_UNIT.get(timeUnit);
        if (amount >= 1000 && bigger != null) {
            return renderDuration(amount / 1000, bigger);
        }

        return String.format("%.2f %s", amount, ABBREVIATION.get(timeUnit));
    }

    private Utils() {}

    public static Path createOutputFile(Class clazz, String methodName) {
        Path base =
                Paths.get(Optional.ofNullable(System.getenv("CIRCLE_ARTIFACTS")).orElse("build"));
        Path dir = base.resolve("tracing").resolve(clazz.getSimpleName());
        try {
            Files.createDirectories(dir);
            return dir.resolve(methodName + ".html");
        } catch (IOException e) {
            throw new RuntimeException("Unable to create directory " + dir, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy