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
9 package rpcbenchmark.impl;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicReference;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.NodeContext;
21 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchPayloadService;
22 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.RpcbenchRpcRoutes;
23 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRoute;
24 import org.opendaylight.yang.gen.v1.rpcbench.payload.rev150702.rpcbench.rpc.routes.RpcRouteKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.RpcbenchmarkService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.StartTestOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutput.ExecStatus;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rpcbenchmark.rev150702.TestStatusOutputBuilder;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class RpcbenchmarkProvider implements AutoCloseable, RpcbenchmarkService {
41 private static final Logger LOG = LoggerFactory.getLogger(RpcbenchmarkProvider.class);
42 private static final int testTimeout = 5;
44 private final GlobalBindingRTCServer globalServer;
45 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
46 private final RpcProviderRegistry providerRegistry;
48 public RpcbenchmarkProvider(final RpcProviderRegistry providerRegistry, final GlobalBindingRTCServer globalServer) {
49 this.providerRegistry = providerRegistry;
50 this.globalServer = globalServer;
54 LOG.info("RpcbenchmarkProvider initiated");
59 LOG.info("RpcbenchmarkProvider closed");
63 public Future<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
64 LOG.debug("startTest {}", input);
66 final RTCClient client;
67 final List<RoutedRpcRegistration<?>> rpcRegs = new ArrayList<>();
69 switch (input.getOperation()) {
71 List<InstanceIdentifier<?>> routeIid = new ArrayList<>();
72 for (int i = 0; i < input.getNumServers().intValue(); i++) {
73 GlobalBindingRTCServer server = new GlobalBindingRTCServer();
74 RoutedRpcRegistration<RpcbenchPayloadService> routedReg =
75 providerRegistry.addRoutedRpcImplementation(RpcbenchPayloadService.class, server);
77 KeyedInstanceIdentifier<RpcRoute, RpcRouteKey> iid =
79 .create(RpcbenchRpcRoutes.class)
80 .child(RpcRoute.class, new RpcRouteKey(Integer.toString(i)));
82 routedReg.registerPath(NodeContext.class, iid);
83 rpcRegs.add(routedReg);
86 client = new RoutedBindingRTClient(providerRegistry, input.getPayloadSize().intValue(), routeIid);
90 client = new GlobalBindingRTCClient(providerRegistry, input.getPayloadSize().intValue());
94 LOG.error("Unsupported server/client type {}", input.getOperation());
95 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
99 ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
101 final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
103 LOG.info("Test Started");
104 long startTime = System.nanoTime();
106 for (int i = 0; i < input.getNumClients().intValue(); i++ ) {
107 executor.submit(testRun);
112 executor.awaitTermination(testTimeout, TimeUnit.MINUTES);
113 } catch (final InterruptedException e) {
114 LOG.error("Out of time: test did not finish within the {} min deadline ", testTimeout);
117 long endTime = System.nanoTime();
118 LOG.info("Test Done");
120 long elapsedTime = endTime - startTime;
122 StartTestOutput output = new StartTestOutputBuilder()
124 .setGlobalRtcClientError(client.getRpcError())
125 .setGlobalRtcClientOk(client.getRpcOk())
126 .setExecTime(TimeUnit.NANOSECONDS.toMillis(elapsedTime))
127 .setRate((client.getRpcOk() + client.getRpcError()) * 1000000000 / elapsedTime)
129 return RpcResultBuilder.success(output).buildFuture();
131 for (RoutedRpcRegistration<?> routedRpcRegistration : rpcRegs) {
132 routedRpcRegistration.close();
138 public Future<RpcResult<TestStatusOutput>> testStatus() {
139 LOG.info("testStatus");
140 TestStatusOutput output = new TestStatusOutputBuilder()
141 .setGlobalServerCnt((long)globalServer.getNumRpcs())
142 .setExecStatus(execStatus.get())
144 return RpcResultBuilder.success(output).buildFuture();