Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / TimeCounter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import java.util.concurrent.TimeUnit;
12
13 /**
14  * Moving average - measure and compute.
15  */
16 public class TimeCounter {
17     private long beginningOfLap;
18     private long delta;
19     private int marksCount = 0;
20
21     public void markStart() {
22         beginningOfLap = System.nanoTime();
23         delta = 0;
24         marksCount = 0;
25     }
26
27     public void addTimeMark() {
28         final long now = System.nanoTime();
29         delta += now - beginningOfLap;
30         marksCount++;
31         beginningOfLap = now;
32     }
33
34     public long getAverageTimeBetweenMarks() {
35         long average = 0;
36         if (marksCount > 0) {
37             average = delta / marksCount;
38         }
39         return TimeUnit.NANOSECONDS.toMillis(average);
40     }
41 }