/*
 * Copyright 2015 Timothy Brooks
 *
 * 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 net.uncontended.precipice.metrics;

import org.HdrHistogram.AtomicHistogram;
import org.HdrHistogram.Histogram;

import java.util.concurrent.TimeUnit;

public class DefaultLatencyMetrics implements LatencyMetrics {

    private final Histogram histogram;

    public DefaultLatencyMetrics() {
        this(new AtomicHistogram(TimeUnit.HOURS.toNanos(1), 2));
    }

    public DefaultLatencyMetrics(Histogram histogram) {
        this.histogram = histogram;
    }

    @Override
    public void recordLatency(long nanoLatency) {
        recordLatency(nanoLatency, System.nanoTime());
    }

    @Override
    public void recordLatency(long nanoLatency, long nanoTime) {
        if (nanoLatency != -1) {
            histogram.recordValue(Math.min(nanoLatency, histogram.getHighestTrackableValue()));
        }
    }
}

@@ -712,16 +712,64 @@
 ogram;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import j
@@ -1460,11 +1460,845 @@
  %7D%0A    %7D
+%0A%0A    public Map%3CObject, Object%3E getLatencySnapshot() %7B%0A        // Getting values from histogram is not threadsafe. Need to evaluate the best way to do snapshot.%0A%0A        Map%3CObject, Object%3E metricsMap = new HashMap%3C%3E();%0A        metricsMap.put(Snapshot.LATENCY_MAX, histogram.getMaxValue());%0A        metricsMap.put(Snapshot.LATENCY_50, histogram.getValueAtPercentile(50.0));%0A        metricsMap.put(Snapshot.LATENCY_90, histogram.getValueAtPercentile(90.0));%0A        metricsMap.put(Snapshot.LATENCY_99, histogram.getValueAtPercentile(99.0));%0A        metricsMap.put(Snapshot.LATENCY_99_9, histogram.getValueAtPercentile(99.9));%0A        metricsMap.put(Snapshot.LATENCY_99_99, histogram.getValueAtPercentile(99.99));%0A        metricsMap.put(Snapshot.LATENCY_99_999, histogram.getValueAtPercentile(99.999));%0A        return metricsMap;%0A    %7D
 %0A%7D%0A

Add basic ability to do snapshot for latency.