Bump upstreams
[netconf.git] / netconf / tools / netconf-test-perf / src / main / java / org / opendaylight / netconf / test / perf / notifications / NotificationsCounter.java
1 /*
2  * Copyright (c) 2021 Pantheon Technologies, s.r.o. 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.netconf.test.perf.notifications;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Stopwatch;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.atomic.AtomicLong;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
19 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ip._static.cfg.rev130722.VRFPREFIXTABLE;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class NotificationsCounter implements DOMNotificationListener {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NotificationsCounter.class);
26
27     /**
28      * Custom pattern to identify nodes where performance should be measured.
29      */
30     private static final Pattern NOTIFICATION_NUMBER_PATTERN = Pattern.compile(".*-notif-([0-9]+)");
31
32     private final String nodeId;
33     private final BindingNormalizedNodeSerializer serializer;
34     private final AtomicLong notifCounter;
35     private final long expectedNotificationCount;
36     private Stopwatch stopWatch;
37     private long totalPrefixesReceived = 0;
38
39     public NotificationsCounter(final String nodeId, final BindingNormalizedNodeSerializer serializer) {
40         this.nodeId = nodeId;
41         this.serializer = serializer;
42         final Matcher matcher = NOTIFICATION_NUMBER_PATTERN.matcher(nodeId);
43         Preconditions.checkArgument(matcher.matches());
44         expectedNotificationCount = Long.parseLong(matcher.group(1));
45         Preconditions.checkArgument(expectedNotificationCount > 0);
46         notifCounter = new AtomicLong(expectedNotificationCount);
47     }
48
49
50     @Override
51     public void onNotification(final DOMNotification domNotification) {
52         final long andDecrement = notifCounter.getAndDecrement();
53
54         if (andDecrement == expectedNotificationCount) {
55             stopWatch = Stopwatch.createStarted();
56             LOG.info("First notification received at {}", stopWatch);
57         }
58
59         LOG.debug("Notification received, {} to go.", andDecrement);
60         if (LOG.isTraceEnabled()) {
61             LOG.trace("Notification received: {}", domNotification);
62         }
63
64         final var notification = serializer.fromNormalizedNodeNotification(domNotification.getType(),
65             domNotification.getBody());
66         if (notification instanceof VRFPREFIXTABLE) {
67             totalPrefixesReceived += ((VRFPREFIXTABLE)notification).getVrfPrefixes().getVrfPrefix().size();
68         }
69
70         if (andDecrement == 1) {
71             stopWatch.stop();
72             LOG.info("Last notification received at {}", stopWatch);
73             LOG.info("Elapsed ms for {} notifications: {}", expectedNotificationCount,
74                 stopWatch.elapsed(TimeUnit.MILLISECONDS));
75             LOG.info("Performance (notifications/second): {}",
76                 expectedNotificationCount * 1.0 / stopWatch.elapsed(TimeUnit.MILLISECONDS) * 1000);
77             LOG.info("Performance (prefixes/second): {}",
78                 totalPrefixesReceived * 1.0 / stopWatch.elapsed(TimeUnit.MILLISECONDS) * 1000);
79         }
80     }
81
82 }