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.opendaylight.yangtools.yang.common.Uint32;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
44 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
45 private static final int TEST_TIMEOUT = 5;
47 private final GlobalBindingRTCServer globalServer;
48 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
49 private final RpcProviderRegistry providerRegistry;
51 public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
52 this.providerRegistry = providerRegistry;
53 this.globalServer = globalServer;
57 LOG.info("RpcbenchmarkProvider initiated");
62 LOG.info("RpcbenchmarkProvider closed");
66 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
67 LOG.debug("startTest {}", input);
69 final RTCClient client;
70 final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
72 switch (input.getOperation()) {
74 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
75 for (int i = 0; i < input.getNumServers().intValue(); i++) {
76 GlobalBindingRTCServer server = new GlobalBindingRTCServer();
77 RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
78 providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
80 KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
82 .create(RpcbenchRpcRoutes.class)
83 .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
85 routedReg.registerPath(NodeContext.class, iid);
86 rpcRegs.add(routedReg);
89 client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
93 client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
97 LOG.error("Unsupported server/client type {}", input.getOperation());
98 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
102 ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
104 final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
106 LOG.info("Test Started");
107 final long startTime = System.nanoTime();
109 for (int i = 0; i < input.getNumClients().intValue(); i++) {
110 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
111 verifyNotNull(executor.submit(testRun));
116 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
117 } catch (final InterruptedException e) {
118 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
121 long endTime = System.nanoTime();
122 LOG.info("Test Done");
124 long elapsedTime = endTime - startTime;
126 StartTestOutput output = new StartTestOutputBuilder()
127 .setRate(Uint32.ZERO)
128 .setGlobalRtcClientError(Uint32.valueOf(client.getRpcError()))
129 .setGlobalRtcClientOk(Uint32.valueOf(client.getRpcOk()))
130 .setExecTime(Uint32.valueOf(TimeUnit.NANOSECONDS.toMillis(elapsedTime)))
131 .setRate(Uint32.valueOf(
132 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime))
134 return RpcResultBuilder.success(output).buildFuture();
136 for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
137 routedRpcRegistration.close();
143 public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
144 LOG.info("testStatus");
145 TestStatusOutput output = new TestStatusOutputBuilder()
146 .setGlobalServerCnt(Uint32.valueOf(globalServer.getNumRpcs()))
147 .setExecStatus(execStatus.get())
149 return RpcResultBuilder.success(output).buildFuture();