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.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.binding.api.RpcProviderService;
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.RpcbenchmarkService;
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.TestStatusInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
37 import org.opendaylight.yangtools.concepts.Registration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42 import org.osgi.service.component.annotations.Activate;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Deactivate;
45 import org.osgi.service.component.annotations.Reference;
46 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 @Component(service = {})
52 @RequireServiceComponentRuntime
53 public final class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
54 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
55 private static final int TEST_TIMEOUT = 5;
57 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
58 private final RpcProviderService providerRegistry;
59 private final RpcConsumerRegistry consumerRegistry;
60 private final GlobalBindingRTCServer globalServer;
61 private final Registration reg;
65 public RpcbenchmarkProvider(@Reference final RpcProviderService providerRegistry,
66 @Reference final RpcConsumerRegistry consumerRegistry) {
67 this.providerRegistry = requireNonNull(providerRegistry);
68 this.consumerRegistry = requireNonNull(consumerRegistry);
69 globalServer = new GlobalBindingRTCServer(providerRegistry);
70 reg = providerRegistry.registerRpcImplementation(RpcbenchmarkService.class, this);
71 LOG.info("RpcbenchmarkProvider initiated");
80 LOG.info("RpcbenchmarkProvider closed");
84 public 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) {
153 public ListenableFuture<RpcResult<TestStatusOutput>> testStatus(final TestStatusInput input) {
154 LOG.info("testStatus");
155 TestStatusOutput output = new TestStatusOutputBuilder()
156 .setGlobalServerCnt(Uint32.valueOf(globalServer.getNumRpcs()))
157 .setExecStatus(execStatus.get())
159 return RpcResultBuilder.success(output).buildFuture();