|
| 1 | +/* |
| 2 | + * Copyright 2008 - 2019, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io> |
| 3 | + * |
| 4 | + * |
| 5 | + * This file is part of JBotSim. |
| 6 | + * |
| 7 | + * JBotSim is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * JBotSim is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with JBotSim. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package io.jbotsim.core; |
| 23 | + |
| 24 | +import io.jbotsim.core.event.ClockListener; |
| 25 | +import io.jbotsim.ui.JViewer; |
| 26 | +import io.jbotsim.ui.icons.Icons; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * <p>This purpose of this class is to test the behavior of JBotSim messaging capabilities.</p> |
| 33 | + * <p>Here, we can set the number of nodes to be created and the number of nodes per connected component (wired links). |
| 34 | + * Each round, each node sends a message to all of its neighbor (discarded upon reception).</p> |
| 35 | + */ |
| 36 | +public class MessageEngineLoadTesting { |
| 37 | + public static void main(String[] args) { |
| 38 | + Topology topology = new Topology(); |
| 39 | + Class<? extends Node> nodeClass = MyBroadcastNode.class; |
| 40 | + topology.setDefaultNodeModel(nodeClass); |
| 41 | + topology.setTimeUnit(1); |
| 42 | + |
| 43 | +// DelayMessageEngine messageEngine = new DelayMessageEngine(topology); |
| 44 | +// messageEngine.setDelay(250); |
| 45 | +//// messageEngine.disableLinksContinuityChecks(); |
| 46 | +// topology.setMessageEngine(messageEngine); |
| 47 | + |
| 48 | + topology.addClockListener(new TimeLoggingClockListener(topology)); |
| 49 | + |
| 50 | + topology.setWirelessStatus(false); |
| 51 | + new KDeployer(topology, nodeClass).deploy(3000, 50); |
| 52 | + |
| 53 | + System.out.println("Nb nodes: " + topology.getNodes().size()); |
| 54 | + new JViewer(topology); |
| 55 | + topology.start(); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public static class MyBroadcastNode extends Node { |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onStart() { |
| 64 | + super.onStart(); |
| 65 | + setIconSize(2); |
| 66 | + setIcon(Icons.TRANSPARENT); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onClock() { |
| 71 | + sendAll(new Message()); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + private static class TimeLoggingClockListener implements ClockListener { |
| 77 | + private long previousTime; |
| 78 | + private Topology topology; |
| 79 | + |
| 80 | + public TimeLoggingClockListener(Topology topology) { |
| 81 | + this.previousTime = System.currentTimeMillis(); |
| 82 | + this.topology = topology; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onClock() { |
| 87 | + long newTime = System.currentTimeMillis(); |
| 88 | + System.out.println(topology.getTime() + " " + (newTime - previousTime)); |
| 89 | + previousTime = newTime; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static class KDeployer { |
| 94 | + private final Topology topology; |
| 95 | + private final Class<? extends Node> nodeClass; |
| 96 | + |
| 97 | + public KDeployer(Topology topology, Class<? extends Node> nodeClass) { |
| 98 | + this.topology = topology; |
| 99 | + this.nodeClass = nodeClass; |
| 100 | + } |
| 101 | + |
| 102 | + public void deploy(int totalNodes, int k) { |
| 103 | + int connectedComponents = (int)Math.round((double)totalNodes / k); |
| 104 | + System.out.println("Connected Components " + connectedComponents); |
| 105 | + for(int i = 0; i < connectedComponents; i++ ) |
| 106 | + deployK(topology, k, nodeClass); |
| 107 | + } |
| 108 | + |
| 109 | + private static void deployK(Topology topology, int k, Class<? extends Node> nodeClass) { |
| 110 | + List<Node> nodes = new ArrayList(); |
| 111 | + for(int i = 0 ; i < k ; i++) { |
| 112 | + try { |
| 113 | + Node myNode = nodeClass.newInstance(); |
| 114 | + nodes.add(myNode); |
| 115 | + topology.addNode(-1, -1, myNode); |
| 116 | + |
| 117 | + connectAll(topology, nodes, myNode); |
| 118 | + } catch (InstantiationException e) { |
| 119 | + e.printStackTrace(); |
| 120 | + } catch (IllegalAccessException e) { |
| 121 | + e.printStackTrace(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static void connectAll(Topology topology, List<Node> nodes, Node myNode) { |
| 127 | + for(Node n: nodes) |
| 128 | + topology.addLink(new Link(myNode, n, Link.Orientation.UNDIRECTED, Link.Mode.WIRED)); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments