8e8e31e97527ef09cb1749cb6b82cb0c64ec9df9
[controller.git] / benchmark / rpcbenchmark / src / main / java / rpcbenchmark / impl / RpcbenchmarkProvider.java
1 /*
2  * Copyright (c) 2015 Cisco Systems Inc. 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 rpcbenchmark.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicReference;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
18 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
19 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.NodeContext;
20 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
21 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
22 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
23 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.RpcbenchmarkService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
40
41     private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
42     private static final int testTimeout = 5;
43
44     private final GlobalBindingRTCServer globalServer;
45     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
46     private final RpcProviderRegistry providerRegistry;
47
48     public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
49         this.providerRegistry = providerRegistry;
50         this.globalServer = globalServer;
51     }
52
53     public void init() {
54         LOG.info("RpcbenchmarkProvider initiated");
55     }
56
57     @Override
58     public void close() {
59         LOG.info("RpcbenchmarkProvider closed");
60     }
61
62     @Override
63     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
64         LOG.debug("startTest {}", input);
65
66         final RTCClient client;
67         final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
68
69         switch (input.getOperation()) {
70         case ROUTEDRTC:
71             List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
72             for (int i = 0; i < input.getNumServers().intValue(); i++) {
73                 GlobalBindingRTCServer server = new GlobalBindingRTCServer();
74                 RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
75                         providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
76
77                 KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
78                         InstanceIdentifier
79                             .create(RpcbenchRpcRoutes.class)
80                             .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
81                 routeIid.add(iid);
82                 routedReg.registerPath(NodeContext.class, iid);
83                 rpcRegs.add(routedReg);
84             }
85
86             client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
87             break;
88
89         case GLOBALRTC:
90             client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
91             break;
92
93         default:
94             LOG.error("Unsupported server/client type {}", input.getOperation());
95             throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
96         }
97
98         try {
99             ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
100
101             final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
102
103             LOG.info("Test Started");
104             long startTime = System.nanoTime();
105
106             for (int i = 0; i < input.getNumClients().intValue(); i++ ) {
107                 executor.submit(testRun);
108             }
109
110             executor.shutdown();
111             try {
112                 executor.awaitTermination(testTimeout, TimeUnit.MINUTES);
113             } catch (final InterruptedException e) {
114                 LOG.error("Out of time: test did not finish within the {} min deadline ", testTimeout);
115             }
116
117             long endTime = System.nanoTime();
118             LOG.info("Test Done");
119
120             long elapsedTime = endTime - startTime;
121
122             StartTestOutput output = new StartTestOutputBuilder()
123                                             .setRate((long)0)
124                                             .setGlobalRtcClientError(client.getRpcError())
125                                             .setGlobalRtcClientOk(client.getRpcOk())
126                                             .setExecTime(TimeUnit.NANOSECONDS.toMillis(elapsedTime))
127                                             .setRate((client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime)
128                                             .build();
129             return RpcResultBuilder.success(output).buildFuture();
130         } finally {
131             for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
132                 routedRpcRegistration.close();
133             }
134         }
135     }
136
137     @Override
138     public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
139         LOG.info("testStatus");
140         TestStatusOutput output = new TestStatusOutputBuilder()
141                                         .setGlobalServerCnt((long)globalServer.getNumRpcs())
142                                         .setExecStatus(execStatus.get())
143                                         .build();
144         return RpcResultBuilder.success(output).buildFuture();
145     }
146
147 }