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; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 47 | import java.util.HashSet; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 48 | import java.util.Iterator; |
| 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Set; |
| 52 | import java.util.Timer; |
| 53 | import java.util.TimerTask; |
| 54 | import java.util.concurrent.ExecutorService; |
| 55 | import java.util.concurrent.Executors; |
| 56 | import java.util.concurrent.TimeUnit; |
| 57 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 58 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 59 | import static java.lang.String.format; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 60 | import static java.lang.System.currentTimeMillis; |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 61 | import static org.apache.felix.scr.annotations.ReferenceCardinality.MANDATORY_UNARY; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 62 | import static org.onlab.util.Tools.delay; |
| 63 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 64 | import static org.onosproject.net.intent.IntentEvent.Type.*; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 68 | * Application to test sustained intent throughput. |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | public class IntentPerfInstaller { |
| 72 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 73 | //FIXME make this configurable |
| 74 | private static final int NUM_WORKERS = 1; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 75 | private static final int NUM_KEYS = 20_000; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 76 | |
| 77 | public static final int START_DELAY = 5_000; // ms |
| 78 | private static final int REPORT_PERIOD = 5_000; //ms |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 79 | private static final int GOAL_CYCLE_PERIOD = 1_000; //ms |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 80 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 81 | private final Logger log = getLogger(getClass()); |
| 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 CoreService coreService; |
| 85 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 86 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 87 | protected IntentService intentService; |
| 88 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 89 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 90 | protected ClusterService clusterService; |
| 91 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 92 | @Reference(cardinality = MANDATORY_UNARY) |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 93 | protected DeviceService deviceService; |
| 94 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 95 | private ExecutorService workers; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 96 | private ApplicationId appId; |
| 97 | private Listener listener; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 98 | private boolean stopped; |
| 99 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 100 | private Timer reportTimer; |
| 101 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 102 | // FIXME this variable isn't shared properly between multiple worker threads |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 103 | private int lastKey = 0; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 104 | |
| 105 | @Activate |
| 106 | public void activate() { |
| 107 | String nodeId = clusterService.getLocalNode().ip().toString(); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 108 | appId = coreService.registerApplication("org.onosproject.intentperf." + nodeId); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 109 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 110 | reportTimer = new Timer("onos-intent-perf-reporter"); |
| 111 | workers = Executors.newFixedThreadPool(NUM_WORKERS, groupedThreads("onos/intent-perf", "worker-%d")); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 112 | log.info("Started with Application ID {}", appId.id()); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 113 | |
| 114 | // Schedule delayed start |
| 115 | reportTimer.schedule(new TimerTask() { |
| 116 | @Override |
| 117 | public void run() { |
| 118 | start(); |
| 119 | } |
| 120 | }, START_DELAY); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Deactivate |
| 124 | public void deactivate() { |
| 125 | stop(); |
| 126 | log.info("Stopped"); |
| 127 | } |
| 128 | |
| 129 | public void start() { |
| 130 | // perhaps we want to prime before listening... |
| 131 | // we will need to discard the first few results for priming and warmup |
| 132 | listener = new Listener(); |
| 133 | intentService.addListener(listener); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 134 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 135 | // Schedule reporter task on report period boundary |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 136 | reportTimer.scheduleAtFixedRate(new TimerTask() { |
| 137 | @Override |
| 138 | public void run() { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 139 | //adjustRates(); // FIXME we currently adjust rates in the cycle thread |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 140 | listener.report(); |
| 141 | } |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 142 | }, REPORT_PERIOD - currentTimeMillis() % REPORT_PERIOD, REPORT_PERIOD); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 143 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 144 | // Submit workers |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 145 | stopped = false; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 146 | Set<Device> devices = new HashSet<>(); |
| 147 | for (int i = 0; i < NUM_WORKERS; i++) { |
| 148 | workers.submit(new Submitter(createIntents(NUM_KEYS, 2, lastKey, devices))); |
| 149 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public void stop() { |
| 153 | if (listener != null) { |
| 154 | reportTimer.cancel(); |
| 155 | intentService.removeListener(listener); |
| 156 | listener = null; |
| 157 | reportTimer = null; |
| 158 | } |
| 159 | stopped = true; |
| 160 | try { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 161 | workers.awaitTermination(5, TimeUnit.SECONDS); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 162 | } catch (InterruptedException e) { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 163 | log.warn("Failed to stop worker", e); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Creates a specified number of intents for testing purposes. |
| 169 | * |
| 170 | * @param numberOfKeys number of intents |
| 171 | * @param pathLength path depth |
| 172 | * @param firstKey first key to attempt |
| 173 | * @param devices set of previously utilized devices @return set of intents |
| 174 | */ |
| 175 | private Set<Intent> createIntents(int numberOfKeys, int pathLength, |
| 176 | int firstKey, Set<Device> devices) { |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 177 | Iterator<Device> deviceItr = deviceService.getAvailableDevices().iterator(); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 178 | Set<Intent> result = new HashSet<>(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 179 | |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 180 | Device ingressDevice = null; |
| 181 | while (deviceItr.hasNext()) { |
| 182 | Device device = deviceItr.next(); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 183 | if (deviceService.getRole(device.id()) == MastershipRole.MASTER && |
| 184 | !devices.contains(device)) { |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 185 | ingressDevice = device; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 186 | devices.add(device); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 187 | break; |
| 188 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 189 | } |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 190 | checkState(ingressDevice != null, "There are no local devices"); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 191 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 192 | for (int count = 0, k = firstKey; count < numberOfKeys; k++) { |
| 193 | Key key = Key.of(k, appId); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 194 | if (!intentService.isLocal(key)) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 195 | // Bail if the key is not local |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 196 | continue; |
| 197 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 198 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 199 | //FIXME we currently ignore the path length and always use the same device |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 200 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 201 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 202 | ConnectPoint ingress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(1)); |
| 203 | ConnectPoint egress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(2)); |
| 204 | |
| 205 | Intent intent = new PointToPointIntent(appId, key, |
| 206 | selector, treatment, |
| 207 | ingress, egress, |
| 208 | Collections.emptyList()); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 209 | result.add(intent); |
| 210 | |
| 211 | // Bump up the counter and remember this as the last key used. |
| 212 | count++; |
| 213 | lastKey = k; |
| 214 | if (lastKey % 1000 == 0) { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 215 | log.info("Building intents... {} (attempt: {})", lastKey, count); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 216 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 217 | } |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 218 | log.info("Created {} intents", numberOfKeys); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 219 | return result; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 222 | // Submits intent operations. |
| 223 | final class Submitter implements Runnable { |
| 224 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 225 | private long lastDuration; |
| 226 | private int lastCount; |
| 227 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 228 | private Set<Intent> intents = Sets.newHashSet(); |
| 229 | private Set<Intent> submitted = Sets.newHashSet(); |
| 230 | private Set<Intent> withdrawn = Sets.newHashSet(); |
| 231 | |
| 232 | private Submitter(Set<Intent> intents) { |
| 233 | this.intents = intents; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 234 | lastCount = NUM_KEYS / 4; |
| 235 | lastDuration = 1000; // 1 second |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void run() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 240 | prime(); |
| 241 | while (!stopped) { |
| 242 | cycle(); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 246 | private Iterable<Intent> subset(Set<Intent> intents) { |
| 247 | List<Intent> subset = Lists.newArrayList(intents); |
| 248 | Collections.shuffle(subset); |
| 249 | return subset.subList(0, lastCount); |
| 250 | } |
| 251 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 252 | // Submits the specified intent. |
| 253 | private void submit(Intent intent) { |
| 254 | intentService.submit(intent); |
| 255 | submitted.add(intent); |
| 256 | withdrawn.remove(intent); //TODO could check result here... |
| 257 | } |
| 258 | |
| 259 | // Withdraws the specified intent. |
| 260 | private void withdraw(Intent intent) { |
| 261 | intentService.withdraw(intent); |
| 262 | withdrawn.add(intent); |
| 263 | submitted.remove(intent); //TODO could check result here... |
| 264 | } |
| 265 | |
| 266 | // Primes the cycle. |
| 267 | private void prime() { |
| 268 | int i = 0; |
| 269 | withdrawn.addAll(intents); |
| 270 | for (Intent intent : intents) { |
| 271 | submit(intent); |
| 272 | // only submit half of the intents to start |
| 273 | if (i++ >= intents.size() / 2) { |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Runs a single operation cycle. |
| 280 | private void cycle() { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 281 | //TODO consider running without rate adjustment |
| 282 | adjustRates(); |
| 283 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 284 | long start = currentTimeMillis(); |
| 285 | subset(submitted).forEach(this::withdraw); |
| 286 | subset(withdrawn).forEach(this::submit); |
| 287 | long delta = currentTimeMillis() - start; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 288 | |
| 289 | if (delta > GOAL_CYCLE_PERIOD * 3 || delta < 0) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 290 | log.warn("Cycle took {} ms", delta); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 291 | } |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 292 | |
| 293 | int difference = GOAL_CYCLE_PERIOD - (int) delta; |
| 294 | if (difference > 0) { |
| 295 | delay(difference); |
| 296 | } |
| 297 | |
| 298 | lastDuration = delta; |
| 299 | } |
| 300 | |
| 301 | int cycleCount = 0; |
| 302 | private void adjustRates() { |
| 303 | //FIXME need to iron out the rate adjustment |
| 304 | if (++cycleCount % 5 == 0) { //TODO: maybe use a timer (we should do this every 5-10 sec) |
| 305 | if (listener.requestThroughput() - listener.processedThroughput() <= 500 && |
| 306 | lastDuration <= GOAL_CYCLE_PERIOD) { |
| 307 | lastCount = Math.min(lastCount + 100, intents.size() / 2); |
| 308 | } else { |
| 309 | lastCount *= 0.8; |
| 310 | } |
| 311 | log.info("last count: {}, last duration: {} ms (sub: {} vs inst: {})", |
| 312 | lastCount, lastDuration, listener.requestThroughput(), listener.processedThroughput()); |
| 313 | } |
| 314 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 318 | // Event listener to monitor throughput. |
| 319 | final class Listener implements IntentListener { |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 320 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 321 | private Map<IntentEvent.Type, Counter> counters; |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 322 | private final Counter runningTotal = new Counter(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 323 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 324 | private volatile double processedThroughput = 0; |
| 325 | private volatile double requestThroughput = 0; |
| 326 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 327 | public Listener() { |
| 328 | counters = initCounters(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | private Map<IntentEvent.Type, Counter> initCounters() { |
| 332 | Map<IntentEvent.Type, Counter> map = Maps.newHashMap(); |
| 333 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 334 | map.put(type, new Counter()); |
| 335 | } |
| 336 | return map; |
| 337 | } |
| 338 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 339 | public double processedThroughput() { |
| 340 | return processedThroughput; |
| 341 | } |
| 342 | |
| 343 | public double requestThroughput() { |
| 344 | return requestThroughput; |
| 345 | } |
| 346 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 347 | @Override |
| 348 | public void event(IntentEvent event) { |
| 349 | if (event.subject().appId().equals(appId)) { |
| 350 | counters.get(event.type()).add(1); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | public void report() { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 355 | Map<IntentEvent.Type, Counter> reportCounters = counters; |
| 356 | counters = initCounters(); |
| 357 | |
| 358 | // update running total and latest throughput |
| 359 | Counter installed = reportCounters.get(INSTALLED); |
| 360 | Counter withdrawn = reportCounters.get(WITHDRAWN); |
| 361 | processedThroughput = installed.throughput() + withdrawn.throughput(); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 362 | runningTotal.add(installed.total() + withdrawn.total()); |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 363 | |
| 364 | Counter installReq = reportCounters.get(INSTALL_REQ); |
| 365 | Counter withdrawReq = reportCounters.get(WITHDRAW_REQ); |
| 366 | requestThroughput = installReq.throughput() + withdrawReq.throughput(); |
| 367 | |
| 368 | // build the string to report |
| 369 | StringBuilder stringBuilder = new StringBuilder(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 370 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 371 | Counter counter = reportCounters.get(type); |
| 372 | stringBuilder.append(format("%s=%.2f;", type, counter.throughput())); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 373 | } |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 374 | log.info("Throughput: OVERALL={}; CURRENT={}; {}", |
| 375 | format("%.2f", runningTotal.throughput()), |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 376 | format("%.2f", processedThroughput), |
| 377 | stringBuilder); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 380 | } |