b0e239c4d45f99f483c7abbd1ae681d98498f723
[controller.git] / benchmark / ntfbenchmark / src / main / java / ntfbenchmark / impl / NtfbenchNonblockingProducer.java
1 /*
2  * Copyright (c) 2015 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 ntfbenchmark.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class NtfbenchNonblockingProducer extends AbstractNtfbenchProducer {
17     private static final Logger LOG = LoggerFactory.getLogger(NtfbenchNonblockingProducer.class);
18
19     public NtfbenchNonblockingProducer(final NotificationPublishService publishService, final int iterations,
20             final int payloadSize) {
21         super(publishService, iterations, payloadSize);
22     }
23
24
25     @Override
26     @SuppressWarnings("checkstyle:illegalCatch")
27     public void run() {
28         int ntfOk = 0;
29         int ntfError = 0;
30         ListenableFuture<?> lastOkFuture = null;
31         for (int i = 0; i < this.iterations; i++) {
32             try {
33                 final ListenableFuture<?> result = this.publishService.offerNotification(this.ntf);
34                 if (NotificationPublishService.REJECTED == result) {
35                     ntfError++;
36                 } else {
37                     ntfOk++;
38                     lastOkFuture = result;
39                 }
40             } catch (final Exception e) {
41                 LOG.debug("Failed to publish notification", e);
42                 ntfError++;
43             }
44         }
45
46         this.ntfOk = ntfOk;
47         this.ntfError = ntfError;
48         // We wait for last notification to be delivered to listeners.
49         if (lastOkFuture != null) {
50             try {
51                 lastOkFuture.get();
52             } catch (InterruptedException | ExecutionException e) {
53                 throw new RuntimeException(e);
54             }
55         }
56     }
57
58 }