Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.intentperf; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 25 | import org.onlab.util.Counter; |
| 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Device; |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 31 | import org.onosproject.net.MastershipRole; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentEvent; |
| 40 | import org.onosproject.net.intent.IntentListener; |
| 41 | import org.onosproject.net.intent.IntentService; |
| 42 | import org.onosproject.net.intent.Key; |
| 43 | import org.onosproject.net.intent.PointToPointIntent; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import java.util.Collections; |
| 47 | import java.util.Iterator; |
| 48 | import java.util.List; |
| 49 | import java.util.Map; |
| 50 | import java.util.Set; |
| 51 | import java.util.Timer; |
| 52 | import java.util.TimerTask; |
| 53 | import java.util.concurrent.ExecutorService; |
| 54 | import java.util.concurrent.Executors; |
| 55 | import java.util.concurrent.TimeUnit; |
| 56 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 57 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 58 | import static java.lang.String.format; |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 59 | import static org.apache.felix.scr.annotations.ReferenceCardinality.MANDATORY_UNARY; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 60 | import static org.onlab.util.Tools.delay; |
| 61 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 62 | import static org.onosproject.net.intent.IntentEvent.Type.INSTALLED; |
| 63 | import static org.onosproject.net.intent.IntentEvent.Type.WITHDRAWN; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 64 | import static org.slf4j.LoggerFactory.getLogger; |
| 65 | |
| 66 | /** |
| 67 | * Application to set up demos. |
| 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | public class IntentPerfInstaller { |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 74 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 75 | protected CoreService coreService; |
| 76 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 77 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 78 | protected IntentService intentService; |
| 79 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 80 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 81 | protected ClusterService clusterService; |
| 82 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 83 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 84 | protected DeviceService deviceService; |
| 85 | |
| 86 | private ExecutorService worker; |
| 87 | private ApplicationId appId; |
| 88 | private Listener listener; |
| 89 | private Set<Intent> intents; |
| 90 | private Set<Intent> submitted; |
| 91 | private Set<Intent> withdrawn; |
| 92 | private boolean stopped; |
| 93 | |
| 94 | private static final long REPORT_PERIOD = 5000L; //ms |
| 95 | private Timer reportTimer; |
| 96 | |
| 97 | //FIXME make this configurable |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 98 | private static final int NUM_KEYS = 10_000; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 99 | |
| 100 | @Activate |
| 101 | public void activate() { |
| 102 | String nodeId = clusterService.getLocalNode().ip().toString(); |
| 103 | appId = coreService.registerApplication("org.onosproject.intentperf." |
| 104 | + nodeId); |
| 105 | intents = Sets.newHashSet(); |
| 106 | submitted = Sets.newHashSet(); |
| 107 | withdrawn = Sets.newHashSet(); |
| 108 | |
| 109 | worker = Executors.newFixedThreadPool(1, groupedThreads("onos/intent-perf", "worker")); |
| 110 | log.info("Started with Application ID {}", appId.id()); |
| 111 | start(); //FIXME |
| 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | public void deactivate() { |
| 116 | stop(); |
| 117 | log.info("Stopped"); |
| 118 | } |
| 119 | |
| 120 | public void start() { |
| 121 | // perhaps we want to prime before listening... |
| 122 | // we will need to discard the first few results for priming and warmup |
| 123 | listener = new Listener(); |
| 124 | intentService.addListener(listener); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 125 | |
| 126 | long delay = System.currentTimeMillis() % REPORT_PERIOD; |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 127 | reportTimer = new Timer("onos-intent-perf-reporter"); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 128 | reportTimer.scheduleAtFixedRate(new TimerTask() { |
| 129 | @Override |
| 130 | public void run() { |
| 131 | listener.report(); |
| 132 | } |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 133 | }, delay, REPORT_PERIOD); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 134 | |
| 135 | stopped = false; |
| 136 | worker.submit(() -> { |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 137 | delay(2000); // take a breath to start |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 138 | createIntents(NUM_KEYS, 2); //FIXME |
| 139 | prime(); |
| 140 | while (!stopped) { |
| 141 | cycle(); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 142 | delay(800); // take a breath |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 143 | } |
| 144 | }); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | public void stop() { |
| 149 | if (listener != null) { |
| 150 | reportTimer.cancel(); |
| 151 | intentService.removeListener(listener); |
| 152 | listener = null; |
| 153 | reportTimer = null; |
| 154 | } |
| 155 | stopped = true; |
| 156 | try { |
| 157 | worker.awaitTermination(5, TimeUnit.SECONDS); |
| 158 | } catch (InterruptedException e) { |
| 159 | log.warn("Failed to stop worker."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | private void cycle() { |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 165 | long start = System.currentTimeMillis(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 166 | subset(submitted).forEach(this::withdraw); |
| 167 | subset(withdrawn).forEach(this::submit); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 168 | long delta = System.currentTimeMillis() - start; |
| 169 | if (delta > 1000 || delta < 0) { |
| 170 | log.warn("Cycle took {} ms", delta); |
| 171 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private Iterable<Intent> subset(Set<Intent> intents) { |
| 175 | List<Intent> subset = Lists.newArrayList(intents); |
| 176 | Collections.shuffle(subset); |
| 177 | return subset.subList(0, subset.size() / 2); |
| 178 | } |
| 179 | |
| 180 | private void submit(Intent intent) { |
| 181 | intentService.submit(intent); |
| 182 | submitted.add(intent); |
| 183 | withdrawn.remove(intent); //TODO could check result here... |
| 184 | } |
| 185 | |
| 186 | private void withdraw(Intent intent) { |
| 187 | intentService.withdraw(intent); |
| 188 | withdrawn.add(intent); |
| 189 | submitted.remove(intent); //TODO could check result here... |
| 190 | } |
| 191 | |
| 192 | private void createIntents(int numberOfKeys, int pathLength) { |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 193 | Iterator<Device> deviceItr = deviceService.getAvailableDevices().iterator(); |
| 194 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 195 | Device ingressDevice = null; |
| 196 | while (deviceItr.hasNext()) { |
| 197 | Device device = deviceItr.next(); |
| 198 | if (deviceService.getRole(device.id()) == MastershipRole.MASTER) { |
| 199 | ingressDevice = device; |
| 200 | break; |
| 201 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 202 | } |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 203 | checkState(ingressDevice != null, "There are no local devices"); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 204 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 205 | for (int local = 0, i = 0; local < numberOfKeys; i++) { |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 206 | Key key = Key.of(i, appId); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 207 | if (!intentService.isLocal(key)) { |
| 208 | continue; |
| 209 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 210 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 211 | |
| 212 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 213 | //FIXME |
| 214 | ConnectPoint ingress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(1)); |
| 215 | ConnectPoint egress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(2)); |
| 216 | |
| 217 | Intent intent = new PointToPointIntent(appId, key, |
| 218 | selector, treatment, |
| 219 | ingress, egress, |
| 220 | Collections.emptyList()); |
| 221 | intents.add(intent); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 222 | local++; |
| 223 | if (i % 1000 == 0) { |
| 224 | log.info("Building intents... {} ({})", local, i); |
| 225 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 226 | } |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 227 | log.info("Created {} intents", numberOfKeys); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | private void prime() { |
| 231 | int i = 0; |
| 232 | withdrawn.addAll(intents); |
| 233 | for (Intent intent : intents) { |
| 234 | submit(intent); |
| 235 | // only submit half of the intents to start |
| 236 | if (i++ >= intents.size() / 2) { |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | class Listener implements IntentListener { |
| 243 | |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 244 | private final Map<IntentEvent.Type, Counter> counters; |
| 245 | private final Counter runningTotal = new Counter(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 246 | |
| 247 | public Listener() { |
| 248 | counters = initCounters(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | private Map<IntentEvent.Type, Counter> initCounters() { |
| 252 | Map<IntentEvent.Type, Counter> map = Maps.newHashMap(); |
| 253 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 254 | map.put(type, new Counter()); |
| 255 | } |
| 256 | return map; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void event(IntentEvent event) { |
| 261 | if (event.subject().appId().equals(appId)) { |
| 262 | counters.get(event.type()).add(1); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | public void report() { |
| 267 | StringBuilder stringBuilder = new StringBuilder(); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 268 | Counter installed = counters.get(INSTALLED); |
| 269 | Counter withdrawn = counters.get(WITHDRAWN); |
| 270 | double current = installed.throughput() + withdrawn.throughput(); |
| 271 | runningTotal.add(installed.total() + withdrawn.total()); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 272 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 273 | stringBuilder.append(printCounter(type)).append("; "); |
| 274 | } |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 275 | log.info("Throughput: OVERALL={}; CURRENT={}; {}", |
| 276 | format("%.2f", runningTotal.throughput()), |
| 277 | format("%.2f", current), stringBuilder); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private String printCounter(IntentEvent.Type event) { |
| 281 | Counter counter = counters.get(event); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame^] | 282 | String result = format("%s=%.2f", event, counter.throughput()); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 283 | counter.reset(); |
| 284 | return result; |
| 285 | } |
| 286 | } |
| 287 | } |