atomic-storage: remove type dependency at segment level I/O
[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.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
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;
50
51 @Singleton
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;
57
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;
63
64     @Inject
65     @Activate
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");
73     }
74
75     @Override
76     @Deactivate
77     @PreDestroy
78     public void close() {
79         globalServer.close();
80         reg.close();
81         LOG.info("RpcbenchmarkProvider closed");
82     }
83
84     private ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
85         LOG.debug("startTest {}", input);
86
87         final RTCClient client;
88         RoutedBindingRTCServer routed = null;
89
90         switch (input.getOperation()) {
91             case ROUTEDRTC:
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))));
96                 }
97
98                 routed = new RoutedBindingRTCServer(providerRegistry, Set.copyOf(routeIid));
99                 client = new RoutedBindingRTClient(consumerRegistry, input.getPayloadSize().intValue(), routeIid);
100                 break;
101
102             case GLOBALRTC:
103                 client = new GlobalBindingRTCClient(consumerRegistry, input.getPayloadSize().intValue());
104                 break;
105
106             default:
107                 LOG.error("Unsupported server/client type {}", input.getOperation());
108                 throw new IllegalArgumentException("Unsupported server/client type" + input.getOperation());
109         }
110
111         try {
112             ExecutorService executor = Executors.newFixedThreadPool(input.getNumClients().intValue());
113
114             final Runnable testRun = () -> client.runTest(input.getIterations().intValue());
115
116             LOG.info("Test Started");
117             final long startTime = System.nanoTime();
118
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));
122             }
123
124             executor.shutdown();
125             try {
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);
129             }
130
131             long endTime = System.nanoTime();
132             LOG.info("Test Done");
133
134             long elapsedTime = endTime - startTime;
135
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))
143                                             .build();
144             return RpcResultBuilder.success(output).buildFuture();
145         } finally {
146             if (routed != null) {
147                 routed.close();
148             }
149         }
150     }
151
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())
157                                         .build();
158         return RpcResultBuilder.success(output).buildFuture();
159     }
160
161 }