Improve segmented journal actor metrics
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / listener / DsbenchmarkListener.java
1 /*
2  * Copyright (c) 2016 Cisco Systems 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.dsbenchmark.listener;
9
10 import java.util.List;
11 import java.util.concurrent.atomic.AtomicInteger;
12 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
13 import org.opendaylight.mdsal.binding.api.DataTreeModification;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class DsbenchmarkListener implements DataTreeChangeListener<TestExec> {
19     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkListener.class);
20     private final AtomicInteger numEvents = new AtomicInteger(0);
21     private final AtomicInteger numDataChanges = new AtomicInteger(0);
22
23     @Override
24     public void onDataTreeChanged(final List<DataTreeModification<TestExec>> changes) {
25         // Since we're registering the same DsbenchmarkListener object for both
26         // OPERATIONAL and CONFIG, the onDataTreeChanged() method can be called
27         // from different threads, and we need to use atomic counters.
28
29         final int eventNum = numEvents.incrementAndGet();
30         numDataChanges.addAndGet(changes.size());
31
32         if (LOG.isDebugEnabled()) {
33             logDataTreeChangeEvent(eventNum, changes);
34         }
35     }
36
37     private static synchronized void logDataTreeChangeEvent(final int eventNum,
38             final List<DataTreeModification<TestExec>> changes) {
39         LOG.debug("DsbenchmarkListener-onDataTreeChanged: Event {}", eventNum);
40
41         for (var change : changes) {
42             final var rootNode = change.getRootNode();
43             final var modType = rootNode.modificationType();
44             final var changeId = rootNode.step();
45             final var modifications = rootNode.modifiedChildren();
46
47             LOG.debug("    changeId {}, modType {}, mods: {}", changeId, modType, modifications.size());
48
49             for (var mod : modifications) {
50                 LOG.debug("      mod-getDataAfter: {}", mod.dataAfter());
51             }
52         }
53     }
54
55     public int getNumEvents() {
56         return numEvents.get();
57     }
58
59     public int getNumDataChanges() {
60         return numDataChanges.get();
61     }
62 }