rpcbenchmark: final parameters
[controller.git] / benchmark / rpcbenchmark / src / main / java / rpcbenchmark / impl / RoutedBindingRTClient.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 rpcbenchmark.impl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.atomic.AtomicLong;
17
18 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchInput;
20 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchInputBuilder;
21 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RoutedRpcBenchOutput;
22 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
23 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.payload.Payload;
24 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.payload.PayloadBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class RoutedBindingRTClient implements RTCClient {
31     private static final Logger LOG = LoggerFactory.getLogger(GlobalBindingRTCClient.class);
32     private final RpcbenchPayloadService service;
33     private final AtomicLong rpcOk = new AtomicLong(0);
34     private final AtomicLong rpcError = new AtomicLong(0);
35     private final List<RoutedRpcBenchInput> inVal;
36     private final int inSize;
37
38     public long getRpcOk() {
39         return rpcOk.get();
40     }
41
42     public long getRpcError() {
43         return rpcError.get();
44     }
45
46     public RoutedBindingRTClient(final RpcConsumerRegistry registry, final int inSize, final List<InstanceIdentifier<?>> routeIid) {
47         if (registry != null) {
48             this.service = registry.getRpcService(RpcbenchPayloadService.class);
49         } else {
50             this.service = null;
51         }
52         this.inSize = inSize;
53         this.inVal = new ArrayList<>();
54
55         List<Payload> listVals = new ArrayList<>();
56         for (int i = 0; i < inSize; i++) {
57             listVals.add(new PayloadBuilder().setId(i).build());
58         }
59
60         for (InstanceIdentifier<?> iid : routeIid) {
61             inVal.add(new RoutedRpcBenchInputBuilder().setNode(iid).setPayload(listVals).build());
62         }
63
64     }
65
66     public void runTest(final int iterations) {
67         int rpcOk = 0;
68         int rpcError = 0;
69
70         int rpcServerCnt = inVal.size();
71         for (int i = 0; i < iterations; i++) {
72             RoutedRpcBenchInput input = inVal.get(ThreadLocalRandom.current().nextInt(rpcServerCnt));
73             Future<RpcResult<RoutedRpcBenchOutput>> output = service.routedRpcBench(input);
74             try {
75                 RpcResult<RoutedRpcBenchOutput> rpcResult = output.get();
76
77                 if (rpcResult.isSuccessful()) {
78                     List<Payload> retVal = rpcResult.getResult().getPayload();
79                     if (retVal.size() == inSize) {
80                         rpcOk++;
81                     }
82                     else {
83                         rpcError++;
84                     }
85                 }
86             } catch (InterruptedException | ExecutionException e) {
87                 rpcError++;
88                 LOG.error("Execution failed: ", e);
89             }
90         }
91
92         this.rpcOk.addAndGet(rpcOk);
93         this.rpcError.addAndGet(rpcError);
94     }
95
96     @Override
97     public void close() {
98         // TODO Auto-generated method stub
99
100     }
101
102 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.