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;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.atomic.AtomicReference;
20 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
21 import org.opendaylight.mdsal.binding.api.RpcProviderService;
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.concepts.ObjectRegistration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
45 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
46 private static final int TEST_TIMEOUT = 5;
48 private final GlobalBindingRTCServer globalServer;
49 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
50 private final RpcProviderService providerRegistry;
51 private final RpcConsumerRegistry consumerRegistry;
53 public RpcbenchmarkProvider(final RpcProviderService providerRegistry, final RpcConsumerRegistry consumerRegistry,
54 final GlobalBindingRTCServer globalServer) {
55 this.providerRegistry = providerRegistry;
56 this.consumerRegistry = consumerRegistry;
57 this.globalServer = globalServer;
61 LOG.info("RpcbenchmarkProvider initiated");
66 LOG.info("RpcbenchmarkProvider closed");
70 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
71 LOG.debug("startTest {}", input);
73 final RTCClient client;
74 final List<ObjectRegistration<?>> rpcRegs = new ArrayList<>();
76 switch (input.getOperation()) {
78 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
79 for (int i = 0; i < input.getNumServers().intValue(); i++) {
80 GlobalBindingRTCServer server = new GlobalBindingRTCServer();
81 KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
82 InstanceIdentifier.create(RpcbenchRpcRoutes.class)
83 .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
86 ObjectRegistration<?> routedReg = providerRegistry.registerRpcImplementation(
87 RpcbenchPayloadService.class, server, Set.of(iid));
89 rpcRegs.add(routedReg);
92 client = new RoutedBindingRTClient(consumerRegistry, input.getPayloadSize().intValue(), routeIid);
96 client = new GlobalBindingRTCClient(consumerRegistry, input.getPayloadSize().intValue());
100 LOG.error("Unsupported server/client type {}", input.getOperation());
101 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
105 ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
107 final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
109 LOG.info("Test Started");
110 final long startTime = System.nanoTime();
112 for (int i = 0; i < input.getNumClients().intValue(); i++) {
113 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
114 verifyNotNull(executor.submit(testRun));
119 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
120 } catch (final InterruptedException e) {
121 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
124 long endTime = System.nanoTime();
125 LOG.info("Test Done");
127 long elapsedTime = endTime - startTime;
129 StartTestOutput output = new StartTestOutputBuilder()
130 .setRate(Uint32.ZERO)
131 .setGlobalRtcClientError(Uint32.valueOf(client.getRpcError()))
132 .setGlobalRtcClientOk(Uint32.valueOf(client.getRpcOk()))
133 .setExecTime(Uint32.valueOf(TimeUnit.NANOSECONDS.toMillis(elapsedTime)))
134 .setRate(Uint32.valueOf(
135 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime))
137 return RpcResultBuilder.success(output).buildFuture();
139 rpcRegs.forEach(ObjectRegistration::close);
144 public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
145 LOG.info("testStatus");
146 TestStatusOutput output = new TestStatusOutputBuilder()
147 .setGlobalServerCnt(Uint32.valueOf(globalServer.getNumRpcs()))
148 .setExecStatus(execStatus.get())
150 return RpcResultBuilder.success(output).buildFuture();