44c9e71272d60d620fe10136958be54fe10a21b6
[controller.git] / benchmark / rpcbenchmark / src / main / java / rpcbenchmark / impl / RpcbenchmarkProvider.java
1 /*
2  * Copyright (c) 2015 Cisco Systems Inc. and others.  All rights reserved.
3  *
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
7  */
8 package rpcbenchmark.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
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;
17 import java.util.Set;
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;
52
53 @Singleton
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;
59
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;
65
66     @Inject
67     @Activate
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)
76             .build());
77         LOG.info("RpcbenchmarkProvider initiated");
78     }
79
80     @Override
81     @Deactivate
82     @PreDestroy
83     public void close() {
84         globalServer.close();
85         reg.close();
86         LOG.info("RpcbenchmarkProvider closed");
87     }
88
89     private ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
90         LOG.debug("startTest {}", input);
91
92         final RTCClient client;
93         RoutedBindingRTCServer routed = null;
94
95         switch (input.getOperation()) {
96             case ROUTEDRTC:
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))));
101                 }
102
103                 routed = new RoutedBindingRTCServer(providerRegistry, Set.copyOf(routeIid));
104                 client = new RoutedBindingRTClient(consumerRegistry, input.getPayloadSize().intValue(), routeIid);
105                 break;
106
107             case GLOBALRTC:
108                 client = new GlobalBindingRTCClient(consumerRegistry, input.getPayloadSize().intValue());
109                 break;
110
111             default:
112                 LOG.error("Unsupported server/client type {}", input.getOperation());
113                 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
114         }
115
116         try {
117             ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
118
119             final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
120
121             LOG.info("Test Started");
122             final long startTime = System.nanoTime();
123
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));
127             }
128
129             executor.shutdown();
130             try {
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);
134             }
135
136             long endTime = System.nanoTime();
137             LOG.info("Test Done");
138
139             long elapsedTime = endTime - startTime;
140
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))
148                                             .build();
149             return RpcResultBuilder.success(output).buildFuture();
150         } finally {
151             if (routed != null) {
152                 routed.close();
153             }
154         }
155     }
156
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())
162                                         .build();
163         return RpcResultBuilder.success(output).buildFuture();
164     }
165
166 }