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 | 375573b | 2015-03-06 01:09:43 -0800 | [diff] [blame] | 75 | private static final int NUM_KEYS = 40_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 | |
Brian O'Connor | 375573b | 2015-03-06 01:09:43 -0800 | [diff] [blame] | 192 | // prefix based on node id |
| 193 | long prefix = ((long) clusterService.getLocalNode().ip().getIp4Address().toInt()) << 32; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 194 | for (int count = 0, k = firstKey; count < numberOfKeys; k++) { |
Brian O'Connor | 375573b | 2015-03-06 01:09:43 -0800 | [diff] [blame] | 195 | Key key = Key.of(prefix + k, appId); |
| 196 | //FIXME comment this out for spread of keys |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 197 | if (!intentService.isLocal(key)) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 198 | // Bail if the key is not local |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 199 | continue; |
| 200 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 201 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 202 | //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] | 203 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 204 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 205 | ConnectPoint ingress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(1)); |
| 206 | ConnectPoint egress = new ConnectPoint(ingressDevice.id(), PortNumber.portNumber(2)); |
| 207 | |
| 208 | Intent intent = new PointToPointIntent(appId, key, |
| 209 | selector, treatment, |
| 210 | ingress, egress, |
| 211 | Collections.emptyList()); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 212 | result.add(intent); |
| 213 | |
| 214 | // Bump up the counter and remember this as the last key used. |
| 215 | count++; |
| 216 | lastKey = k; |
| 217 | if (lastKey % 1000 == 0) { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 218 | log.info("Building intents... {} (attempt: {})", lastKey, count); |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 219 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 220 | } |
Brian O'Connor | bcfeadb | 2015-02-19 21:50:01 -0800 | [diff] [blame] | 221 | log.info("Created {} intents", numberOfKeys); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 222 | return result; |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 225 | // Submits intent operations. |
| 226 | final class Submitter implements Runnable { |
| 227 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 228 | private long lastDuration; |
| 229 | private int lastCount; |
| 230 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 231 | private Set<Intent> intents = Sets.newHashSet(); |
| 232 | private Set<Intent> submitted = Sets.newHashSet(); |
| 233 | private Set<Intent> withdrawn = Sets.newHashSet(); |
| 234 | |
| 235 | private Submitter(Set<Intent> intents) { |
| 236 | this.intents = intents; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 237 | lastCount = NUM_KEYS / 4; |
| 238 | lastDuration = 1000; // 1 second |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void run() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 243 | prime(); |
| 244 | while (!stopped) { |
| 245 | cycle(); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 249 | private Iterable<Intent> subset(Set<Intent> intents) { |
| 250 | List<Intent> subset = Lists.newArrayList(intents); |
| 251 | Collections.shuffle(subset); |
| 252 | return subset.subList(0, lastCount); |
| 253 | } |
| 254 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 255 | // Submits the specified intent. |
| 256 | private void submit(Intent intent) { |
| 257 | intentService.submit(intent); |
| 258 | submitted.add(intent); |
| 259 | withdrawn.remove(intent); //TODO could check result here... |
| 260 | } |
| 261 | |
| 262 | // Withdraws the specified intent. |
| 263 | private void withdraw(Intent intent) { |
| 264 | intentService.withdraw(intent); |
| 265 | withdrawn.add(intent); |
| 266 | submitted.remove(intent); //TODO could check result here... |
| 267 | } |
| 268 | |
| 269 | // Primes the cycle. |
| 270 | private void prime() { |
| 271 | int i = 0; |
| 272 | withdrawn.addAll(intents); |
| 273 | for (Intent intent : intents) { |
| 274 | submit(intent); |
| 275 | // only submit half of the intents to start |
| 276 | if (i++ >= intents.size() / 2) { |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Runs a single operation cycle. |
| 283 | private void cycle() { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 284 | //TODO consider running without rate adjustment |
| 285 | adjustRates(); |
| 286 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 287 | long start = currentTimeMillis(); |
| 288 | subset(submitted).forEach(this::withdraw); |
| 289 | subset(withdrawn).forEach(this::submit); |
| 290 | long delta = currentTimeMillis() - start; |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 291 | |
| 292 | if (delta > GOAL_CYCLE_PERIOD * 3 || delta < 0) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 293 | log.warn("Cycle took {} ms", delta); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 294 | } |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 295 | |
| 296 | int difference = GOAL_CYCLE_PERIOD - (int) delta; |
| 297 | if (difference > 0) { |
| 298 | delay(difference); |
| 299 | } |
| 300 | |
| 301 | lastDuration = delta; |
| 302 | } |
| 303 | |
| 304 | int cycleCount = 0; |
| 305 | private void adjustRates() { |
| 306 | //FIXME need to iron out the rate adjustment |
| 307 | if (++cycleCount % 5 == 0) { //TODO: maybe use a timer (we should do this every 5-10 sec) |
Brian O'Connor | 375573b | 2015-03-06 01:09:43 -0800 | [diff] [blame] | 308 | if (listener.requestThroughput() - listener.processedThroughput() <= 2000 && //was 500 |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 309 | lastDuration <= GOAL_CYCLE_PERIOD) { |
Brian O'Connor | 375573b | 2015-03-06 01:09:43 -0800 | [diff] [blame] | 310 | lastCount = Math.min(lastCount + 1000, intents.size() / 2); |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 311 | } else { |
| 312 | lastCount *= 0.8; |
| 313 | } |
| 314 | log.info("last count: {}, last duration: {} ms (sub: {} vs inst: {})", |
| 315 | lastCount, lastDuration, listener.requestThroughput(), listener.processedThroughput()); |
| 316 | } |
| 317 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 321 | // Event listener to monitor throughput. |
| 322 | final class Listener implements IntentListener { |
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 Map<IntentEvent.Type, Counter> counters; |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 325 | private final Counter runningTotal = new Counter(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 326 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 327 | private volatile double processedThroughput = 0; |
| 328 | private volatile double requestThroughput = 0; |
| 329 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 330 | public Listener() { |
| 331 | counters = initCounters(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | private Map<IntentEvent.Type, Counter> initCounters() { |
| 335 | Map<IntentEvent.Type, Counter> map = Maps.newHashMap(); |
| 336 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
| 337 | map.put(type, new Counter()); |
| 338 | } |
| 339 | return map; |
| 340 | } |
| 341 | |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 342 | public double processedThroughput() { |
| 343 | return processedThroughput; |
| 344 | } |
| 345 | |
| 346 | public double requestThroughput() { |
| 347 | return requestThroughput; |
| 348 | } |
| 349 | |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 350 | @Override |
| 351 | public void event(IntentEvent event) { |
| 352 | if (event.subject().appId().equals(appId)) { |
| 353 | counters.get(event.type()).add(1); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | public void report() { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 358 | Map<IntentEvent.Type, Counter> reportCounters = counters; |
| 359 | counters = initCounters(); |
| 360 | |
| 361 | // update running total and latest throughput |
| 362 | Counter installed = reportCounters.get(INSTALLED); |
| 363 | Counter withdrawn = reportCounters.get(WITHDRAWN); |
| 364 | processedThroughput = installed.throughput() + withdrawn.throughput(); |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 365 | runningTotal.add(installed.total() + withdrawn.total()); |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 366 | |
| 367 | Counter installReq = reportCounters.get(INSTALL_REQ); |
| 368 | Counter withdrawReq = reportCounters.get(WITHDRAW_REQ); |
| 369 | requestThroughput = installReq.throughput() + withdrawReq.throughput(); |
| 370 | |
| 371 | // build the string to report |
| 372 | StringBuilder stringBuilder = new StringBuilder(); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 373 | for (IntentEvent.Type type : IntentEvent.Type.values()) { |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 374 | Counter counter = reportCounters.get(type); |
| 375 | stringBuilder.append(format("%s=%.2f;", type, counter.throughput())); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 376 | } |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 377 | log.info("Throughput: OVERALL={}; CURRENT={}; {}", |
| 378 | format("%.2f", runningTotal.throughput()), |
Brian O'Connor | 36ef71a | 2015-02-24 12:05:01 -0800 | [diff] [blame] | 379 | format("%.2f", processedThroughput), |
| 380 | stringBuilder); |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
Brian O'Connor | 6ccba96 | 2015-02-17 18:16:02 -0800 | [diff] [blame] | 383 | } |