2 * Copyright (c) 2015 Cisco Systems Inc. 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 rpcbenchmark.impl;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicReference;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.NodeContext;
22 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
23 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
24 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
25 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.RpcbenchmarkService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
43 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
44 private static final int TEST_TIMEOUT = 5;
46 private final GlobalBindingRTCServer globalServer;
47 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
48 private final RpcProviderRegistry providerRegistry;
50 public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
51 this.providerRegistry = providerRegistry;
52 this.globalServer = globalServer;
56 LOG.info("RpcbenchmarkProvider initiated");
61 LOG.info("RpcbenchmarkProvider closed");
65 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
66 LOG.debug("startTest {}", input);
68 final RTCClient client;
69 final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
71 switch (input.getOperation()) {
73 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
74 for (int i = 0; i < input.getNumServers().intValue(); i++) {
75 GlobalBindingRTCServer server = new GlobalBindingRTCServer();
76 RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
77 providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
79 KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
81 .create(RpcbenchRpcRoutes.class)
82 .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
84 routedReg.registerPath(NodeContext.class, iid);
85 rpcRegs.add(routedReg);
88 client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
92 client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
96 LOG.error("Unsupported server/client type {}", input.getOperation());
97 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
101 ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
103 final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
105 LOG.info("Test Started");
106 final long startTime = System.nanoTime();
108 for (int i = 0; i < input.getNumClients().intValue(); i++) {
109 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
110 verifyNotNull(executor.submit(testRun));
115 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
116 } catch (final InterruptedException e) {
117 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
120 long endTime = System.nanoTime();
121 LOG.info("Test Done");
123 long elapsedTime = endTime - startTime;
125 StartTestOutput output = new StartTestOutputBuilder()
127 .setGlobalRtcClientError(client.getRpcError())
128 .setGlobalRtcClientOk(client.getRpcOk())
129 .setExecTime(TimeUnit.NANOSECONDS.toMillis(elapsedTime))
131 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime)
133 return RpcResultBuilder.success(output).buildFuture();
135 for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
136 routedRpcRegistration.close();
142 public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
143 LOG.info("testStatus");
144 TestStatusOutput output = new TestStatusOutputBuilder()
145 .setGlobalServerCnt((long)globalServer.getNumRpcs())
146 .setExecStatus(execStatus.get())
148 return RpcResultBuilder.success(output).buildFuture();