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 org.opendaylight.controller.netconf.test.tool.client.stress;
11 import ch.qos.logback.classic.Level;
12 import com.google.common.base.Charsets;
13 import com.google.common.base.Stopwatch;
14 import com.google.common.io.Files;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import io.netty.util.HashedWheelTimer;
17 import io.netty.util.Timer;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 import net.sourceforge.argparse4j.inf.ArgumentParser;
28 import net.sourceforge.argparse4j.inf.ArgumentParserException;
29 import org.opendaylight.controller.netconf.api.NetconfMessage;
30 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
31 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
32 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
33 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
34 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.CommitInput;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfigInput;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43 import org.xml.sax.SAXException;
45 public final class StressClient {
47 private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
49 static final QName COMMIT_QNAME = QName.create(CommitInput.QNAME, "commit");
50 public static final NetconfMessage COMMIT_MSG;
54 COMMIT_MSG = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"commit-batch\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
57 } catch (SAXException | IOException e) {
58 throw new ExceptionInInitializerError(e);
62 static final QName EDIT_QNAME = QName.create(EditConfigInput.QNAME, "edit-config");
63 static final org.w3c.dom.Document editBlueprint;
67 editBlueprint = XmlUtil.readXmlToDocument(
68 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
69 " <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
73 " <default-operation>none</default-operation>" +
77 } catch (SAXException | IOException e) {
78 throw new ExceptionInInitializerError(e);
82 private static final String MSG_ID_PLACEHOLDER_REGEX = "\\{MSG_ID\\}";
83 private static final String PHYS_ADDR_PLACEHOLDER = "{PHYS_ADDR}";
85 private static long macStart = 0xAABBCCDD0000L;
87 public static void main(final String[] args) {
88 final Parameters params = parseArgs(args, Parameters.getParser());
91 final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
92 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
94 final int threadAmount = params.threadAmount;
95 LOG.info("thread amount: " + threadAmount);
96 final int requestsPerThread = params.editCount / params.threadAmount;
97 LOG.info("requestsPerThread: " + requestsPerThread);
98 final int leftoverRequests = params.editCount % params.threadAmount;
99 LOG.info("leftoverRequests: " + leftoverRequests);
102 LOG.info("Preparing messages");
103 // Prepare all msgs up front
104 final List<List<NetconfMessage>> allPreparedMessages = new ArrayList<>(threadAmount);
105 for (int i = 0; i < threadAmount; i++) {
106 if (i != threadAmount - 1) {
107 allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread));
109 allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread + leftoverRequests));
114 final String editContentString;
116 editContentString = Files.toString(params.editContent, Charsets.UTF_8);
117 } catch (final IOException e) {
118 throw new IllegalArgumentException("Cannot read content of " + params.editContent);
121 for (int i = 0; i < threadAmount; i++) {
122 final List<NetconfMessage> preparedMessages = allPreparedMessages.get(i);
124 if (i == threadAmount - 1) {
125 padding = leftoverRequests;
127 for (int j = 0; j < requestsPerThread + padding; j++) {
128 LOG.debug("id: " + (i * requestsPerThread + j));
129 preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
133 final NioEventLoopGroup nioGroup = new NioEventLoopGroup();
134 final Timer timer = new HashedWheelTimer();
136 final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(params, nioGroup, timer);
138 final List<StressClientCallable> callables = new ArrayList<>(threadAmount);
139 for (final List<NetconfMessage> messages : allPreparedMessages) {
140 callables.add(new StressClientCallable(params, netconfClientDispatcher, messages));
143 final ExecutorService executorService = Executors.newFixedThreadPool(threadAmount);
145 LOG.info("Starting stress test");
146 final Stopwatch started = Stopwatch.createStarted();
148 final List<Future<Boolean>> futures = executorService.invokeAll(callables);
149 for (final Future<Boolean> future : futures) {
151 future.get(4L, TimeUnit.MINUTES);
152 } catch (ExecutionException | TimeoutException e) {
153 throw new RuntimeException(e);
156 executorService.shutdownNow();
157 } catch (final InterruptedException e) {
158 throw new RuntimeException("Unable to execute requests", e);
162 LOG.info("FINISHED. Execution time: {}", started);
163 LOG.info("Requests per second: {}", (params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS)));
168 nioGroup.shutdownGracefully().get(20L, TimeUnit.SECONDS);
169 } catch (InterruptedException | ExecutionException | TimeoutException e) {
170 LOG.warn("Unable to close executor properly", e);
174 static NetconfMessage prepareMessage(final int id, final String editContentString) {
175 final Document msg = XmlUtil.createDocumentCopy(editBlueprint);
176 msg.getDocumentElement().setAttribute("message-id", Integer.toString(id));
177 final NetconfMessage netconfMessage = new NetconfMessage(msg);
179 final Element editContentElement;
181 // Insert message id where needed
182 String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
184 final StringBuilder stringBuilder = new StringBuilder(specificEditContent);
185 int idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
187 stringBuilder.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), getMac(macStart++));
188 idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
190 specificEditContent = stringBuilder.toString();
192 editContentElement = XmlUtil.readXmlToElement(specificEditContent);
193 final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0)).
194 getElementsByTagName("config").item(0);
195 config.appendChild(msg.importNode(editContentElement, true));
196 } catch (final IOException | SAXException e) {
197 throw new IllegalArgumentException("Edit content file is unreadable", e);
200 return netconfMessage;
203 private static NetconfClientDispatcherImpl configureClientDispatcher(final Parameters params, final NioEventLoopGroup nioGroup, final Timer timer) {
204 final NetconfClientDispatcherImpl netconfClientDispatcher;
206 if(params.legacyFraming) {
207 netconfClientDispatcher= ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer);
209 netconfClientDispatcher = ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
212 if(params.legacyFraming) {
213 netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
215 netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
218 return netconfClientDispatcher;
221 public static String getMac(long mac) {
222 StringBuilder m = new StringBuilder(Long.toString(mac, 16));
224 for (int i = m.length(); i < 12; i++) {
228 for (int j = m.length() - 2; j >= 2; j-=2) {
235 private static Parameters parseArgs(final String[] args, final ArgumentParser parser) {
236 final Parameters opt = new Parameters();
238 parser.parseArgs(args, opt);
240 } catch (final ArgumentParserException e) {
241 parser.handleError(e);
249 static class LoggingRemoteDevice implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
251 public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities, final NetconfDeviceCommunicator netconfDeviceCommunicator) {
252 LOG.info("Session established");
256 public void onRemoteSessionDown() {
257 LOG.info("Session down");
261 public void onRemoteSessionFailed(final Throwable throwable) {
262 LOG.info("Session failed");
266 public void onNotification(final NetconfMessage notification) {
267 LOG.info("Notification received: {}", notification.toString());