aa4297613e183a335aea53eef47e9f39b3788cc9
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / DataStoreStatsWrapper.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.dom.broker.impl;
9
10 import java.util.concurrent.atomic.AtomicLong;
11
12 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
13 import org.opendaylight.controller.sal.core.api.data.DataStore;
14 import org.opendaylight.yangtools.concepts.Delegator;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17
18 public class DataStoreStatsWrapper implements Delegator<DataStore>, DataStore {
19
20     private final DataStore delegate;
21
22     private AtomicLong cfgReadCount = new AtomicLong();
23     private AtomicLong cfgReadTimeTotal = new AtomicLong();
24
25     private AtomicLong operReadCount = new AtomicLong();
26     private AtomicLong operReadTimeTotal = new AtomicLong();
27
28     private AtomicLong requestCommitCount = new AtomicLong();
29     private AtomicLong requestCommitTimeTotal = new AtomicLong();
30
31     public DataStoreStatsWrapper(DataStore store) {
32         delegate = store;
33     }
34
35     @Override
36     public DataStore getDelegate() {
37         return delegate;
38     }
39
40     @Override
41     public CompositeNode readConfigurationData(InstanceIdentifier path) {
42         cfgReadCount.incrementAndGet();
43         final long startTime = System.nanoTime();
44         try {
45             return delegate.readConfigurationData(path);
46         } finally {
47             final long endTime = System.nanoTime();
48             final long runTime = endTime - startTime;
49             cfgReadTimeTotal.addAndGet(runTime);
50         }
51     }
52
53     @Override
54     public CompositeNode readOperationalData(InstanceIdentifier path) {
55         operReadCount.incrementAndGet();
56         final long startTime = System.nanoTime();
57         try {
58             return delegate.readOperationalData(path);
59         } finally {
60             final long endTime = System.nanoTime();
61             final long runTime = endTime - startTime;
62             operReadTimeTotal.addAndGet(runTime);
63         }
64     }
65
66     public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
67             DataModification<InstanceIdentifier, CompositeNode> modification) {
68         requestCommitCount.incrementAndGet();
69         final long startTime = System.nanoTime();
70         try {
71             return delegate.requestCommit(modification);
72         } finally {
73             final long endTime = System.nanoTime();
74             final long runTime = endTime - startTime;
75             requestCommitTimeTotal.addAndGet(runTime);
76         }
77     };
78
79     @Override
80     public boolean containsConfigurationPath(InstanceIdentifier path) {
81         return delegate.containsConfigurationPath(path);
82     }
83
84     public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
85         return delegate.getStoredConfigurationPaths();
86     }
87
88     public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
89         return delegate.getStoredOperationalPaths();
90     }
91
92     public boolean containsOperationalPath(InstanceIdentifier path) {
93         return delegate.containsOperationalPath(path);
94     }
95
96     public final long getConfigurationReadCount() {
97         return cfgReadCount.get();
98     }
99
100     public final long getOperationalReadCount() {
101         return operReadCount.get();
102     }
103
104     public final long getRequestCommitCount() {
105         return requestCommitCount.get();
106     }
107
108     public final double getConfigurationReadTotalTime() {
109         return cfgReadTimeTotal.get() / 1000.0d;
110     }
111
112     public final double getOperationalReadTotalTime() {
113         return operReadTimeTotal.get() / 1000.0d;
114     }
115
116     public final double getRequestCommitTotalTime() {
117         return requestCommitTimeTotal.get() / 1000.0d;
118     }
119
120     public final double getConfigurationReadAverageTime() {
121         long readCount = cfgReadCount.get();
122         if(readCount == 0) {
123             return 0;
124         }
125         return getConfigurationReadTotalTime() / readCount;
126     }
127
128     public final double getOperationalReadAverageTime() {
129         long readCount = operReadCount.get();
130         if(readCount == 0) {
131             return 0;
132         }
133         return getOperationalReadTotalTime() / readCount;
134     }
135
136     public final double getRequestCommitAverageTime() {
137         long count = requestCommitCount.get();
138         if(count == 0) {
139             return 0;
140         }
141         return getRequestCommitTotalTime() / count;
142     }
143
144 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.