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