Mechanical code cleanup (benchmark)
[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
9 package ntfbenchmark.impl;
10
11 import com.google.common.base.Throwables;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
16
17 public class NtfbenchNonblockingProducer extends AbstractNtfbenchProducer {
18
19     private final SettableFuture<?> lastDeliveryFuture = SettableFuture.create();
20
21
22     public NtfbenchNonblockingProducer(final NotificationPublishService publishService, final int iterations,
23             final int payloadSize) {
24         super(publishService, iterations, payloadSize);
25     }
26
27
28     @Override
29     public void run() {
30         int ntfOk = 0;
31         int ntfError = 0;
32         ListenableFuture<?> lastOkFuture = null;
33         for (int i = 0; i < this.iterations; i++) {
34             try {
35                 final ListenableFuture<?> result = this.publishService.offerNotification(this.ntf);
36                 if (NotificationPublishService.REJECTED == result) {
37                     ntfError++;
38                 } else {
39                     ntfOk++;
40                     lastOkFuture = result;
41                 }
42             } catch (final Exception e) {
43                 ntfError++;
44             }
45         }
46
47         this.ntfOk = ntfOk;
48         this.ntfError = ntfError;
49         // We wait for last notification to be delivered to listeners.
50         if (lastOkFuture != null) {
51             try {
52                 lastOkFuture.get();
53             } catch (InterruptedException | ExecutionException e) {
54                 throw Throwables.propagate(e);
55             }
56         }
57     }
58
59 }