2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import com.google.common.primitives.UnsignedLong;
13 * Non-concurrent implementation, useful for non-contended cases.
15 final class SynchronizedDurationStatsTracker extends DurationStatisticsTracker {
16 private static final long NOT_SET = -1;
18 // Hot fields in the order in which they are accessed
19 private long durationSum = 0;
20 private long durationCount = 0;
21 private long shortestDuration = NOT_SET;
22 private long longestDuration = NOT_SET;
24 // Cold fields, longest has a higher chance of being accessed
25 private long longestTimestamp;
26 private long shortestTimestamp;
28 SynchronizedDurationStatsTracker() {
33 public synchronized void addDuration(final long duration) {
34 durationSum += duration;
37 if (duration < shortestDuration || shortestDuration == NOT_SET) {
38 shortestDuration = duration;
39 shortestTimestamp = System.currentTimeMillis();
41 if (duration > longestDuration) {
42 longestDuration = duration;
43 longestTimestamp = System.currentTimeMillis();
48 public synchronized double getAverageDuration() {
49 return durationCount == 0 ? 0 : UnsignedLong.fromLongBits(durationSum).doubleValue() / durationCount;
53 public synchronized long getTotalDurations() {
58 public synchronized void reset() {
61 longestDuration = NOT_SET;
62 shortestDuration = NOT_SET;
66 protected synchronized DurationWithTime getShortest() {
67 return shortestDuration == NOT_SET ? null : new DurationWithTime(shortestDuration, shortestTimestamp);
71 protected synchronized DurationWithTime getLongest() {
72 return longestDuration == NOT_SET ? null : new DurationWithTime(longestDuration, longestTimestamp);