2 * Copyright (c) 2021 Pantheon Technologies, s.r.o. 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.netconf.test.perf.notifications;
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.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
20 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ip._static.cfg.rev130722.VRFPREFIXTABLE;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class NotificationsCounter implements DOMNotificationListener {
27 private static final Logger LOG = LoggerFactory.getLogger(NotificationsCounter.class);
30 * Custom pattern to identify nodes where performance should be measured.
32 private static final Pattern NOTIFICATION_NUMBER_PATTERN = Pattern.compile(".*-notif-([0-9]+)");
34 private final String nodeId;
35 private final BindingNormalizedNodeSerializer serializer;
36 private final AtomicLong notifCounter;
37 private final long expectedNotificationCount;
38 private Stopwatch stopWatch;
39 private long totalPrefixesReceived = 0;
41 public NotificationsCounter(final String nodeId, final BindingNormalizedNodeSerializer serializer) {
43 this.serializer = serializer;
44 final Matcher matcher = NOTIFICATION_NUMBER_PATTERN.matcher(nodeId);
45 Preconditions.checkArgument(matcher.matches());
46 expectedNotificationCount = Long.parseLong(matcher.group(1));
47 Preconditions.checkArgument(expectedNotificationCount > 0);
48 this.notifCounter = new AtomicLong(this.expectedNotificationCount);
53 public void onNotification(@NonNull DOMNotification domNotification) {
54 final long andDecrement = notifCounter.getAndDecrement();
56 if (andDecrement == expectedNotificationCount) {
57 this.stopWatch = Stopwatch.createStarted();
58 LOG.info("First notification received at {}", stopWatch);
61 LOG.debug("Notification received, {} to go.", andDecrement);
62 if (LOG.isTraceEnabled()) {
63 LOG.trace("Notification received: {}", domNotification);
66 final Notification notification = serializer.fromNormalizedNodeNotification(domNotification.getType(),
67 domNotification.getBody());
68 if (notification instanceof VRFPREFIXTABLE) {
69 totalPrefixesReceived += ((VRFPREFIXTABLE)notification).getVrfPrefixes().getVrfPrefix().size();
72 if (andDecrement == 1) {
73 this.stopWatch.stop();
74 LOG.info("Last notification received at {}", stopWatch);
75 LOG.info("Elapsed ms for {} notifications: {}", expectedNotificationCount,
76 stopWatch.elapsed(TimeUnit.MILLISECONDS));
77 LOG.info("Performance (notifications/second): {}",
78 (expectedNotificationCount * 1.0 / stopWatch.elapsed(TimeUnit.MILLISECONDS)) * 1000);
79 LOG.info("Performance (prefixes/second): {}",
80 (totalPrefixesReceived * 1.0 / stopWatch.elapsed(TimeUnit.MILLISECONDS)) * 1000);