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;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicReference;
21 import javax.annotation.PreDestroy;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.mdsal.binding.api.RpcService;
26 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
27 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
28 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTest;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatus;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
38 import org.opendaylight.yangtools.concepts.Registration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Deactivate;
46 import org.osgi.service.component.annotations.Reference;
47 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 @Component(service = {})
53 @RequireServiceComponentRuntime
54 public final class RpcbenchmarkProvider implements AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
56 private static final int TEST_TIMEOUT = 5;
58 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
59 private final RpcProviderService providerRegistry;
60 private final RpcService consumerRegistry;
61 private final GlobalBindingRTCServer globalServer;
62 private final Registration reg;
66 public RpcbenchmarkProvider(@Reference final RpcProviderService providerRegistry,
67 @Reference final RpcService consumerRegistry) {
68 this.providerRegistry = requireNonNull(providerRegistry);
69 this.consumerRegistry = requireNonNull(consumerRegistry);
70 globalServer = new GlobalBindingRTCServer(providerRegistry);
71 reg = providerRegistry.registerRpcImplementations((TestStatus) this::testStatus, (StartTest) this::startTest);
72 LOG.info("RpcbenchmarkProvider initiated");
81 LOG.info("RpcbenchmarkProvider closed");
84 private ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
85 LOG.debug("startTest {}", input);
87 final RTCClient client;
88 RoutedBindingRTCServer routed = null;
90 switch (input.getOperation()) {
92 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
93 for (int i = 0; i < input.getNumServers().intValue(); i++) {
94 routeIid.add(InstanceIdentifier.create(RpcbenchRpcRoutes.class)
95 .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i))));
98 routed = new RoutedBindingRTCServer(providerRegistry, Set.copyOf(routeIid));
99 client = new RoutedBindingRTClient(consumerRegistry, input.getPayloadSize().intValue(), routeIid);
103 client = new GlobalBindingRTCClient(consumerRegistry, input.getPayloadSize().intValue());
107 LOG.error("Unsupported server/client type {}", input.getOperation());
108 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
112 ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
114 final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
116 LOG.info("Test Started");
117 final long startTime = System.nanoTime();
119 for (int i = 0; i < input.getNumClients().intValue(); i++) {
120 // FIXME: fools RV_RETURN_VALUE_IGNORED_BAD_PRACTICE, but we should check more
121 verifyNotNull(executor.submit(testRun));
126 executor.awaitTermination(TEST_TIMEOUT, TimeUnit.MINUTES);
127 } catch (final InterruptedException e) {
128 LOG.error("Out of time: test did not finish within the {} min deadline ", TEST_TIMEOUT);
131 long endTime = System.nanoTime();
132 LOG.info("Test Done");
134 long elapsedTime = endTime - startTime;
136 StartTestOutput output = new StartTestOutputBuilder()
137 .setRate(Uint32.ZERO)
138 .setGlobalRtcClientError(Uint32.valueOf(client.getRpcError()))
139 .setGlobalRtcClientOk(Uint32.valueOf(client.getRpcOk()))
140 .setExecTime(Uint32.valueOf(TimeUnit.NANOSECONDS.toMillis(elapsedTime)))
141 .setRate(Uint32.valueOf(
142 (client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime))
144 return RpcResultBuilder.success(output).buildFuture();
146 if (routed != null) {
152 private ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
153 LOG.info("testStatus");
154 TestStatusOutput output = new TestStatusOutputBuilder()
155 .setGlobalServerCnt(Uint32.valueOf(globalServer.getNumRpcs()))
156 .setExecStatus(execStatus.get())
158 return RpcResultBuilder.success(output).buildFuture();