Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 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.loadtest; |
| 17 | |
| 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 19 | import static org.onlab.util.Tools.get; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Dictionary; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 23 | import java.util.List; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 24 | import java.util.concurrent.Executors; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 25 | import java.util.concurrent.ScheduledExecutorService; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 26 | import java.util.concurrent.Semaphore; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 27 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 28 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 29 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 30 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 31 | import java.util.stream.Collectors; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 32 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 33 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Activate; |
| 35 | import org.apache.felix.scr.annotations.Component; |
| 36 | import org.apache.felix.scr.annotations.Deactivate; |
| 37 | import org.apache.felix.scr.annotations.Modified; |
| 38 | import org.apache.felix.scr.annotations.Property; |
| 39 | import org.apache.felix.scr.annotations.Reference; |
| 40 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 41 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 42 | import org.onlab.util.Tools; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 43 | import org.onosproject.cfg.ComponentConfigService; |
| 44 | import org.onosproject.core.ApplicationId; |
| 45 | import org.onosproject.core.CoreService; |
| 46 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 47 | import org.onosproject.store.service.StorageService; |
| 48 | import org.osgi.service.component.ComponentContext; |
| 49 | import org.slf4j.Logger; |
| 50 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 51 | import com.google.common.collect.Lists; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 52 | import com.google.common.util.concurrent.RateLimiter; |
| 53 | |
| 54 | /** |
| 55 | * Simple application for load testing distributed consensus. |
| 56 | * <p> |
| 57 | * This application simply increments as {@link AsyncAtomicCounter} at a configurable rate. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service(value = DistributedConsensusLoadTest.class) |
| 61 | public class DistributedConsensusLoadTest { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected ComponentConfigService configService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected StorageService storageService; |
| 70 | |
| 71 | private ApplicationId appId; |
| 72 | |
| 73 | private AtomicBoolean stopped = new AtomicBoolean(false); |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected CoreService coreService; |
| 77 | |
| 78 | private static final int DEFAULT_RATE = 100; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 79 | private static final int TOTAL_COUNTERS = 50; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 80 | |
| 81 | @Property(name = "rate", intValue = DEFAULT_RATE, |
| 82 | label = "Total number of increments per second to the atomic counter") |
| 83 | protected int rate = 0; |
| 84 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 85 | private final AtomicLong previousReportTime = new AtomicLong(0); |
| 86 | private final AtomicLong previousCount = new AtomicLong(0); |
| 87 | private final AtomicInteger increments = new AtomicInteger(0); |
| 88 | private final List<AsyncAtomicCounter> counters = Lists.newArrayList(); |
| 89 | private final ScheduledExecutorService runner = Executors.newSingleThreadScheduledExecutor(); |
| 90 | private final ScheduledExecutorService reporter = Executors.newSingleThreadScheduledExecutor(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 91 | |
| 92 | @Activate |
| 93 | public void activate(ComponentContext context) { |
| 94 | configService.registerProperties(getClass()); |
| 95 | appId = coreService.registerApplication("org.onosproject.loadtest"); |
| 96 | log.info("Started with {}", appId); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 97 | for (int i = 0; i < TOTAL_COUNTERS; ++i) { |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 98 | AsyncAtomicCounter counter = |
| 99 | storageService.getAsyncAtomicCounter(String.format("onos-app-loadtest-counter-%d", i)); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 100 | counters.add(counter); |
| 101 | } |
| 102 | reporter.scheduleWithFixedDelay(() -> { |
| 103 | Tools.allOf(counters.stream() |
| 104 | .map(AsyncAtomicCounter::get) |
| 105 | .collect(Collectors.toList())) |
| 106 | .whenComplete((r, e) -> { |
| 107 | if (e == null) { |
| 108 | long newCount = r.stream().reduce(Long::sum).get(); |
| 109 | long currentTime = System.currentTimeMillis(); |
| 110 | long delta = currentTime - previousReportTime.getAndSet(currentTime); |
| 111 | long rate = (newCount - previousCount.getAndSet(newCount)) * 1000 / delta; |
| 112 | log.info("{} updates per second", rate); |
| 113 | } else { |
| 114 | log.warn(e.getMessage()); |
| 115 | } |
| 116 | }); |
| 117 | }, 5, 5, TimeUnit.SECONDS); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 118 | modified(null); |
| 119 | } |
| 120 | |
| 121 | private void startTest() { |
| 122 | stopped.set(false); |
| 123 | RateLimiter limiter = RateLimiter.create(rate); |
| 124 | Semaphore s = new Semaphore(100); |
| 125 | while (!stopped.get()) { |
| 126 | limiter.acquire(); |
| 127 | s.acquireUninterruptibly(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 128 | counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> { |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 129 | s.release(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 130 | if (e == null) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 131 | increments.incrementAndGet(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 132 | } |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private void stopTest() { |
| 138 | stopped.set(true); |
| 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate(ComponentContext context) { |
| 143 | configService.unregisterProperties(getClass(), false); |
| 144 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 145 | runner.shutdown(); |
| 146 | reporter.shutdown(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 147 | log.info("Stopped"); |
| 148 | } |
| 149 | |
| 150 | @Modified |
| 151 | public void modified(ComponentContext context) { |
| 152 | int newRate = DEFAULT_RATE; |
| 153 | if (context != null) { |
| 154 | Dictionary properties = context.getProperties(); |
| 155 | try { |
| 156 | String s = get(properties, "rate"); |
| 157 | newRate = isNullOrEmpty(s) |
| 158 | ? rate : Integer.parseInt(s.trim()); |
| 159 | } catch (Exception e) { |
| 160 | return; |
| 161 | } |
| 162 | } |
| 163 | if (newRate != rate) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 164 | log.info("Per node rate changed to {}", newRate); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 165 | rate = newRate; |
| 166 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 167 | runner.execute(this::startTest); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |